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')
|
|
|
|
|
2020-10-16 00:28:40 +08:00
|
|
|
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('.'))
|
|
|
|
|
2020-02-19 03:48:56 +08:00
|
|
|
const skippedPackages = []
|
2019-12-21 02:43:48 +08:00
|
|
|
|
2019-12-11 10:29:52 +08:00
|
|
|
const versionIncrements = [
|
|
|
|
'patch',
|
|
|
|
'minor',
|
|
|
|
'major',
|
2020-10-16 00:28:40 +08:00
|
|
|
...(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') {
|
2021-07-24 07:22:47 +08:00
|
|
|
targetVersion = (
|
|
|
|
await prompt({
|
|
|
|
type: 'input',
|
|
|
|
name: 'version',
|
|
|
|
message: 'Input custom version',
|
|
|
|
initial: currentVersion
|
|
|
|
})
|
|
|
|
).version
|
2019-12-11 10:29:52 +08:00
|
|
|
} 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'])
|
2021-03-01 23:55:39 +08:00
|
|
|
await run('yarn', ['test', '--bail'])
|
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...')
|
2020-06-13 06:03:51 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-03 07:12:54 +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) {
|
2020-01-07 01:09:37 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-07 01:09:37 +08:00
|
|
|
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
|
|
|
|
}
|
2020-01-03 07:27:07 +08:00
|
|
|
|
2021-07-24 07:22:47 +08:00
|
|
|
// For now, all 3.x packages except "vue" can be published as
|
2020-01-07 01:09:37 +08:00
|
|
|
// `latest`, whereas "vue" will be published under the "next" tag.
|
2021-07-24 07:22:47 +08:00
|
|
|
let releaseTag = null
|
|
|
|
if (args.tag) {
|
|
|
|
releaseTag = args.tag
|
|
|
|
} else if (version.includes('alpha')) {
|
|
|
|
releaseTag = 'alpha'
|
|
|
|
} else if (version.includes('beta')) {
|
|
|
|
releaseTag = 'beta'
|
|
|
|
} else if (version.includes('rc')) {
|
|
|
|
releaseTag = 'rc'
|
|
|
|
} else if (pkgName === 'vue') {
|
|
|
|
// TODO remove when 3.x becomes default
|
|
|
|
releaseTag = 'next'
|
|
|
|
}
|
2020-01-07 01:09:37 +08:00
|
|
|
|
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-07 01:09:37 +08:00
|
|
|
|
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)
|
|
|
|
})
|