forked from vuejs/vuex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.js
113 lines (91 loc) · 2.77 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
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
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const semver = require('semver')
const { prompt } = require('enquirer')
const execa = require('execa')
const currentVersion = require('../package.json').version
const versionIncrements = [
'patch',
'minor',
'major'
]
const tags = [
'latest',
'next'
]
const inc = (i) => semver.inc(currentVersion, i)
const bin = (name) => path.resolve(__dirname, `../node_modules/.bin/${name}`)
const run = (bin, args, opts = {}) => execa(bin, args, { stdio: 'inherit', ...opts })
const step = (msg) => console.log(chalk.cyan(msg))
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 { tag } = await prompt({
type: 'select',
name: 'tag',
message: 'Select tag type',
choices: tags
})
console.log(tag)
const { yes } = await prompt({
type: 'confirm',
name: 'yes',
message: `Releasing v${targetVersion} with the "${tag}" tag. Confirm?`
})
if (!yes) {
return
}
// Run tests before release.
step('\nRunning tests...')
await run('yarn', ['test'])
// Update the package version.
step('\nUpdating the package version...')
updatePackage(targetVersion)
// Build the package.
step('\nBuilding the package...')
await run('yarn', ['build'])
// Generate the changelog.
step('\nGenerating the changelog...')
await run('yarn', ['changelog'])
// Commit changes to the Git.
step('\nCommitting changes...')
await run('git', ['add', '-A'])
await run('git', ['commit', '-m', `release: v${targetVersion}`])
// Publish the package.
step('\nPublishing the package...')
await run ('yarn', [
'publish', '--tag', tag, '--new-version', targetVersion, '--no-commit-hooks',
'--no-git-tag-version'
])
// Push to GitHub.
step('\nPushing to GitHub...')
await run('git', ['tag', `v${targetVersion}`])
await run('git', ['push', 'origin', `refs/tags/v${targetVersion}`])
await run('git', ['push'])
}
function updatePackage(version) {
const pkgPath = path.resolve(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')
}
main().catch((err) => console.error(err))