Skip to content

Commit 9314fb6

Browse files
authored
Wait for versioning prompt to show up for stable release (#48692)
Wait for the prompt to show up then enter the cursor change or "y" for prompt to avoid write to child process too early Example ``` Running pnpm release-stable... Releasing patch: cursor stay Enter newline Enter y Await child process... > [email protected] release-stable /home/runner/work/next.js/next.js > lerna version --force-publish lerna notice cli v4.0.0 lerna info ci enabled lerna info current version 13.3.1-canary.19 lerna WARN force-publish all packages lerna info Assuming all packages changed ? Select a new version (currently 13.3.1-canary.19) (Use arrow keys) ❯ Patch (13.3.1) Minor (13.4.0) Major (14.0.0) Prepatch (13.3.2-canary.0) Preminor (13.4.0-canary.0) Premajor (14.0.0-canary.0) Custom Prerelease Custom Version ? Select a new version (currently 13.3.1-canary.19) Patch (13.3.1) Changes: ```
1 parent 455de08 commit 9314fb6

File tree

1 file changed

+78
-2
lines changed

1 file changed

+78
-2
lines changed

scripts/start-release.js

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,78 @@ const resolveFrom = require('resolve-from')
44
const ansiEscapes = require('ansi-escapes')
55
const fetch = require('node-fetch')
66

7+
function getPromptErrorDetails(rawAssertion, mostRecentChunk) {
8+
const assertion = rawAssertion.toString().trim()
9+
const mostRecent = (mostRecentChunk || '').trim()
10+
return `Waiting for:\n "${assertion}"\nmost recent chunk was:\n "${mostRecent}"`
11+
}
12+
13+
async function waitForPrompt(cp, rawAssertion, timeout = 3000) {
14+
let assertion
15+
if (typeof rawAssertion === 'string') {
16+
assertion = (chunk) => chunk.includes(rawAssertion)
17+
} else if (rawAssertion instanceof RegExp) {
18+
assertion = (chunk) => rawAssertion.test(chunk)
19+
} else {
20+
assertion = rawAssertion
21+
}
22+
23+
return new Promise((resolve, reject) => {
24+
let mostRecentChunk = 'NO CHUNKS SO FAR'
25+
26+
console.log('Waiting for prompt...')
27+
const handleTimeout = setTimeout(() => {
28+
cleanup()
29+
const promptErrorDetails = getPromptErrorDetails(
30+
rawAssertion,
31+
mostRecentChunk
32+
)
33+
reject(
34+
new Error(
35+
`Timed out after ${timeout}ms in waitForPrompt. ${promptErrorDetails}`
36+
)
37+
)
38+
}, timeout)
39+
40+
const onComplete = () => {
41+
cleanup()
42+
const promptErrorDetails = getPromptErrorDetails(
43+
rawAssertion,
44+
mostRecentChunk
45+
)
46+
reject(
47+
new Error(
48+
`Process exited before prompt was found in waitForPrompt. ${promptErrorDetails}`
49+
)
50+
)
51+
}
52+
53+
const onData = (rawChunk) => {
54+
const chunk = rawChunk.toString()
55+
56+
mostRecentChunk = chunk
57+
console.log('> ' + chunk)
58+
if (assertion(chunk)) {
59+
cleanup()
60+
resolve()
61+
}
62+
}
63+
64+
const cleanup = () => {
65+
cp.stdout?.off('data', onData)
66+
cp.stderr?.off('data', onData)
67+
cp.off('close', onComplete)
68+
cp.off('exit', onComplete)
69+
clearTimeout(handleTimeout)
70+
}
71+
72+
cp.stdout?.on('data', onData)
73+
cp.stderr?.on('data', onData)
74+
cp.on('close', onComplete)
75+
cp.on('exit', onComplete)
76+
})
77+
}
78+
779
async function main() {
880
const args = process.argv
981
const releaseType = args[args.indexOf('--release-type') + 1]
@@ -65,6 +137,9 @@ async function main() {
65137
console.log("Releasing canary: enter 'y'\n")
66138
child.stdin.write('y\n')
67139
} else {
140+
console.log('Wait for the version prompt to show up')
141+
await waitForPrompt(child, 'Select a new version')
142+
console.log('Releasing stable')
68143
if (semverType === 'minor') {
69144
console.log('Releasing minor: cursor down > 1\n')
70145
child.stdin.write(ansiEscapes.cursorDown(1))
@@ -78,9 +153,10 @@ async function main() {
78153
if (semverType === 'patch') {
79154
console.log('Releasing patch: cursor stay\n')
80155
}
81-
console.log("Enter newline")
156+
console.log('Enter newline')
82157
child.stdin.write('\n')
83-
console.log("Enter y")
158+
await waitForPrompt(child, 'Changes:')
159+
console.log('Enter y')
84160
child.stdin.write('y\n')
85161
}
86162
console.log('Await child process...')

0 commit comments

Comments
 (0)