Unverified Commit 9314fb65 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

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...
> nextjs-project@0.0.0 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:
```
parent 455de08b
...@@ -4,6 +4,78 @@ const resolveFrom = require('resolve-from') ...@@ -4,6 +4,78 @@ const resolveFrom = require('resolve-from')
const ansiEscapes = require('ansi-escapes') const ansiEscapes = require('ansi-escapes')
const fetch = require('node-fetch') const fetch = require('node-fetch')
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)
})
}
async function main() { async function main() {
const args = process.argv const args = process.argv
const releaseType = args[args.indexOf('--release-type') + 1] const releaseType = args[args.indexOf('--release-type') + 1]
...@@ -65,6 +137,9 @@ async function main() { ...@@ -65,6 +137,9 @@ async function main() {
console.log("Releasing canary: enter 'y'\n") console.log("Releasing canary: enter 'y'\n")
child.stdin.write('y\n') child.stdin.write('y\n')
} else { } 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') { if (semverType === 'minor') {
console.log('Releasing minor: cursor down > 1\n') console.log('Releasing minor: cursor down > 1\n')
child.stdin.write(ansiEscapes.cursorDown(1)) child.stdin.write(ansiEscapes.cursorDown(1))
...@@ -78,9 +153,10 @@ async function main() { ...@@ -78,9 +153,10 @@ async function main() {
if (semverType === 'patch') { if (semverType === 'patch') {
console.log('Releasing patch: cursor stay\n') console.log('Releasing patch: cursor stay\n')
} }
console.log("Enter newline") console.log('Enter newline')
child.stdin.write('\n') child.stdin.write('\n')
console.log("Enter y") await waitForPrompt(child, 'Changes:')
console.log('Enter y')
child.stdin.write('y\n') child.stdin.write('y\n')
} }
console.log('Await child process...') console.log('Await child process...')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment