|
| 1 | +/** |
| 2 | + * Release workflow CLI. Run a single step with `bun run scripts/release/cli.ts <command>`: |
| 3 | + * |
| 4 | + * compute-version Resolve the version from git tags + workflow inputs and export |
| 5 | + * RELEASE_VERSION / RELEASE_TAG / PREVIOUS_TAG to later job steps. |
| 6 | + * set-version Write RELEASE_VERSION into package.json before publish. |
| 7 | + * publish Publish the current package to npm (idempotent; skips if the |
| 8 | + * version already exists). In CI it authenticates via npm OIDC |
| 9 | + * trusted publishing (no token) and adds --provenance. |
| 10 | + * create-github-release Create the git tag + GitHub release at GITHUB_SHA (idempotent). |
| 11 | + * |
| 12 | + * Inputs are read from the environment so the same script works as plain CI steps: |
| 13 | + * compute-version ← EXPECTED_VERSION, VERSION_BUMP |
| 14 | + * set-version ← RELEASE_VERSION |
| 15 | + * publish ← RELEASE_VERSION, GITHUB_ACTIONS (auth via OIDC trusted publishing in CI) |
| 16 | + * create-release ← RELEASE_TAG, PREVIOUS_TAG, GITHUB_SHA, GH_TOKEN |
| 17 | + */ |
| 18 | + |
| 19 | +import process from "node:process"; |
| 20 | +import { fileURLToPath } from "node:url"; |
| 21 | + |
| 22 | +import { |
| 23 | + listGitTags, |
| 24 | + readPackageName, |
| 25 | + readPackageVersion, |
| 26 | + readRequiredEnv, |
| 27 | + runCapture, |
| 28 | + runInherit, |
| 29 | + setPackageVersion, |
| 30 | + writeGitHubEnv, |
| 31 | +} from "./lib"; |
| 32 | +import { |
| 33 | + computeReleaseVersion, |
| 34 | + isStableSemver, |
| 35 | + readVersionBump, |
| 36 | +} from "./version"; |
| 37 | + |
| 38 | +// Run everything from the repo root regardless of the caller's cwd, so git/npm/gh and the |
| 39 | +// relative package.json path all resolve correctly. |
| 40 | +const repoRoot = fileURLToPath(new URL("../../", import.meta.url)); |
| 41 | +process.chdir(repoRoot); |
| 42 | +const packageJsonPath = "package.json"; |
| 43 | + |
| 44 | +async function runComputeVersion(): Promise<void> { |
| 45 | + const baseVersion = await readPackageVersion(packageJsonPath); |
| 46 | + const result = computeReleaseVersion({ |
| 47 | + expectedVersion: process.env.EXPECTED_VERSION ?? "", |
| 48 | + versionBump: readVersionBump(process.env.VERSION_BUMP ?? "patch"), |
| 49 | + tags: listGitTags(), |
| 50 | + baseVersion: isStableSemver(baseVersion) ? baseVersion : "0.0.0", |
| 51 | + }); |
| 52 | + |
| 53 | + await writeGitHubEnv({ |
| 54 | + RELEASE_VERSION: result.version, |
| 55 | + RELEASE_TAG: result.tagName, |
| 56 | + PREVIOUS_TAG: result.previousTag, |
| 57 | + }); |
| 58 | + process.stdout.write( |
| 59 | + `Release version ${result.version} (tag ${result.tagName}, previous ${result.previousTag || "none"}).\n`, |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +async function runSetVersion(): Promise<void> { |
| 64 | + const version = readRequiredEnv("RELEASE_VERSION"); |
| 65 | + await setPackageVersion(packageJsonPath, version); |
| 66 | + process.stdout.write(`Set package.json version to ${version}.\n`); |
| 67 | +} |
| 68 | + |
| 69 | +function npmVersionExists(packageSpec: string): boolean { |
| 70 | + const result = runCapture("npm", ["view", packageSpec, "version", "--json"]); |
| 71 | + if (result.status === 0) { |
| 72 | + return result.stdout.trim() !== ""; |
| 73 | + } |
| 74 | + |
| 75 | + const output = `${result.stdout}\n${result.stderr}`.toLowerCase(); |
| 76 | + if (output.includes("e404") || output.includes("no match found")) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + throw new Error(`Failed to query npm for ${packageSpec}:\n${(result.stderr || result.stdout).trim()}`); |
| 80 | +} |
| 81 | + |
| 82 | +async function runPublish(): Promise<void> { |
| 83 | + const version = readRequiredEnv("RELEASE_VERSION"); |
| 84 | + const name = await readPackageName(packageJsonPath); |
| 85 | + const packageSpec = `${name}@${version}`; |
| 86 | + |
| 87 | + if (npmVersionExists(packageSpec)) { |
| 88 | + process.stdout.write(`Skipping publish: ${packageSpec} already exists on npm.\n`); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const args = ["publish", "--access", "public"]; |
| 93 | + // In CI npm authenticates via OIDC trusted publishing; provenance rides on the same |
| 94 | + // id-token (id-token: write). Neither is available locally, so add --provenance only here. |
| 95 | + if (process.env.GITHUB_ACTIONS === "true") { |
| 96 | + args.push("--provenance"); |
| 97 | + } |
| 98 | + runInherit("npm", args); |
| 99 | + process.stdout.write(`Published ${packageSpec}.\n`); |
| 100 | +} |
| 101 | + |
| 102 | +function gitHubReleaseExists(tag: string): boolean { |
| 103 | + return runCapture("gh", ["release", "view", tag, "--json", "tagName"]).status === 0; |
| 104 | +} |
| 105 | + |
| 106 | +function runCreateGitHubRelease(): void { |
| 107 | + const tag = readRequiredEnv("RELEASE_TAG"); |
| 108 | + |
| 109 | + if (gitHubReleaseExists(tag)) { |
| 110 | + process.stdout.write(`Skipping GitHub release: ${tag} already exists.\n`); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + const previousTag = process.env.PREVIOUS_TAG ?? ""; |
| 115 | + const args = [ |
| 116 | + "release", |
| 117 | + "create", |
| 118 | + tag, |
| 119 | + "--target", |
| 120 | + readRequiredEnv("GITHUB_SHA"), |
| 121 | + "--title", |
| 122 | + tag, |
| 123 | + "--generate-notes", |
| 124 | + ]; |
| 125 | + if (previousTag !== "") { |
| 126 | + args.push("--notes-start-tag", previousTag); |
| 127 | + } |
| 128 | + args.push("--latest"); |
| 129 | + |
| 130 | + runInherit("gh", args); |
| 131 | + process.stdout.write(`Created GitHub release ${tag}.\n`); |
| 132 | +} |
| 133 | + |
| 134 | +async function main(): Promise<void> { |
| 135 | + const command = process.argv[2]; |
| 136 | + switch (command) { |
| 137 | + case "compute-version": |
| 138 | + await runComputeVersion(); |
| 139 | + return; |
| 140 | + case "set-version": |
| 141 | + await runSetVersion(); |
| 142 | + return; |
| 143 | + case "publish": |
| 144 | + await runPublish(); |
| 145 | + return; |
| 146 | + case "create-github-release": |
| 147 | + runCreateGitHubRelease(); |
| 148 | + return; |
| 149 | + default: |
| 150 | + throw new Error( |
| 151 | + `Unknown command: ${command ?? "(none)"}. ` |
| 152 | + + "Expected one of: compute-version, set-version, publish, create-github-release.", |
| 153 | + ); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +await main(); |
0 commit comments