vue3-yuanma/scripts/release.js

221 lines
6.0 KiB
JavaScript
Raw Normal View History

2019-12-11 10:29:52 +08:00
const args = require('minimist')(process.argv.slice(2))
const fs = require('fs')
const path = require('path')
2019-12-21 02:43:48 +08:00
const chalk = require('chalk')
2019-12-11 10:29:52 +08:00
const semver = require('semver')
const currentVersion = require('../package.json').version
const { prompt } = require('enquirer')
const execa = require('execa')
const preId =
args.preid ||
(semver.prerelease(currentVersion) && semver.prerelease(currentVersion)[0])
2019-12-11 10:29:52 +08:00
const isDryRun = args.dry
const skipTests = args.skipTests
const skipBuild = args.skipBuild
const packages = fs
.readdirSync(path.resolve(__dirname, '../packages'))
.filter(p => !p.endsWith('.ts') && !p.startsWith('.'))
const skippedPackages = []
2019-12-21 02:43:48 +08:00
2019-12-11 10:29:52 +08:00
const versionIncrements = [
'patch',
'minor',
'major',
...(preId ? ['prepatch', 'preminor', 'premajor', 'prerelease'] : [])
2019-12-11 10:29:52 +08:00
]
const inc = i => semver.inc(currentVersion, i, preId)
const bin = name => path.resolve(__dirname, '../node_modules/.bin/' + name)
const run = (bin, args, opts = {}) =>
execa(bin, args, { stdio: 'inherit', ...opts })
2019-12-21 02:43:48 +08:00
const dryRun = (bin, args, opts = {}) =>
console.log(chalk.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts)
const runIfNotDry = isDryRun ? dryRun : run
2019-12-11 10:29:52 +08:00
const getPkgRoot = pkg => path.resolve(__dirname, '../packages/' + pkg)
2019-12-21 02:43:48 +08:00
const step = msg => console.log(chalk.cyan(msg))
2019-12-11 10:29:52 +08:00
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
}
// run tests before release
2019-12-21 02:43:48 +08:00
step('\nRunning tests...')
if (!skipTests && !isDryRun) {
2019-12-11 10:29:52 +08:00
await run(bin('jest'), ['--clearCache'])
await run('yarn', ['test'])
2019-12-21 02:43:48 +08:00
} else {
console.log(`(skipped)`)
2019-12-11 10:29:52 +08:00
}
// update all package versions and inter-dependencies
2019-12-21 02:43:48 +08:00
step('\nUpdating cross dependencies...')
2019-12-11 10:29:52 +08:00
updateVersions(targetVersion)
// build all packages with types
2019-12-21 02:43:48 +08:00
step('\nBuilding all packages...')
if (!skipBuild && !isDryRun) {
2019-12-11 11:14:02 +08:00
await run('yarn', ['build', '--release'])
2019-12-11 10:29:52 +08:00
// test generated dts files
2019-12-21 02:43:48 +08:00
step('\nVerifying type declarations...')
await run('yarn', ['test-dts-only'])
2019-12-21 02:43:48 +08:00
} else {
console.log(`(skipped)`)
2019-12-11 10:29:52 +08:00
}
// generate changelog
await run(`yarn`, ['changelog'])
2019-12-21 02:43:48 +08:00
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}`])
2019-12-11 10:29:52 +08:00
} else {
2019-12-21 02:43:48 +08:00
console.log('No changes to commit.')
}
// publish packages
step('\nPublishing packages...')
for (const pkg of packages) {
await publishPackage(pkg, targetVersion, runIfNotDry)
2019-12-21 02:43:48 +08:00
}
// push to GitHub
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.`)
}
2019-12-11 10:29:52 +08:00
2019-12-21 02:43:48 +08:00
if (skippedPackages.length) {
console.log(
chalk.yellow(
`The following packages are skipped and NOT published:\n- ${skippedPackages.join(
'\n- '
)}`
)
)
2019-12-11 10:29:52 +08:00
}
2019-12-21 02:43:48 +08:00
console.log()
2019-12-11 10:29:52 +08:00
}
function updateVersions(version) {
// 1. update root package.json
updatePackage(path.resolve(__dirname, '..'), version)
// 2. update all packages
packages.forEach(p => updatePackage(getPkgRoot(p), version))
}
function updatePackage(pkgRoot, version) {
2019-12-21 02:43:48 +08:00
const pkgPath = path.resolve(pkgRoot, 'package.json')
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
2019-12-11 10:29:52 +08:00
pkg.version = version
2019-12-21 02:43:48 +08:00
updateDeps(pkg, 'dependencies', version)
updateDeps(pkg, 'peerDependencies', version)
2019-12-11 10:29:52 +08:00
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
}
2019-12-21 02:43:48 +08:00
function updateDeps(pkg, depType, version) {
const deps = pkg[depType]
2019-12-18 10:51:41 +08:00
if (!deps) return
Object.keys(deps).forEach(dep => {
if (
dep === 'vue' ||
(dep.startsWith('@vue') && packages.includes(dep.replace(/^@vue\//, '')))
) {
2019-12-21 02:43:48 +08:00
console.log(
chalk.yellow(`${pkg.name} -> ${depType} -> ${dep}@${version}`)
)
2019-12-18 10:51:41 +08:00
deps[dep] = version
}
})
}
async function publishPackage(pkgName, version, runIfNotDry) {
2020-01-03 07:08:42 +08:00
if (skippedPackages.includes(pkgName)) {
2019-12-21 02:43:48 +08:00
return
}
2019-12-11 10:29:52 +08:00
const pkgRoot = getPkgRoot(pkgName)
2019-12-21 02:43:48 +08:00
const pkgPath = path.resolve(pkgRoot, 'package.json')
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
if (pkg.private) {
return
}
// for now (alpha/beta phase), every package except "vue" can be published as
// `latest`, whereas "vue" will be published under the "next" tag.
2020-01-22 23:51:17 +08:00
const releaseTag = pkgName === 'vue' ? 'next' : null
2020-01-13 20:09:30 +08:00
// TODO use inferred release channel after official 3.0 release
2020-01-22 23:51:17 +08:00
// const releaseTag = semver.prerelease(version)[0] || null
2020-01-14 06:53:54 +08:00
step(`Publishing ${pkgName}...`)
2019-12-21 02:43:48 +08:00
try {
await runIfNotDry(
'yarn',
[
'publish',
'--new-version',
version,
2020-01-22 23:51:17 +08:00
...(releaseTag ? ['--tag', releaseTag] : []),
2019-12-21 02:43:48 +08:00
'--access',
'public'
],
{
cwd: pkgRoot,
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
}
2019-12-11 10:29:52 +08:00
}
}
main().catch(err => {
console.error(err)
})