-
Notifications
You must be signed in to change notification settings - Fork 28.3k
/
Copy pathstart-release.js
168 lines (147 loc) · 4.62 KB
/
start-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
const path = require('path')
const execa = require('execa')
const resolveFrom = require('resolve-from')
const ansiEscapes = require('ansi-escapes')
function getPromptErrorDetails(rawAssertion, mostRecentChunk) {
const assertion = rawAssertion.toString().trim()
const mostRecent = (mostRecentChunk || '').trim()
return `Waiting for:\n "${assertion}"\nmost recent chunk was:\n "${mostRecent}"`
}
async function waitForPrompt(cp, rawAssertion, timeout = 3000) {
let assertion
if (typeof rawAssertion === 'string') {
assertion = (chunk) => chunk.includes(rawAssertion)
} else if (rawAssertion instanceof RegExp) {
assertion = (chunk) => rawAssertion.test(chunk)
} else {
assertion = rawAssertion
}
return new Promise((resolve, reject) => {
let mostRecentChunk = 'NO CHUNKS SO FAR'
console.log('Waiting for prompt...')
const handleTimeout = setTimeout(() => {
cleanup()
const promptErrorDetails = getPromptErrorDetails(
rawAssertion,
mostRecentChunk
)
reject(
new Error(
`Timed out after ${timeout}ms in waitForPrompt. ${promptErrorDetails}`
)
)
}, timeout)
const onComplete = () => {
cleanup()
const promptErrorDetails = getPromptErrorDetails(
rawAssertion,
mostRecentChunk
)
reject(
new Error(
`Process exited before prompt was found in waitForPrompt. ${promptErrorDetails}`
)
)
}
const onData = (rawChunk) => {
const chunk = rawChunk.toString()
mostRecentChunk = chunk
console.log('> ' + chunk)
if (assertion(chunk)) {
cleanup()
resolve()
}
}
const cleanup = () => {
cp.stdout?.off('data', onData)
cp.stderr?.off('data', onData)
cp.off('close', onComplete)
cp.off('exit', onComplete)
clearTimeout(handleTimeout)
}
cp.stdout?.on('data', onData)
cp.stderr?.on('data', onData)
cp.on('close', onComplete)
cp.on('exit', onComplete)
})
}
const SEMVER_TYPES = ['patch', 'minor', 'major']
async function main() {
const args = process.argv
const releaseType = args[args.indexOf('--release-type') + 1]
const semverType = args[args.indexOf('--semver-type') + 1]
const isCanary = releaseType !== 'stable'
if (releaseType !== 'stable' && releaseType !== 'canary') {
console.log(`Invalid release type ${releaseType}, must be stable or canary`)
return
}
if (!isCanary && !SEMVER_TYPES.includes(semverType)) {
console.log(
`Invalid semver type ${semverType}, must be one of ${SEMVER_TYPES.join(
', '
)}`
)
return
}
const githubToken = process.env.RELEASE_BOT_GITHUB_TOKEN
if (!githubToken) {
console.log(`Missing RELEASE_BOT_GITHUB_TOKEN`)
return
}
const configStorePath = resolveFrom(
path.join(process.cwd(), 'node_modules/release'),
'configstore'
)
const ConfigStore = require(configStorePath)
const config = new ConfigStore('release')
config.set('token', githubToken)
await execa(
`git remote set-url origin https://vercel-release-bot:${githubToken}@github.com/vercel/next.js.git`,
{ stdio: 'inherit', shell: true }
)
await execa(`git config user.name "vercel-release-bot"`, {
stdio: 'inherit',
shell: true,
})
await execa(`git config user.email "infra+release@vercel.com"`, {
stdio: 'inherit',
shell: true,
})
console.log(`Running pnpm release-${isCanary ? 'canary' : 'stable'}...`)
const child = execa(`pnpm release-${isCanary ? 'canary' : 'stable'}`, {
stdio: 'pipe',
shell: true,
})
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)
if (isCanary) {
console.log("Releasing canary: enter 'y'\n")
child.stdin.write('y\n')
} else {
console.log('Wait for the version prompt to show up')
await waitForPrompt(child, 'Select a new version')
console.log('Releasing stable')
if (semverType === 'minor') {
console.log('Releasing minor: cursor down > 1\n')
child.stdin.write(ansiEscapes.cursorDown(1))
}
if (semverType === 'major') {
console.log('Releasing major: curser down > 1')
child.stdin.write(ansiEscapes.cursorDown(1))
console.log('Releasing major: curser down > 2')
child.stdin.write(ansiEscapes.cursorDown(1))
}
if (semverType === 'patch') {
console.log('Releasing patch: cursor stay\n')
}
console.log('Enter newline')
child.stdin.write('\n')
await waitForPrompt(child, 'Changes:')
console.log('Enter y')
child.stdin.write('y\n')
}
console.log('Await child process...')
await child
console.log('Release process is finished')
}
main()