|
| 1 | +#!/usr/bin/env node |
| 2 | +// Bumps package.json's version, commits it, tags it, and pushes — triggering |
| 3 | +// .github/workflows/release.yml. See README.md's "Releasing" section. |
| 4 | +import { execSync } from 'node:child_process'; |
| 5 | +import { createInterface } from 'node:readline/promises'; |
| 6 | +import { stdin, stdout } from 'node:process'; |
| 7 | + |
| 8 | +function run(cmd) { |
| 9 | + execSync(cmd, { stdio: 'inherit' }); |
| 10 | +} |
| 11 | + |
| 12 | +function capture(cmd) { |
| 13 | + return execSync(cmd, { encoding: 'utf8' }).trim(); |
| 14 | +} |
| 15 | + |
| 16 | +async function prompt(question) { |
| 17 | + const rl = createInterface({ input: stdin, output: stdout }); |
| 18 | + try { |
| 19 | + return (await rl.question(question)).trim(); |
| 20 | + } finally { |
| 21 | + rl.close(); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +async function main() { |
| 26 | + const branch = capture('git rev-parse --abbrev-ref HEAD'); |
| 27 | + if (branch !== 'main') { |
| 28 | + throw new Error(`Refusing to release from branch "${branch}" — switch to main first.`); |
| 29 | + } |
| 30 | + |
| 31 | + if (capture('git status --porcelain')) { |
| 32 | + throw new Error('Working tree is not clean — commit or stash changes before releasing.'); |
| 33 | + } |
| 34 | + |
| 35 | + run('git fetch origin main --quiet'); |
| 36 | + if (capture('git rev-list HEAD..origin/main --count') !== '0') { |
| 37 | + throw new Error('Local main is behind origin/main — pull before releasing.'); |
| 38 | + } |
| 39 | + |
| 40 | + let version = process.argv[2] ?? (await prompt('Enter release version (e.g. 1.2.0): ')); |
| 41 | + version = version.replace(/^v/, '').trim(); |
| 42 | + if (!/^\d+\.\d+\.\d+(-[0-9A-Za-z-.]+)?$/.test(version)) { |
| 43 | + throw new Error(`Invalid version "${version}" — expected semver like 1.2.0`); |
| 44 | + } |
| 45 | + |
| 46 | + const tag = `v${version}`; |
| 47 | + if (capture(`git tag -l ${tag}`)) { |
| 48 | + throw new Error(`Tag ${tag} already exists locally.`); |
| 49 | + } |
| 50 | + |
| 51 | + const proceed = await prompt( |
| 52 | + `About to bump package.json to ${version}, commit, tag ${tag}, and push both to origin/main. Continue? [y/N] `, |
| 53 | + ); |
| 54 | + if (!/^y(es)?$/i.test(proceed)) { |
| 55 | + console.log('Aborted.'); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + run(`pnpm pkg set version="${version}"`); |
| 60 | + run('git add package.json'); |
| 61 | + run(`git commit -m "chore: release ${tag}"`); |
| 62 | + run(`git tag ${tag}`); |
| 63 | + run('git push origin main'); |
| 64 | + run(`git push origin ${tag}`); |
| 65 | + |
| 66 | + console.log(`Pushed ${tag} — release workflow will build, publish, and update the changelog.`); |
| 67 | +} |
| 68 | + |
| 69 | +main().catch((err) => { |
| 70 | + console.error(err.message ?? err); |
| 71 | + process.exit(1); |
| 72 | +}); |
0 commit comments