build: add release script (#789) (#790)

This commit is contained in:
Li Chen 2020-10-24 02:54:29 +08:00 committed by GitHub
parent 3b7b697fc8
commit fe63890618
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 153 additions and 2 deletions

View File

@ -34,8 +34,7 @@
"lint": "prettier --write --parser typescript \"src/**/*.ts\"",
"test": "jest --clearCache && jest --runInBand --forceExit",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"prepublishOnly": "yarn build && yarn changelog",
"postpublish": "git add CHANGELOG.md && git commit -m 'chore: changelog [ci skip]'"
"release": "node scripts/release.js"
},
"gitHooks": {
"pre-commit": "lint-staged",
@ -118,6 +117,7 @@
"bootstrap": "^4.5.0",
"conventional-changelog-cli": "^2.0.31",
"cross-env": "^7.0.2",
"enquirer": "^2.3.6",
"jest": "^26.4.2",
"js-yaml": "^3.14.0",
"less": "^3.11.2",
@ -133,6 +133,7 @@
"puppeteer": "^3.0.0",
"rimraf": "^3.0.2",
"sass": "^1.26.5",
"semver": "^7.3.2",
"source-map-support": "^0.5.19",
"typescript": "^4.0.2",
"vue-router": "^4.0.0-beta.2",

143
scripts/release.js Normal file
View File

@ -0,0 +1,143 @@
/**
* modified from https://github.com/vuejs/vue-next/blob/master/scripts/release.js
*/
const execa = require('execa')
const path = require('path')
const fs = require('fs')
const args = require('minimist')(process.argv.slice(2))
const semver = require('semver')
const chalk = require('chalk')
const { prompt } = require('enquirer')
const currentVersion = require('../package.json').version
const pkgPath = path.resolve(__dirname, '../package.json')
const isDryRun = args.dry
const skipBuild = args.skipBuild
const versionIncrements = [
'patch',
'minor',
'major',
'prepatch',
'preminor',
'premajor',
'prerelease'
]
const inc = (i) => semver.inc(currentVersion, i)
const run = (bin, args, opts = {}) =>
execa(bin, args, { stdio: 'inherit', ...opts })
const dryRun = (bin, args, opts = {}) =>
console.log(chalk.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts)
const runIfNotDry = isDryRun ? dryRun : run
const step = (msg) => console.log(chalk.cyan(msg))
async function main() {
let targetVersion = args._[0]
if (!targetVersion) {
// no explicit version, offer suggestions
const { release } = await prompt({
type: 'select',
name: 'release',
message: 'Select release type',
choices: versionIncrements
.map((i) => `${i} (${inc(i)})`)
.concat(['custom'])
})
if (release === 'custom') {
targetVersion = (
await prompt({
type: 'input',
name: 'version',
message: 'Input custom version',
initial: currentVersion
})
).version
} else {
targetVersion = release.match(/\((.*)\)/)[1]
}
}
if (!semver.valid(targetVersion)) {
throw new Error(`invalid target version: ${targetVersion}`)
}
const { yes } = await prompt({
type: 'confirm',
name: 'yes',
message: `Releasing v${targetVersion}. Confirm?`
})
if (!yes) {
return
}
step('\nUpdating package version...')
updateVersion(targetVersion)
step('\nBuilding package...')
if (!skipBuild && !isDryRun) {
await run('yarn', ['build'])
} else {
console.log(`(skipped)`)
}
step('\nGenerating changelog...')
await run('yarn', ['changelog'])
const { stdout } = await run('git', ['diff'], { stdio: 'pipe' })
if (stdout) {
step('\nCommitting changes...')
await runIfNotDry('git', ['add', '-A'])
await runIfNotDry('git', ['commit', '-m', `release: v${targetVersion}`])
} else {
console.log('No changes to commit.')
}
step('\nPublishing package...')
await publishPackage(targetVersion, runIfNotDry)
step('\nPushing to GitHub...')
await runIfNotDry('git', ['tag', `v${targetVersion}`])
await runIfNotDry('git', ['push', 'origin', `refs/tags/v${targetVersion}`])
await runIfNotDry('git', ['push'])
if (isDryRun) {
console.log(`\nDry run finished - run git diff to see package changes.`)
}
console.log()
}
function updateVersion(version) {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
pkg.version = version
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
}
async function publishPackage(version, runIfNotDry) {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
const pkgName = pkg.name
try {
await runIfNotDry(
'yarn',
['publish', '--new-version', version, '--access', 'public'],
{
stdio: 'pipe'
}
)
console.log(chalk.green(`Successfully published ${pkgName}@${version}`))
} catch (e) {
if (e.stderr.match(/previously published/)) {
console.log(chalk.red(`Skipping already published: ${pkgName}`))
} else {
throw e
}
}
}
main().catch((err) => {
console.error(err)
})

View File

@ -2349,6 +2349,13 @@ end-of-stream@^1.1.0, end-of-stream@^1.4.1:
dependencies:
once "^1.4.0"
enquirer@^2.3.6:
version "2.3.6"
resolved "https://registry.npmjs.org/enquirer/-/enquirer-2.3.6.tgz#2a7fe5dd634a1e4125a975ec994ff5456dc3734d"
integrity sha512-yjNnPr315/FjS4zIsUxYguYUPP2e1NK4d7E7ZOLiyYCcbFBiTMyID+2wvm2w6+pZ/odMA7cRkjhsPbltwBOrLg==
dependencies:
ansi-colors "^4.1.1"
enquirer@^2.3.6:
version "2.3.6"
resolved "https://registry.yarnpkg.com/enquirer/-/enquirer-2.3.6.tgz#2a7fe5dd634a1e4125a975ec994ff5456dc3734d"