2023-03-24 00:28:49 +01:00
|
|
|
const path = require('path')
|
|
|
|
const execa = require('execa')
|
|
|
|
const resolveFrom = require('resolve-from')
|
|
|
|
const ansiEscapes = require('ansi-escapes')
|
2023-04-01 04:00:12 +02:00
|
|
|
const fetch = require('node-fetch')
|
2023-03-24 00:28:49 +01:00
|
|
|
|
2023-04-21 23:58:24 +02:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-24 00:28:49 +01:00
|
|
|
async function main() {
|
|
|
|
const args = process.argv
|
|
|
|
const releaseType = args[args.indexOf('--release-type') + 1]
|
|
|
|
const semverType = args[args.indexOf('--semver-type') + 1]
|
2023-03-24 06:30:08 +01:00
|
|
|
const isCanary = releaseType !== 'stable'
|
2023-03-24 00:28:49 +01:00
|
|
|
|
|
|
|
if (releaseType !== 'stable' && releaseType !== 'canary') {
|
|
|
|
console.log(`Invalid release type ${releaseType}, must be stable or canary`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!isCanary && !['patch', 'minor', 'stable'].includes(semverType)) {
|
|
|
|
console.log(
|
|
|
|
`Invalid semver type ${semverType}, must be one of ${semverType.join(
|
|
|
|
', '
|
|
|
|
)}`
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-23 16:29:58 +02:00
|
|
|
const githubToken = process.env.RELEASE_BOT_GITHUB_TOKEN
|
2023-03-24 00:28:49 +01:00
|
|
|
|
|
|
|
if (!githubToken) {
|
2023-05-23 16:29:58 +02:00
|
|
|
console.log(`Missing RELEASE_BOT_GITHUB_TOKEN`)
|
2023-03-24 00:28:49 +01:00
|
|
|
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(
|
2023-04-20 17:58:36 +02:00
|
|
|
`git remote set-url origin https://vercel-release-bot:${githubToken}@github.com/vercel/next.js.git`,
|
2023-03-24 00:28:49 +01:00
|
|
|
{ stdio: 'inherit', shell: true }
|
|
|
|
)
|
2023-04-20 17:58:36 +02:00
|
|
|
await execa(`git config user.name "vercel-release-bot"`, {
|
2023-03-24 00:28:49 +01:00
|
|
|
stdio: 'inherit',
|
|
|
|
shell: true,
|
|
|
|
})
|
2023-04-20 17:58:36 +02:00
|
|
|
await execa(`git config user.email "infra+release@vercel.com"`, {
|
2023-03-24 00:28:49 +01:00
|
|
|
stdio: 'inherit',
|
|
|
|
shell: true,
|
|
|
|
})
|
|
|
|
|
2023-04-21 22:54:12 +02:00
|
|
|
console.log(`Running pnpm release-${isCanary ? 'canary' : 'stable'}...`)
|
2023-03-25 20:59:51 +01:00
|
|
|
const child = execa(`pnpm release-${isCanary ? 'canary' : 'stable'}`, {
|
2023-03-24 00:28:49 +01:00
|
|
|
stdio: 'pipe',
|
|
|
|
shell: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
child.stdout.pipe(process.stdout)
|
|
|
|
child.stderr.pipe(process.stderr)
|
|
|
|
|
|
|
|
if (isCanary) {
|
2023-04-21 22:54:12 +02:00
|
|
|
console.log("Releasing canary: enter 'y'\n")
|
2023-03-24 00:28:49 +01:00
|
|
|
child.stdin.write('y\n')
|
|
|
|
} else {
|
2023-04-21 23:58:24 +02:00
|
|
|
console.log('Wait for the version prompt to show up')
|
|
|
|
await waitForPrompt(child, 'Select a new version')
|
|
|
|
console.log('Releasing stable')
|
2023-03-24 00:28:49 +01:00
|
|
|
if (semverType === 'minor') {
|
2023-04-21 22:54:12 +02:00
|
|
|
console.log('Releasing minor: cursor down > 1\n')
|
2023-03-24 00:28:49 +01:00
|
|
|
child.stdin.write(ansiEscapes.cursorDown(1))
|
|
|
|
}
|
|
|
|
if (semverType === 'major') {
|
2023-04-21 22:54:12 +02:00
|
|
|
console.log('Releasing major: curser down > 1')
|
2023-03-24 00:28:49 +01:00
|
|
|
child.stdin.write(ansiEscapes.cursorDown(1))
|
2023-04-21 22:54:12 +02:00
|
|
|
console.log('Releasing major: curser down > 2')
|
2023-03-24 00:28:49 +01:00
|
|
|
child.stdin.write(ansiEscapes.cursorDown(1))
|
|
|
|
}
|
2023-04-21 22:54:12 +02:00
|
|
|
if (semverType === 'patch') {
|
|
|
|
console.log('Releasing patch: cursor stay\n')
|
|
|
|
}
|
2023-04-21 23:58:24 +02:00
|
|
|
console.log('Enter newline')
|
2023-03-24 00:28:49 +01:00
|
|
|
child.stdin.write('\n')
|
2023-04-21 23:58:24 +02:00
|
|
|
await waitForPrompt(child, 'Changes:')
|
|
|
|
console.log('Enter y')
|
2023-04-06 23:52:56 +02:00
|
|
|
child.stdin.write('y\n')
|
2023-03-24 00:28:49 +01:00
|
|
|
}
|
2023-04-21 22:54:12 +02:00
|
|
|
console.log('Await child process...')
|
2023-03-24 00:28:49 +01:00
|
|
|
await child
|
2023-04-21 22:54:12 +02:00
|
|
|
console.log('Release process is finished')
|
2023-04-01 04:00:12 +02:00
|
|
|
|
|
|
|
if (isCanary) {
|
|
|
|
try {
|
|
|
|
const ghHeaders = {
|
|
|
|
Accept: 'application/vnd.github+json',
|
|
|
|
Authorization: `Bearer ${githubToken}`,
|
|
|
|
'X-GitHub-Api-Version': '2022-11-28',
|
|
|
|
}
|
2023-04-01 06:06:51 +02:00
|
|
|
let { version } = require('../lerna.json')
|
|
|
|
version = `v${version}`
|
|
|
|
|
2023-04-02 20:37:08 +02:00
|
|
|
let release
|
|
|
|
let releasesData
|
|
|
|
|
|
|
|
// The release might take a minute to show up in
|
|
|
|
// the list so retry a bit
|
|
|
|
for (let i = 0; i < 6; i++) {
|
|
|
|
try {
|
|
|
|
const releaseUrlRes = await fetch(
|
|
|
|
`https://api.github.com/repos/vercel/next.js/releases`,
|
|
|
|
{
|
|
|
|
headers: ghHeaders,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
releasesData = await releaseUrlRes.json()
|
|
|
|
|
|
|
|
release = releasesData.find((release) => release.tag_name === version)
|
|
|
|
} catch (err) {
|
|
|
|
console.log(`Fetching release failed`, err)
|
2023-04-01 04:00:12 +02:00
|
|
|
}
|
2023-04-02 20:37:08 +02:00
|
|
|
if (!release) {
|
|
|
|
console.log(`Retrying in 10s...`)
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 10 * 1000))
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 06:06:51 +02:00
|
|
|
|
|
|
|
if (!release) {
|
|
|
|
console.log(`Failed to find release`, releasesData)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const undraftRes = await fetch(release.url, {
|
2023-04-01 04:00:12 +02:00
|
|
|
headers: ghHeaders,
|
|
|
|
method: 'PATCH',
|
|
|
|
body: JSON.stringify({
|
|
|
|
draft: false,
|
|
|
|
name: version,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
if (undraftRes.ok) {
|
2023-04-01 06:06:51 +02:00
|
|
|
console.log('un-drafted canary release successfully')
|
2023-04-01 04:00:12 +02:00
|
|
|
} else {
|
|
|
|
console.log(`Failed to undraft`, await undraftRes.text())
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`Failed to undraft release`, err)
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 00:28:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main()
|