| @@ -1,225 +0,0 @@ |
| 1 | | −import { spawn } from "node:child_process"; |
| 2 | | −import { readFile, writeFile } from "node:fs/promises"; |
| 3 | | −import { resolve } from "node:path"; |
| 4 | | − |
| 5 | | −import { readDistributionPackageMetadata, repoRoot } from "./metadata"; |
| 6 | | −import { resolveNextBetaVersion } from "./release-beta-version"; |
| 7 | | − |
| 8 | | −const packageJsonPath = resolve(repoRoot, "apps/executor/package.json"); |
| 9 | | − |
| 10 | | −type ReleaseBetaCliOptions = { |
| 11 | | − dryRun: boolean; |
| 12 | | −}; |
| 13 | | − |
| 14 | | −type CommandInput = { |
| 15 | | − command: string; |
| 16 | | − args: ReadonlyArray<string>; |
| 17 | | − cwd: string; |
| 18 | | − captureOutput?: boolean; |
| 19 | | −}; |
| 20 | | − |
| 21 | | −type CommandOutput = { |
| 22 | | − stdout: string; |
| 23 | | − stderr: string; |
| 24 | | −}; |
| 25 | | − |
| 26 | | −const parseArgs = (argv: ReadonlyArray<string>): ReleaseBetaCliOptions => { |
| 27 | | − const options: ReleaseBetaCliOptions = { |
| 28 | | − dryRun: false, |
| 29 | | − }; |
| 30 | | − |
| 31 | | − for (const arg of argv) { |
| 32 | | − if (arg === "--dry-run") { |
| 33 | | − options.dryRun = true; |
| 34 | | − continue; |
| 35 | | − } |
| 36 | | − |
| 37 | | − throw new Error(`Unknown argument: ${arg}`); |
| 38 | | − } |
| 39 | | − |
| 40 | | − return options; |
| 41 | | −}; |
| 42 | | − |
| 43 | | −const runCommand = async (input: CommandInput): Promise<CommandOutput> => { |
| 44 | | − const child = spawn(input.command, [...input.args], { |
| 45 | | − cwd: input.cwd, |
| 46 | | − env: process.env, |
| 47 | | − stdio: input.captureOutput ? ["ignore", "pipe", "pipe"] : "inherit", |
| 48 | | − }); |
| 49 | | − |
| 50 | | − let stdout = ""; |
| 51 | | − let stderr = ""; |
| 52 | | − |
| 53 | | − if (input.captureOutput) { |
| 54 | | − child.stdout?.setEncoding("utf8"); |
| 55 | | − child.stdout?.on("data", (chunk) => { |
| 56 | | − stdout += chunk; |
| 57 | | − }); |
| 58 | | − |
| 59 | | − child.stderr?.setEncoding("utf8"); |
| 60 | | − child.stderr?.on("data", (chunk) => { |
| 61 | | − stderr += chunk; |
| 62 | | − }); |
| 63 | | − } |
| 64 | | − |
| 65 | | − const exitCode = await new Promise<number>((resolveExitCode, reject) => { |
| 66 | | − child.once("error", reject); |
| 67 | | − child.once("close", (code) => { |
| 68 | | − resolveExitCode(code ?? -1); |
| 69 | | − }); |
| 70 | | − }); |
| 71 | | − |
| 72 | | − if (exitCode !== 0) { |
| 73 | | − throw new Error( |
| 74 | | − [ |
| 75 | | − `${input.command} ${input.args.join(" ")} exited with code ${exitCode}`, |
| 76 | | − stdout.trim().length > 0 ? `stdout:\n${stdout.trim()}` : null, |
| 77 | | − stderr.trim().length > 0 ? `stderr:\n${stderr.trim()}` : null, |
| 78 | | − ] |
| 79 | | − .filter((part) => part !== null) |
| 80 | | − .join("\n\n"), |
| 81 | | − ); |
| 82 | | − } |
| 83 | | − |
| 84 | | − return { |
| 85 | | − stdout: stdout.trim(), |
| 86 | | − stderr: stderr.trim(), |
| 87 | | − }; |
| 88 | | −}; |
| 89 | | − |
| 90 | | −const assertVersionFileIsUnchanged = async (): Promise<void> => { |
| 91 | | − const output = await runCommand({ |
| 92 | | − command: "git", |
| 93 | | − args: ["status", "--short", "--", "apps/executor/package.json"], |
| 94 | | − cwd: repoRoot, |
| 95 | | − captureOutput: true, |
| 96 | | − }); |
| 97 | | − |
| 98 | | − if (output.stdout.length > 0) { |
| 99 | | − throw new Error( |
| 100 | | − [ |
| 101 | | − "Refusing to publish a beta with existing changes to apps/executor/package.json.", |
| 102 | | − "Commit, stash, or discard that file first.", |
| 103 | | − output.stdout, |
| 104 | | − ].join("\n"), |
| 105 | | − ); |
| 106 | | − } |
| 107 | | −}; |
| 108 | | − |
| 109 | | −const getCurrentBranch = async (): Promise<string> => { |
| 110 | | − const output = await runCommand({ |
| 111 | | − command: "git", |
| 112 | | − args: ["branch", "--show-current"], |
| 113 | | − cwd: repoRoot, |
| 114 | | − captureOutput: true, |
| 115 | | − }); |
| 116 | | − |
| 117 | | − const branch = output.stdout.trim(); |
| 118 | | − if (!branch) { |
| 119 | | − throw new Error("Unable to determine the current git branch."); |
| 120 | | − } |
| 121 | | − |
| 122 | | − return branch; |
| 123 | | −}; |
| 124 | | − |
| 125 | | −const updatePackageVersion = async (nextVersion: string): Promise<void> => { |
| 126 | | − const contents = await readFile(packageJsonPath, "utf8"); |
| 127 | | − const packageJson = JSON.parse(contents) as { version?: string }; |
| 128 | | − packageJson.version = nextVersion; |
| 129 | | − await writeFile(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`); |
| 130 | | −}; |
| 131 | | − |
| 132 | | −const assertTagDoesNotExist = async (tag: string): Promise<void> => { |
| 133 | | − const output = await runCommand({ |
| 134 | | − command: "git", |
| 135 | | − args: ["tag", "--list", tag], |
| 136 | | − cwd: repoRoot, |
| 137 | | − captureOutput: true, |
| 138 | | − }); |
| 139 | | − |
| 140 | | − if (output.stdout.trim() === tag) { |
| 141 | | − throw new Error(`Refusing to publish beta because git tag ${tag} already exists.`); |
| 142 | | − } |
| 143 | | −}; |
| 144 | | − |
| 145 | | −const printPlan = (input: { |
| 146 | | − currentVersion: string; |
| 147 | | − nextVersion: string; |
| 148 | | − branch: string; |
| 149 | | −}): void => { |
| 150 | | − const tag = `v${input.nextVersion}`; |
| 151 | | − |
| 152 | | − process.stdout.write(`Current version: ${input.currentVersion}\n`); |
| 153 | | − process.stdout.write(`Next beta: ${input.nextVersion}\n`); |
| 154 | | − process.stdout.write(`Branch: ${input.branch}\n`); |
| 155 | | − process.stdout.write("Planned actions:\n"); |
| 156 | | − process.stdout.write("- Update apps/executor/package.json\n"); |
| 157 | | − process.stdout.write("- Build publish artifact with release:publish:dry-run\n"); |
| 158 | | − process.stdout.write(`- Commit with message: release: ${input.nextVersion}\n`); |
| 159 | | − process.stdout.write(`- Push branch: ${input.branch}\n`); |
| 160 | | − process.stdout.write(`- Create tag: ${tag}\n`); |
| 161 | | − process.stdout.write(`- Push tag: ${tag}\n`); |
| 162 | | −}; |
| 163 | | − |
| 164 | | −const main = async () => { |
| 165 | | − const options = parseArgs(process.argv.slice(2)); |
| 166 | | − const branch = await getCurrentBranch(); |
| 167 | | − const currentVersion = (await readDistributionPackageMetadata()).version; |
| 168 | | − const nextVersion = resolveNextBetaVersion(currentVersion); |
| 169 | | − const tag = `v${nextVersion}`; |
| 170 | | − |
| 171 | | − if (options.dryRun) { |
| 172 | | − printPlan({ |
| 173 | | − currentVersion, |
| 174 | | − nextVersion, |
| 175 | | − branch, |
| 176 | | − }); |
| 177 | | − return; |
| 178 | | − } |
| 179 | | − |
| 180 | | − await assertVersionFileIsUnchanged(); |
| 181 | | − await assertTagDoesNotExist(tag); |
| 182 | | − |
| 183 | | − await updatePackageVersion(nextVersion); |
| 184 | | − process.stdout.write(`Updated apps/executor/package.json to ${nextVersion}\n`); |
| 185 | | − |
| 186 | | − await runCommand({ |
| 187 | | − command: "bun", |
| 188 | | − args: ["run", "--cwd", "apps/executor", "release:publish:dry-run"], |
| 189 | | − cwd: repoRoot, |
| 190 | | − }); |
| 191 | | − |
| 192 | | − await runCommand({ |
| 193 | | − command: "git", |
| 194 | | − args: ["add", "apps/executor/package.json"], |
| 195 | | − cwd: repoRoot, |
| 196 | | − }); |
| 197 | | − |
| 198 | | − await runCommand({ |
| 199 | | − command: "git", |
| 200 | | − args: ["commit", "-m", `release: ${nextVersion}`], |
| 201 | | − cwd: repoRoot, |
| 202 | | − }); |
| 203 | | − |
| 204 | | − await runCommand({ |
| 205 | | − command: "git", |
| 206 | | − args: ["push", "origin", branch], |
| 207 | | − cwd: repoRoot, |
| 208 | | − }); |
| 209 | | − |
| 210 | | − await runCommand({ |
| 211 | | − command: "git", |
| 212 | | − args: ["tag", tag], |
| 213 | | − cwd: repoRoot, |
| 214 | | − }); |
| 215 | | − |
| 216 | | − await runCommand({ |
| 217 | | − command: "git", |
| 218 | | − args: ["push", "origin", tag], |
| 219 | | − cwd: repoRoot, |
| 220 | | − }); |
| 221 | | − |
| 222 | | − process.stdout.write(`Published beta release trigger for ${nextVersion}\n`); |
| 223 | | −}; |
| 224 | | − |
| 225 | | −await main(); |