build: polish release script
This commit is contained in:
parent
ff4c5c1bb6
commit
7d2ae08277
@ -1,6 +1,7 @@
|
|||||||
const args = require('minimist')(process.argv.slice(2))
|
const args = require('minimist')(process.argv.slice(2))
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
const chalk = require('chalk')
|
||||||
const semver = require('semver')
|
const semver = require('semver')
|
||||||
const currentVersion = require('../package.json').version
|
const currentVersion = require('../package.json').version
|
||||||
const { prompt } = require('enquirer')
|
const { prompt } = require('enquirer')
|
||||||
@ -14,6 +15,8 @@ const packages = fs
|
|||||||
.readdirSync(path.resolve(__dirname, '../packages'))
|
.readdirSync(path.resolve(__dirname, '../packages'))
|
||||||
.filter(p => !p.endsWith('.ts') && !p.startsWith('.'))
|
.filter(p => !p.endsWith('.ts') && !p.startsWith('.'))
|
||||||
|
|
||||||
|
const skippedPackages = ['server-renderer']
|
||||||
|
|
||||||
const versionIncrements = [
|
const versionIncrements = [
|
||||||
'patch',
|
'patch',
|
||||||
'minor',
|
'minor',
|
||||||
@ -28,7 +31,11 @@ const inc = i => semver.inc(currentVersion, i, preId)
|
|||||||
const bin = name => path.resolve(__dirname, '../node_modules/.bin/' + name)
|
const bin = name => path.resolve(__dirname, '../node_modules/.bin/' + name)
|
||||||
const run = (bin, args, opts = {}) =>
|
const run = (bin, args, opts = {}) =>
|
||||||
execa(bin, args, { stdio: 'inherit', ...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 getPkgRoot = pkg => path.resolve(__dirname, '../packages/' + pkg)
|
const getPkgRoot = pkg => path.resolve(__dirname, '../packages/' + pkg)
|
||||||
|
const step = msg => console.log(chalk.cyan(msg))
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
let targetVersion = args._[0]
|
let targetVersion = args._[0]
|
||||||
@ -69,47 +76,69 @@ async function main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// run tests before release
|
// run tests before release
|
||||||
if (!skipTests) {
|
step('\nRunning tests...')
|
||||||
|
if (!skipTests && !isDryRun) {
|
||||||
await run(bin('jest'), ['--clearCache'])
|
await run(bin('jest'), ['--clearCache'])
|
||||||
await run('yarn', ['test'])
|
await run('yarn', ['test'])
|
||||||
|
} else {
|
||||||
|
console.log(`(skipped)`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// update all package versions and inter-dependencies
|
// update all package versions and inter-dependencies
|
||||||
|
step('\nUpdating cross dependencies...')
|
||||||
updateVersions(targetVersion)
|
updateVersions(targetVersion)
|
||||||
|
|
||||||
// build all packages with types
|
// build all packages with types
|
||||||
if (!skipBuild) {
|
step('\nBuilding all packages...')
|
||||||
|
if (!skipBuild && !isDryRun) {
|
||||||
await run('yarn', ['build', '--release'])
|
await run('yarn', ['build', '--release'])
|
||||||
// test generated dts files
|
// test generated dts files
|
||||||
|
step('\nVerifying type declarations...')
|
||||||
await run(bin('tsd'))
|
await run(bin('tsd'))
|
||||||
}
|
|
||||||
|
|
||||||
// all good...
|
|
||||||
if (isDryRun) {
|
|
||||||
// stop here so we can inspect changes to be committed
|
|
||||||
// and packages built
|
|
||||||
console.log('Dry run finished.')
|
|
||||||
} else {
|
} else {
|
||||||
// commit all changes
|
console.log(`(skipped)`)
|
||||||
console.log('Committing changes...')
|
|
||||||
await run('git', ['add', '-A'])
|
|
||||||
await run('git', ['commit', '-m', `release: v${targetVersion}`])
|
|
||||||
|
|
||||||
// publish packages
|
|
||||||
const releaseTag = semver.prerelease(targetVersion)[0] || 'latest'
|
|
||||||
for (const pkg of packagesToPublish) {
|
|
||||||
await publish(pkg, releaseTag)
|
|
||||||
}
|
|
||||||
|
|
||||||
// push to GitHub
|
|
||||||
await run('git', ['tag', `v${targetVersion}`])
|
|
||||||
await run('git', ['push', 'origin', `refs/tags/v${targetVersion}`])
|
|
||||||
await run('git', ['push'])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.')
|
||||||
|
}
|
||||||
|
|
||||||
|
// publish packages
|
||||||
|
step('\nPublishing packages...')
|
||||||
|
const releaseTag = semver.prerelease(targetVersion)[0] || 'latest'
|
||||||
|
for (const pkg of packages) {
|
||||||
|
step(`Publishing ${pkg}...`)
|
||||||
|
await publishPackage(pkg, targetVersion, releaseTag, runIfNotDry)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (skippedPackages.length) {
|
||||||
|
console.log(
|
||||||
|
chalk.yellow(
|
||||||
|
`The following packages are skipped and NOT published:\n- ${skippedPackages.join(
|
||||||
|
'\n- '
|
||||||
|
)}`
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
console.log()
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateVersions(version) {
|
function updateVersions(version) {
|
||||||
console.log('Updating versions...')
|
|
||||||
// 1. update root package.json
|
// 1. update root package.json
|
||||||
updatePackage(path.resolve(__dirname, '..'), version)
|
updatePackage(path.resolve(__dirname, '..'), version)
|
||||||
// 2. update all packages
|
// 2. update all packages
|
||||||
@ -117,37 +146,64 @@ function updateVersions(version) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function updatePackage(pkgRoot, version) {
|
function updatePackage(pkgRoot, version) {
|
||||||
const pkg = readPkg(pkgRoot)
|
const pkgPath = path.resolve(pkgRoot, 'package.json')
|
||||||
|
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
|
||||||
pkg.version = version
|
pkg.version = version
|
||||||
updateDeps(pkg.dependencies)
|
updateDeps(pkg, 'dependencies', version)
|
||||||
updateDeps(pkg.peerDependencies)
|
updateDeps(pkg, 'peerDependencies', version)
|
||||||
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
|
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateDeps(deps, version) {
|
function updateDeps(pkg, depType, version) {
|
||||||
|
const deps = pkg[depType]
|
||||||
if (!deps) return
|
if (!deps) return
|
||||||
Object.keys(deps).forEach(dep => {
|
Object.keys(deps).forEach(dep => {
|
||||||
if (
|
if (
|
||||||
dep === 'vue' ||
|
dep === 'vue' ||
|
||||||
(dep.startsWith('@vue') && packages.includes(dep.replace(/^@vue\//, '')))
|
(dep.startsWith('@vue') && packages.includes(dep.replace(/^@vue\//, '')))
|
||||||
) {
|
) {
|
||||||
|
console.log(
|
||||||
|
chalk.yellow(`${pkg.name} -> ${depType} -> ${dep}@${version}`)
|
||||||
|
)
|
||||||
deps[dep] = version
|
deps[dep] = version
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function readPkg(pkgRoot) {
|
async function publishPackage(pkgName, version, releaseTag, runIfNotDry) {
|
||||||
const pkgPath = path.resolve(pkgRoot, 'package.json')
|
if (skippedPackages.includes[pkgName]) {
|
||||||
return JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
async function publish(pkgName, releaseTag) {
|
|
||||||
const pkgRoot = getPkgRoot(pkgName)
|
const pkgRoot = getPkgRoot(pkgName)
|
||||||
const pkg = readPkg(pkgRoot)
|
const pkgPath = path.resolve(pkgRoot, 'package.json')
|
||||||
if (!pkg.private) {
|
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
|
||||||
await run('npm', ['publish', '--tag', releaseTag], {
|
if (pkg.private) {
|
||||||
cwd: pkgRoot
|
return
|
||||||
})
|
}
|
||||||
|
try {
|
||||||
|
await runIfNotDry(
|
||||||
|
'yarn',
|
||||||
|
[
|
||||||
|
'publish',
|
||||||
|
'--new-version',
|
||||||
|
version,
|
||||||
|
'--tag',
|
||||||
|
releaseTag,
|
||||||
|
'--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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user