-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathrelease.mjs
555 lines (495 loc) · 14.3 KB
/
release.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import fs from 'node:fs/promises'
import { existsSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import minimist from 'minimist'
import chalk from 'chalk'
import semver from 'semver'
import prompts from '@posva/prompts'
import { execa } from 'execa'
import pSeries from 'p-series'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const args = minimist(process.argv.slice(2))
const {
skipBuild,
tag: optionTag = 'legacy',
dry: isDryRun,
skipCleanCheck: skipCleanGitCheck,
noDepsUpdate,
noPublish,
noLockUpdate,
} = args
if (args.h || args.help) {
console.log(
`
Usage: node release.mjs [flags]
node release.mjs [ -h | --help ]
Flags:
--skipBuild Skip building packages
--tag Publish under a given npm dist tag
--dry Dry run
--skipCleanCheck Skip checking if the git repo is clean
--noDepsUpdate Skip updating dependencies in package.json files
--noPublish Skip publishing packages
--noLockUpdate Skips updating the lock with "pnpm install"
`.trim()
)
process.exit(0)
}
// const preId =
// args.preId ||
// (semver.prerelease(currentVersion) && semver.prerelease(currentVersion)[0])
const EXPECTED_BRANCH = 'v2'
// this package will use tags like v1.0.0 while the rest will use the full package name like @pinia/[email protected]
const MAIN_PKG_NAME = 'pinia'
// whether the main package is at the root of the mono repo or this is not a mono repo
const IS_MAIN_PKG_ROOT = false
// array of folders of packages to release
const PKG_FOLDERS = [
// comment for multiline format
join(__dirname, '../packages/pinia'),
join(__dirname, '../packages/testing'),
join(__dirname, '../packages/nuxt'),
]
// files to add and commit after building a new version
const FILES_TO_COMMIT = [
// comment for multiline format
'packages/*/package.json',
'packages/*/CHANGELOG.md',
]
/**
* @type {typeof execa}
*/
const run = (bin, args, opts = {}) =>
execa(bin, args, { stdio: 'inherit', ...opts })
/**
* @param bin {string}
* @param args {string[]}
* @param opts {import('execa').Options}
*/
const dryRun = async (bin, args, opts = {}) =>
console.log(chalk.blue(`[dry-run] ${bin} ${args.join(' ')}`), opts)
const runIfNotDry = isDryRun ? dryRun : run
/**
* @param msg {string[]}
*/
const step = (...msg) => console.log(chalk.cyan(...msg))
async function main() {
if (!skipCleanGitCheck) {
const isDirtyGit = !!(
await run('git', ['status', '--porcelain'], { stdio: 'pipe' })
).stdout
if (isDirtyGit) {
console.log(chalk.red(`Git repo isn't clean.`))
return
}
const currentBranch = (
await run('git', ['branch', '--show-current'], { stdio: 'pipe' })
).stdout
if (currentBranch !== EXPECTED_BRANCH) {
console.log(
chalk.red(
`You should be on branch "${EXPECTED_BRANCH}" but are on "${currentBranch}"`
)
)
return
}
} else {
console.log(chalk.bold.white(`Skipping git checks...`))
}
if (!skipCleanGitCheck) {
const isOutdatedRE = new RegExp(
`\\W${EXPECTED_BRANCH}\\W.*(?:fast-forwardable|local out of date)`,
'i'
)
const isOutdatedGit = isOutdatedRE.test(
(await run('git', ['remote', 'show', 'origin'], { stdio: 'pipe' })).stdout
)
if (isOutdatedGit) {
console.log(chalk.red(`Git branch is not in sync with remote`))
return
}
}
const changedPackages = await getChangedPackages(...PKG_FOLDERS)
if (!changedPackages.length) {
console.log(chalk.red(`No packages have changed since last release`))
return
}
if (isDryRun) {
console.log(`\n${chalk.bold.blue('This is a dry run')}\n`)
}
let packagesToRelease = changedPackages
// if there are more than one package, ask which ones to release
if (packagesToRelease.length > 1) {
// allow to select which packages
const { pickedPackages } = await prompts({
type: 'multiselect',
name: 'pickedPackages',
message: 'What packages do you want to release?',
instructions: false,
min: 1,
choices: changedPackages.map((pkg) => ({
title: pkg.name,
value: pkg.name,
selected: true,
})),
})
// const packagesToRelease = changedPackages
packagesToRelease = changedPackages.filter((pkg) =>
pickedPackages.includes(pkg.name)
)
}
step(
`Ready to release ${packagesToRelease.map(({ name }) => chalk.bold.white(name)).join(', ')}`
)
const pkgWithVersions = await pSeries(
packagesToRelease.map(({ name, path, pkg }) => async () => {
let { version } = pkg
const prerelease = semver.prerelease(version)
const preId = prerelease && prerelease[0]
const versionIncrements = [
'patch',
'minor',
'major',
...(preId ? ['prepatch', 'preminor', 'premajor', 'prerelease'] : []),
]
const betaVersion = semver.inc(version, 'prerelease', 'beta')
const { release } = await prompts({
type: 'select',
name: 'release',
message: `Select release type for ${chalk.bold.white(name)}`,
choices: versionIncrements
.map((release) => {
const newVersion = semver.inc(version, release, preId)
return {
value: newVersion,
title: `${release}: ${name} (${newVersion})`,
}
})
.concat(
optionTag === 'beta'
? [
{
title: `beta: ${name} (${betaVersion})`,
value: betaVersion,
},
]
: []
)
.concat([{ value: 'custom', title: 'custom' }]),
})
console.log(release)
if (release === 'custom') {
version = (
await prompts({
type: 'text',
name: 'version',
message: `Input custom version (${chalk.bold.white(name)})`,
initial: version,
})
).version
} else {
version = release
}
if (!semver.valid(version)) {
throw new Error(`invalid target version: ${version}`)
}
return { name, path, version, pkg }
})
)
// put the main package first as others might depend on it
const mainPkgIndex = packagesToRelease.find(
({ name }) => name === MAIN_PKG_NAME
)
if (mainPkgIndex > 0) {
packagesToRelease.unshift(packagesToRelease.splice(mainPkgIndex, 1)[0])
}
const { yes: isReleaseConfirmed } = await prompts({
type: 'confirm',
name: 'yes',
message: `Releasing \n${pkgWithVersions
.map(
({ name, version }) =>
` · ${chalk.white(name)}: ${chalk.yellow.bold(`v${version}`)}`
)
.join('\n')}\nConfirm?`,
})
if (!isReleaseConfirmed) {
return
}
step('\nUpdating versions in package.json files...')
await updateVersions(pkgWithVersions)
if (!noLockUpdate) {
step('\nUpdating lock...')
await runIfNotDry(`pnpm`, ['install'])
}
step('\nGenerating changelogs...')
await Promise.all(
pkgWithVersions.map(async (pkg) => {
step(` -> ${pkg.name} (${pkg.path})`)
const changelogExists = existsSync(join(pkg.path, 'CHANGELOG.md'))
if (!changelogExists) {
console.log(chalk.yellow(`No CHANGELOG.md found in ${pkg.name}`))
}
await runIfNotDry(
`pnpm`,
[
'exec',
'conventional-changelog',
'-i',
'CHANGELOG.md',
'--same-file',
'-p',
'conventionalcommits',
'-r',
changelogExists ? '1' : '0',
'--commit-path',
'.',
...(pkg.name === MAIN_PKG_NAME && IS_MAIN_PKG_ROOT
? []
: ['--lerna-package', pkg.name]),
...(pkg.name === MAIN_PKG_NAME
? []
: ['--tag-prefix', `${pkg.name}@`]),
],
{ cwd: pkg.path }
)
await runIfNotDry(
`pnpm`,
['exec', 'prettier', '--write', 'CHANGELOG.md'],
{
cwd: pkg.path,
}
)
// NOTE: pnpm publish automatically copies the LICENSE file
})
)
const { yes: isChangelogCorrect } = await prompts({
type: 'confirm',
name: 'yes',
message: 'Are the changelogs correct?',
initial: true,
})
if (!isChangelogCorrect) {
return
}
step('\nBuilding all packages...')
if (!skipBuild && !isDryRun) {
await run('pnpm', ['run', 'build'])
await run('pnpm', ['run', 'build:dts'])
} else {
console.log(`(skipped)`)
}
const { stdout } = await run('git', ['diff'], { stdio: 'pipe' })
if (stdout) {
step('\nCommitting changes...')
await runIfNotDry('git', ['add', ...FILES_TO_COMMIT])
await runIfNotDry('git', [
'commit',
'-m',
`release: ${pkgWithVersions.map(({ name, version }) => `${name}@${version}`).join(' ')}`,
])
} else {
console.log('No changes to commit.')
}
step('\nCreating tags...')
const versionsToPush = []
for (const pkg of pkgWithVersions) {
const tagName =
pkg.name === MAIN_PKG_NAME
? `v${pkg.version}`
: `${pkg.name}@${pkg.version}`
versionsToPush.push(`refs/tags/${tagName}`)
await runIfNotDry('git', ['tag', `${tagName}`])
}
if (!noPublish) {
step('\nPublishing packages...')
for (const pkg of pkgWithVersions) {
await publishPackage(pkg)
}
step('\nPushing to Github...')
await runIfNotDry('git', ['push', 'origin', ...versionsToPush])
await runIfNotDry('git', ['push'])
} else {
console.log(chalk.bold.white(`Skipping publishing...`))
}
}
/**
*
* @param packageList {{ name: string; path: string; version: string, pkg: any }[]}
*/
async function updateVersions(packageList) {
return Promise.all(
packageList.map(({ pkg, version, path, name }) => {
pkg.version = version
if (!noDepsUpdate) {
updateDeps(pkg, 'dependencies', packageList)
updateDeps(pkg, 'peerDependencies', packageList)
}
const content = `${JSON.stringify(pkg, null, 2)}\n`
return isDryRun
? dryRun('write', [name], {
version: pkg.version,
dependencies: pkg.dependencies,
peerDependencies: pkg.peerDependencies,
})
: fs.writeFile(join(path, 'package.json'), content)
})
)
}
function updateDeps(pkg, depType, updatedPackages) {
const deps = pkg[depType]
if (!deps) return
step(`Updating ${chalk.bold(depType)} for ${chalk.bold.white(pkg.name)}...`)
Object.keys(deps).forEach((dep) => {
const updatedDep = updatedPackages.find((pkg) => pkg.name === dep)
// avoid updated peer deps that are external like @vue/devtools-api
if (dep && updatedDep) {
// skip any workspace reference, pnpm will handle it
if (deps[dep].startsWith('workspace:')) {
console.log(
chalk.yellow.dim(
`${pkg.name} -> ${depType} -> ${dep}@${deps[dep]} (skipped)`
)
)
} else {
console.log(
chalk.yellow(
`${pkg.name} -> ${depType} -> ${dep}@>=${updatedDep.version}`
)
)
deps[dep] = `>=${updatedDep.version}`
}
}
})
}
async function publishPackage(pkg) {
step(`Publishing ${pkg.name}...`)
try {
await runIfNotDry(
'pnpm',
[
'publish',
...(optionTag ? ['--tag', optionTag] : []),
...(skipCleanGitCheck ? ['--no-git-checks'] : []),
'--access',
'public',
// only needed for branches other than main
'--publish-branch',
EXPECTED_BRANCH,
],
{
cwd: pkg.path,
stdio: 'pipe',
}
)
console.log(
chalk.green(`Successfully published ${pkg.name}@${pkg.version}`)
)
} catch (e) {
if (e.stderr.match(/previously published/)) {
console.log(chalk.red(`Skipping already published: ${pkg.name}`))
} else {
throw e
}
}
}
/**
* Get the last tag published for a package or null if there are no tags
*
* @param {string} pkgName - package name
* @returns {string} the last tag or full commit hash
*/
async function getLastTag(pkgName) {
try {
const { stdout } = await run(
'git',
[
'describe',
'--tags',
'--abbrev=0',
'--match',
pkgName === MAIN_PKG_NAME ? 'v*' : `${pkgName}@*`,
],
{
stdio: 'pipe',
}
)
return stdout
} catch (error) {
console.log(
chalk.dim(
`Couldn't get "${chalk.bold(pkgName)}" last tag, using first commit...`
)
)
// 128 is the git exit code when there is nothing to describe
if (error.exitCode !== 128) {
console.error(error)
}
const { stdout } = await run(
'git',
['rev-list', '--max-parents=0', 'HEAD'],
{ stdio: 'pipe' }
)
return stdout
}
}
/**
* Get the packages that have changed. Based on `lerna changed` but without lerna.
*
* @param {string[]} folders
* @returns {Promise<{ name: string; path: string; pkg: any; version: string; start: string }[]} a promise of changed packages
*/
async function getChangedPackages(...folders) {
const pkgs = await Promise.all(
folders.map(async (folder) => {
if (!(await fs.lstat(folder)).isDirectory()) {
console.warn(chalk.dim(`Skipping "${folder}" as it is not a directory`))
return null
}
const pkg = JSON.parse(
await fs.readFile(join(folder, 'package.json'), 'utf-8')
)
if (pkg.private) {
console.info(chalk.dim(`Skipping "${pkg.name}" it's private`))
return null
}
const lastTag = await getLastTag(pkg.name)
const { stdout: hasChanges } = await run(
'git',
[
'diff',
lastTag,
'--',
// apparently {src,package.json} doesn't work
join(folder, 'src'),
// TODO: should not check dev deps and should compare to last tag changes
join(folder, 'package.json'),
],
{ stdio: 'pipe' }
)
if (hasChanges) {
return {
path: folder,
name: pkg.name,
version: pkg.version,
pkg,
start: lastTag,
}
} else {
console.warn(
chalk.dim(
`Skipping "${pkg.name}" as it has no changes since last release`
)
)
return null
}
})
)
return pkgs.filter((p) => p)
}
main().catch((error) => {
console.error(error)
process.exit(1)
})