|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Bumps package.json + jsr.json, opens release/VERSION → main PR, enables auto-merge. |
| 4 | + * Usage: node scripts/create-release-pr.mjs [VERSION] |
| 5 | + * If VERSION is omitted, bumps the patch segment of the current package.json version. |
| 6 | + */ |
| 7 | +import { execFileSync, execSync } from "node:child_process"; |
| 8 | +import { readFileSync, writeFileSync } from "node:fs"; |
| 9 | +import { dirname, join } from "node:path"; |
| 10 | +import { fileURLToPath } from "node:url"; |
| 11 | + |
| 12 | +const repoRoot = join(dirname(fileURLToPath(import.meta.url)), ".."); |
| 13 | + |
| 14 | +function sh(cmd, inherit = false) { |
| 15 | + return execSync(cmd, { |
| 16 | + cwd: repoRoot, |
| 17 | + encoding: "utf8", |
| 18 | + stdio: inherit ? "inherit" : "pipe", |
| 19 | + }).trim(); |
| 20 | +} |
| 21 | + |
| 22 | +function gh(args, inherit = false) { |
| 23 | + if (inherit) { |
| 24 | + execFileSync("gh", args, { cwd: repoRoot, stdio: "inherit" }); |
| 25 | + return ""; |
| 26 | + } |
| 27 | + return execFileSync("gh", args, { cwd: repoRoot, encoding: "utf8" }).trim(); |
| 28 | +} |
| 29 | + |
| 30 | +function ensureCleanWorkingTree() { |
| 31 | + const out = sh("git status --porcelain"); |
| 32 | + if (out) { |
| 33 | + throw new Error( |
| 34 | + "Working tree is not clean. Commit or stash changes before running this script.", |
| 35 | + ); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function refExists(ref) { |
| 40 | + try { |
| 41 | + execSync(`git rev-parse --verify --quiet "${ref}"`, { |
| 42 | + cwd: repoRoot, |
| 43 | + stdio: "ignore", |
| 44 | + }); |
| 45 | + return true; |
| 46 | + } catch { |
| 47 | + return false; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function remoteHeadsHas(branch) { |
| 52 | + const out = sh(`git ls-remote --heads origin "${branch}"`); |
| 53 | + return out.length > 0; |
| 54 | +} |
| 55 | + |
| 56 | +function parseSemver(v) { |
| 57 | + const m = String(v).match(/^(\d+)\.(\d+)\.(\d+)$/); |
| 58 | + if (!m) { |
| 59 | + throw new Error(`VERSION must be semver X.Y.Z (got ${JSON.stringify(v)})`); |
| 60 | + } |
| 61 | + return [Number(m[1]), Number(m[2]), Number(m[3])]; |
| 62 | +} |
| 63 | + |
| 64 | +function bumpPatch(version) { |
| 65 | + const [a, b, c] = parseSemver(version); |
| 66 | + return `${a}.${b}.${c + 1}`; |
| 67 | +} |
| 68 | + |
| 69 | +function cmpVersion(a, b) { |
| 70 | + const pa = parseSemver(a); |
| 71 | + const pb = parseSemver(b); |
| 72 | + for (let i = 0; i < 3; i++) { |
| 73 | + if (pa[i] > pb[i]) return 1; |
| 74 | + if (pa[i] < pb[i]) return -1; |
| 75 | + } |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +function readJson(path) { |
| 80 | + return JSON.parse(readFileSync(path, "utf8")); |
| 81 | +} |
| 82 | + |
| 83 | +function writeJson(path, obj) { |
| 84 | + writeFileSync(path, `${JSON.stringify(obj, null, 2)}\n`, "utf8"); |
| 85 | +} |
| 86 | + |
| 87 | +function main() { |
| 88 | + process.chdir(repoRoot); |
| 89 | + |
| 90 | + const arg = process.argv[2]; |
| 91 | + const pkgPath = join(repoRoot, "package.json"); |
| 92 | + const jsrPath = join(repoRoot, "jsr.json"); |
| 93 | + |
| 94 | + ensureCleanWorkingTree(); |
| 95 | + |
| 96 | + const pkg = readJson(pkgPath); |
| 97 | + const current = pkg.version; |
| 98 | + const target = arg ? String(arg).trim() : bumpPatch(current); |
| 99 | + |
| 100 | + parseSemver(target); |
| 101 | + if (cmpVersion(target, current) <= 0) { |
| 102 | + throw new Error( |
| 103 | + `New version must be greater than current ${current} (got ${target})`, |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + sh("git fetch origin main", true); |
| 108 | + sh("git checkout main", true); |
| 109 | + sh("git pull --ff-only origin main", true); |
| 110 | + |
| 111 | + const branch = `release/${target}`; |
| 112 | + if (refExists(`refs/heads/${branch}`)) { |
| 113 | + throw new Error( |
| 114 | + `Local branch ${branch} already exists. Delete it or pick another version.`, |
| 115 | + ); |
| 116 | + } |
| 117 | + if (remoteHeadsHas(branch)) { |
| 118 | + throw new Error( |
| 119 | + `Remote branch origin/${branch} already exists. Delete it or pick another version.`, |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + sh(`git checkout -b "${branch}"`, true); |
| 124 | + |
| 125 | + pkg.version = target; |
| 126 | + writeJson(pkgPath, pkg); |
| 127 | + |
| 128 | + const jsr = readJson(jsrPath); |
| 129 | + jsr.version = target; |
| 130 | + writeJson(jsrPath, jsr); |
| 131 | + |
| 132 | + sh(`git add package.json jsr.json`, true); |
| 133 | + sh(`git commit -m "chore(release): bump version to ${target}"`, true); |
| 134 | + sh(`git push -u origin "${branch}"`, true); |
| 135 | + |
| 136 | + const body = [ |
| 137 | + "Automated release PR.", |
| 138 | + "", |
| 139 | + `- Bumps \`package.json\` and \`jsr.json\` to **${target}**`, |
| 140 | + `- Merging publishes GitHub release \`v-${target}\` and triggers npm/JSR publish (see \`.github/workflows/\`).`, |
| 141 | + ].join("\n"); |
| 142 | + |
| 143 | + const prUrl = gh([ |
| 144 | + "pr", |
| 145 | + "create", |
| 146 | + "--base", |
| 147 | + "main", |
| 148 | + "--head", |
| 149 | + branch, |
| 150 | + "--title", |
| 151 | + `Release ${target}`, |
| 152 | + "--body", |
| 153 | + body, |
| 154 | + ]); |
| 155 | + |
| 156 | + console.log(prUrl); |
| 157 | + |
| 158 | + try { |
| 159 | + gh(["pr", "merge", prUrl, "--auto", "--merge"], true); |
| 160 | + console.log("Auto-merge enabled; PR will merge when required checks pass."); |
| 161 | + } catch { |
| 162 | + console.warn( |
| 163 | + "\nCould not enable auto-merge (repo may not allow it, or checks are pending). Merge the PR manually when ready.", |
| 164 | + ); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +main(); |
0 commit comments