History

fix ci

Rhys Sullivan committed 8 months ago

Commit 4bb4d2d1 parent 107a17310 files · +267 −293Browse files

Showing the changes to apps/executor (4 of 10 files). Show all

Unified · Split

4 files
  1. apps/executor/package.json +0 −2
  2. apps/executor/src/distribution/release-beta-version.test.ts +0 −19
  3. apps/executor/src/distribution/release-beta-version.ts +0 −26
  4. apps/executor/src/distribution/release-beta.ts +0 −225
apps/executor/package.json+0−2
@@ -29,8 +29,6 @@ · 28 unmodified lines
29 "test": "vitest run",29 "test": "vitest run",
30 "test:watch": "vitest",30 "test:watch": "vitest",
31 "start": "bun src/cli/main.ts",31 "start": "bun src/cli/main.ts",
32 "release:beta:dry-run": "bun src/distribution/release-beta.ts --dry-run",
33 "release:beta": "bun src/distribution/release-beta.ts",
34 "release:publish:dry-run": "bun src/distribution/publish.ts --dry-run",32 "release:publish:dry-run": "bun src/distribution/publish.ts --dry-run",
35 "release:publish": "bun src/distribution/publish.ts"33 "release:publish": "bun src/distribution/publish.ts"
36 },34 },
apps/executor/src/distribution/release-beta-version.test.ts+0−19
@@ -1,19 +0,0 @@
1import { describe, expect, it } from "vitest";
2
3import { resolveNextBetaVersion } from "./release-beta-version";
4
5describe("resolveNextBetaVersion", () => {
6 it("starts a beta prerelease from a stable version", () => {
7 expect(resolveNextBetaVersion("1.1.9")).toBe("1.1.10-beta.0");
8 });
9
10 it("increments an existing beta prerelease", () => {
11 expect(resolveNextBetaVersion("1.1.10-beta.0")).toBe("1.1.10-beta.1");
12 });
13
14 it("rejects unsupported prerelease inputs", () => {
15 expect(() => resolveNextBetaVersion("1.2.0-rc.1")).toThrow(
16 "Unsupported version for automatic beta release",
17 );
18 });
19});
apps/executor/src/distribution/release-beta-version.ts+0−26
@@ -1,26 +0,0 @@
1const stableVersionPattern = /^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)$/;
2const betaVersionPattern =
3 /^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)-beta\.(?<beta>0|[1-9]\d*)$/;
4
5export const resolveNextBetaVersion = (version: string): string => {
6 const betaMatch = version.match(betaVersionPattern);
7 if (betaMatch?.groups) {
8 const { major, minor, patch, beta } = betaMatch.groups;
9
10 return `${major}.${minor}.${patch}-beta.${Number(beta) + 1}`;
11 }
12
13 const stableMatch = version.match(stableVersionPattern);
14 if (stableMatch?.groups) {
15 const { major, minor, patch } = stableMatch.groups;
16
17 return `${major}.${minor}.${Number(patch) + 1}-beta.0`;
18 }
19
20 throw new Error(
21 [
22 `Unsupported version for automatic beta release: ${version}`,
23 "Expected a stable version like 1.2.3 or an existing beta like 1.2.4-beta.0.",
24 ].join("\n"),
25 );
26};
apps/executor/src/distribution/release-beta.ts+0−225
@@ -1,225 +0,0 @@
1import { spawn } from "node:child_process";
2import { readFile, writeFile } from "node:fs/promises";
3import { resolve } from "node:path";
4
5import { readDistributionPackageMetadata, repoRoot } from "./metadata";
6import { resolveNextBetaVersion } from "./release-beta-version";
7
8const packageJsonPath = resolve(repoRoot, "apps/executor/package.json");
9
10type ReleaseBetaCliOptions = {
11 dryRun: boolean;
12};
13
14type CommandInput = {
15 command: string;
16 args: ReadonlyArray<string>;
17 cwd: string;
18 captureOutput?: boolean;
19};
20
21type CommandOutput = {
22 stdout: string;
23 stderr: string;
24};
25
26const 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
43const 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
90const 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
109const 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
125const 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
132const 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
145const 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
164const 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
225await main();

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t