-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.js
85 lines (73 loc) · 2.19 KB
/
release.js
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
const { prompt } = require('enquirer')
const chalk = require('chalk')
const execa = require('execa')
const semver = require('semver')
const path = require('path')
const fs = require('fs')
const versionIncrements = ['patch', 'minor', 'major']
const currentVersion = require('./package.json').version
const inc = (i) => semver.inc(currentVersion, i)
const step = (msg) => console.log(chalk.cyan(msg))
const run = (bin, args, opts = {}) =>
execa(bin, args, { stdio: 'inherit', ...opts })
function updateVersions(version) {
const pkgPath = path.resolve(__dirname, '.', 'package.json')
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
pkg.version = version
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
}
async function main() {
let targetVersion = ''
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
}
step('\nUpdating Version...')
updateVersions(targetVersion)
step('\nPublishing packages...')
await run('npm', ['publish', '--registry=https://registry.npmjs.org/'])
step('\nGenerate changelog...')
await run(`yarn`, ['changelog'])
step('\nPushing to GitHub...')
const { stdout } = await run('git', ['diff'], { stdio: 'pipe' })
if (stdout) {
step('\nCommitting changes...')
await run('git', ['add', '-A'])
await run('git', ['commit', '-m', `release: v${targetVersion}`])
await run('git', ['push'])
} else {
console.log('No changes to commit.')
}
}
main()
.then(() => {
step('\nRelease success')
})
.catch(() => {
step('\nRelease error')
})