Skip to content

Commit 7d2ae08

Browse files
committed
build: polish release script
1 parent ff4c5c1 commit 7d2ae08

File tree

1 file changed

+93
-37
lines changed

1 file changed

+93
-37
lines changed

scripts/release.js

+93-37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const args = require('minimist')(process.argv.slice(2))
22
const fs = require('fs')
33
const path = require('path')
4+
const chalk = require('chalk')
45
const semver = require('semver')
56
const currentVersion = require('../package.json').version
67
const { prompt } = require('enquirer')
@@ -14,6 +15,8 @@ const packages = fs
1415
.readdirSync(path.resolve(__dirname, '../packages'))
1516
.filter(p => !p.endsWith('.ts') && !p.startsWith('.'))
1617

18+
const skippedPackages = ['server-renderer']
19+
1720
const versionIncrements = [
1821
'patch',
1922
'minor',
@@ -28,7 +31,11 @@ const inc = i => semver.inc(currentVersion, i, preId)
2831
const bin = name => path.resolve(__dirname, '../node_modules/.bin/' + name)
2932
const run = (bin, args, opts = {}) =>
3033
execa(bin, args, { stdio: 'inherit', ...opts })
34+
const dryRun = (bin, args, opts = {}) =>
35+
console.log(chalk.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts)
36+
const runIfNotDry = isDryRun ? dryRun : run
3137
const getPkgRoot = pkg => path.resolve(__dirname, '../packages/' + pkg)
38+
const step = msg => console.log(chalk.cyan(msg))
3239

3340
async function main() {
3441
let targetVersion = args._[0]
@@ -69,85 +76,134 @@ async function main() {
6976
}
7077

7178
// run tests before release
72-
if (!skipTests) {
79+
step('\nRunning tests...')
80+
if (!skipTests && !isDryRun) {
7381
await run(bin('jest'), ['--clearCache'])
7482
await run('yarn', ['test'])
83+
} else {
84+
console.log(`(skipped)`)
7585
}
7686

7787
// update all package versions and inter-dependencies
88+
step('\nUpdating cross dependencies...')
7889
updateVersions(targetVersion)
7990

8091
// build all packages with types
81-
if (!skipBuild) {
92+
step('\nBuilding all packages...')
93+
if (!skipBuild && !isDryRun) {
8294
await run('yarn', ['build', '--release'])
8395
// test generated dts files
96+
step('\nVerifying type declarations...')
8497
await run(bin('tsd'))
98+
} else {
99+
console.log(`(skipped)`)
85100
}
86101

87-
// all good...
88-
if (isDryRun) {
89-
// stop here so we can inspect changes to be committed
90-
// and packages built
91-
console.log('Dry run finished.')
102+
const { stdout } = await run('git', ['diff'], { stdio: 'pipe' })
103+
if (stdout) {
104+
step('\nCommitting changes...')
105+
await runIfNotDry('git', ['add', '-A'])
106+
await runIfNotDry('git', ['commit', '-m', `release: v${targetVersion}`])
92107
} else {
93-
// commit all changes
94-
console.log('Committing changes...')
95-
await run('git', ['add', '-A'])
96-
await run('git', ['commit', '-m', `release: v${targetVersion}`])
97-
98-
// publish packages
99-
const releaseTag = semver.prerelease(targetVersion)[0] || 'latest'
100-
for (const pkg of packagesToPublish) {
101-
await publish(pkg, releaseTag)
102-
}
108+
console.log('No changes to commit.')
109+
}
110+
111+
// publish packages
112+
step('\nPublishing packages...')
113+
const releaseTag = semver.prerelease(targetVersion)[0] || 'latest'
114+
for (const pkg of packages) {
115+
step(`Publishing ${pkg}...`)
116+
await publishPackage(pkg, targetVersion, releaseTag, runIfNotDry)
117+
}
118+
119+
// push to GitHub
120+
step('\nPushing to GitHub...')
121+
await runIfNotDry('git', ['tag', `v${targetVersion}`])
122+
await runIfNotDry('git', ['push', 'origin', `refs/tags/v${targetVersion}`])
123+
await runIfNotDry('git', ['push'])
124+
125+
if (isDryRun) {
126+
console.log(`\nDry run finished - run git diff to see package changes.`)
127+
}
103128

104-
// push to GitHub
105-
await run('git', ['tag', `v${targetVersion}`])
106-
await run('git', ['push', 'origin', `refs/tags/v${targetVersion}`])
107-
await run('git', ['push'])
129+
if (skippedPackages.length) {
130+
console.log(
131+
chalk.yellow(
132+
`The following packages are skipped and NOT published:\n- ${skippedPackages.join(
133+
'\n- '
134+
)}`
135+
)
136+
)
108137
}
138+
console.log()
109139
}
110140

111141
function updateVersions(version) {
112-
console.log('Updating versions...')
113142
// 1. update root package.json
114143
updatePackage(path.resolve(__dirname, '..'), version)
115144
// 2. update all packages
116145
packages.forEach(p => updatePackage(getPkgRoot(p), version))
117146
}
118147

119148
function updatePackage(pkgRoot, version) {
120-
const pkg = readPkg(pkgRoot)
149+
const pkgPath = path.resolve(pkgRoot, 'package.json')
150+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
121151
pkg.version = version
122-
updateDeps(pkg.dependencies)
123-
updateDeps(pkg.peerDependencies)
152+
updateDeps(pkg, 'dependencies', version)
153+
updateDeps(pkg, 'peerDependencies', version)
124154
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
125155
}
126156

127-
function updateDeps(deps, version) {
157+
function updateDeps(pkg, depType, version) {
158+
const deps = pkg[depType]
128159
if (!deps) return
129160
Object.keys(deps).forEach(dep => {
130161
if (
131162
dep === 'vue' ||
132163
(dep.startsWith('@vue') && packages.includes(dep.replace(/^@vue\//, '')))
133164
) {
165+
console.log(
166+
chalk.yellow(`${pkg.name} -> ${depType} -> ${dep}@${version}`)
167+
)
134168
deps[dep] = version
135169
}
136170
})
137171
}
138172

139-
function readPkg(pkgRoot) {
140-
const pkgPath = path.resolve(pkgRoot, 'package.json')
141-
return JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
142-
}
143-
144-
async function publish(pkgName, releaseTag) {
173+
async function publishPackage(pkgName, version, releaseTag, runIfNotDry) {
174+
if (skippedPackages.includes[pkgName]) {
175+
return
176+
}
145177
const pkgRoot = getPkgRoot(pkgName)
146-
const pkg = readPkg(pkgRoot)
147-
if (!pkg.private) {
148-
await run('npm', ['publish', '--tag', releaseTag], {
149-
cwd: pkgRoot
150-
})
178+
const pkgPath = path.resolve(pkgRoot, 'package.json')
179+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
180+
if (pkg.private) {
181+
return
182+
}
183+
try {
184+
await runIfNotDry(
185+
'yarn',
186+
[
187+
'publish',
188+
'--new-version',
189+
version,
190+
'--tag',
191+
releaseTag,
192+
'--access',
193+
'public'
194+
],
195+
{
196+
cwd: pkgRoot,
197+
stdio: 'pipe'
198+
}
199+
)
200+
console.log(chalk.green(`Successfully published ${pkgName}@${version}`))
201+
} catch (e) {
202+
if (e.stderr.match(/previously published/)) {
203+
console.log(chalk.red(`Skipping already published: ${pkgName}`))
204+
} else {
205+
throw e
206+
}
151207
}
152208
}
153209

0 commit comments

Comments
 (0)