forked from vitejs/vite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.js
219 lines (191 loc) · 4.84 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
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
// @ts-check
/**
* modified from https://github.com/vuejs/vue-next/blob/master/scripts/release.js
*/
const execa = require('execa')
const path = require('path')
const fs = require('fs')
const args = require('minimist')(process.argv.slice(2))
const semver = require('semver')
const chalk = require('chalk')
const prompts = require('prompts')
const pkgDir = process.cwd()
const pkgPath = path.resolve(pkgDir, 'package.json')
/**
* @type {{ name: string, version: string }}
*/
const pkg = require(pkgPath)
const pkgName = pkg.name.replace(/^@vitejs\//, '')
const currentVersion = pkg.version
/**
* @type {boolean}
*/
const isDryRun = args.dry
/**
* @type {boolean}
*/
const skipBuild = args.skipBuild
/**
* @type {import('semver').ReleaseType[]}
*/
const versionIncrements = [
'patch',
'minor',
'major',
'prepatch',
'preminor',
'premajor',
'prerelease'
]
/**
* @param {import('semver').ReleaseType} i
*/
const inc = (i) => semver.inc(currentVersion, i, 'beta')
/**
* @param {string} bin
* @param {string[]} args
* @param {object} opts
*/
const run = (bin, args, opts = {}) =>
execa(bin, args, { stdio: 'inherit', ...opts })
/**
* @param {string} bin
* @param {string[]} args
* @param {object} opts
*/
const dryRun = (bin, args, opts = {}) =>
console.log(chalk.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts)
const runIfNotDry = isDryRun ? dryRun : run
/**
* @param {string} msg
*/
const step = (msg) => console.log(chalk.cyan(msg))
async function main() {
let targetVersion = args._[0]
if (!targetVersion) {
// no explicit version, offer suggestions
/**
* @type {{ release: string }}
*/
const { release } = await prompts({
type: 'select',
name: 'release',
message: 'Select release type',
choices: versionIncrements
.map((i) => `${i} (${inc(i)})`)
.concat(['custom'])
.map((i) => ({ value: i, title: i }))
})
if (release === 'custom') {
/**
* @type {{ version: string }}
*/
const res = await prompts({
type: 'text',
name: 'version',
message: 'Input custom version',
initial: currentVersion
})
targetVersion = res.version
} else {
targetVersion = release.match(/\((.*)\)/)[1]
}
}
if (!semver.valid(targetVersion)) {
throw new Error(`invalid target version: ${targetVersion}`)
}
const tag =
pkgName === 'vite' ? `v${targetVersion}` : `${pkgName}@${targetVersion}`
if (targetVersion.includes('beta') && !args.tag) {
/**
* @type {{ tagBeta: boolean }}
*/
const { tagBeta } = await prompts({
type: 'confirm',
name: 'tagBeta',
message: `Publish under dist-tag "beta"?`
})
if (tagBeta) args.tag = 'beta'
}
/**
* @type {{ yes: boolean }}
*/
const { yes } = await prompts({
type: 'confirm',
name: 'yes',
message: `Releasing ${tag}. Confirm?`
})
if (!yes) {
return
}
step('\nUpdating package version...')
updateVersion(targetVersion)
step('\nBuilding package...')
if (!skipBuild && !isDryRun) {
await run('pnpm', ['run', 'build'])
} else {
console.log(`(skipped)`)
}
step('\nGenerating changelog...')
await run('pnpm', ['run', 'changelog'])
const { stdout } = await run('git', ['diff'], { stdio: 'pipe' })
if (stdout) {
step('\nCommitting changes...')
await runIfNotDry('git', ['add', '-A'])
await runIfNotDry('git', ['commit', '-m', `release: ${tag}`])
await runIfNotDry('git', ['tag', tag])
} else {
console.log('No changes to commit.')
}
step('\nPublishing package...')
await publishPackage(targetVersion, runIfNotDry)
step('\nPushing to GitHub...')
await runIfNotDry('git', ['push', 'origin', `refs/tags/${tag}`])
await runIfNotDry('git', ['push'])
if (isDryRun) {
console.log(`\nDry run finished - run git diff to see package changes.`)
}
console.log()
}
/**
* @param {string} version
*/
function updateVersion(version) {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
pkg.version = version
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
}
/**
* @param {string} version
* @param {Function} runIfNotDry
*/
async function publishPackage(version, runIfNotDry) {
const publicArgs = [
'publish',
'--no-git-tag-version',
'--new-version',
version,
'--access',
'public'
]
if (args.tag) {
publicArgs.push(`--tag`, args.tag)
}
try {
// important: we still use Yarn 1 to publish since we rely on its specific
// behavior
await runIfNotDry('yarn', publicArgs, {
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
}
}
}
main().catch((err) => {
console.error(err)
})