Open History to see the last commit
1import { appendFileSync, readFileSync, writeFileSync } from "node:fs";
2import { resolve } from "node:path";
3import { fileURLToPath } from "node:url";
4
5export const releasePackageFiles = [
6 "apps/server/package.json",
7 "apps/desktop/package.json",
8 "apps/web/package.json",
9 "packages/contracts/package.json",
10] as const;
11
12interface UpdateReleasePackageVersionsOptions {
13 readonly rootDir?: string;
14}
15
16interface MutablePackageJson {
17 version?: string;
18 [key: string]: unknown;
19}
20
21export function updateReleasePackageVersions(
22 version: string,
23 options: UpdateReleasePackageVersionsOptions = {},
24): { changed: boolean } {
25 const rootDir = resolve(options.rootDir ?? process.cwd());
26 let changed = false;
27
28 for (const relativePath of releasePackageFiles) {
29 const filePath = resolve(rootDir, relativePath);
30 const packageJson = JSON.parse(readFileSync(filePath, "utf8")) as MutablePackageJson;
31 if (packageJson.version === version) {
32 continue;
33 }
34
35 packageJson.version = version;
36 writeFileSync(filePath, `${JSON.stringify(packageJson, null, 2)}\n`);
37 changed = true;
38 }
39
40 return { changed };
41}
42
43function parseArgs(argv: ReadonlyArray<string>): {
44 version: string;
45 rootDir: string | undefined;
46 writeGithubOutput: boolean;
47} {
48 let version: string | undefined;
49 let rootDir: string | undefined;
50 let writeGithubOutput = false;
51
52 for (let index = 0; index < argv.length; index += 1) {
53 const argument = argv[index];
54 if (argument === undefined) {
55 continue;
56 }
57
58 if (argument === "--github-output") {
59 writeGithubOutput = true;
60 continue;
61 }
62
63 if (argument === "--root") {
64 rootDir = argv[index + 1];
65 if (!rootDir) {
66 throw new Error("Missing value for --root.");
67 }
68 index += 1;
69 continue;
70 }
71
72 if (argument.startsWith("--")) {
73 throw new Error(`Unknown argument: ${argument}`);
74 }
75
76 if (version !== undefined) {
77 throw new Error("Only one release version can be provided.");
78 }
79 version = argument;
80 }
81
82 if (!version) {
83 throw new Error(
84 "Usage: node scripts/update-release-package-versions.ts <version> [--root <path>] [--github-output]",
85 );
86 }
87
88 return { version, rootDir, writeGithubOutput };
89}
90
91const isMain =
92 process.argv[1] !== undefined && resolve(process.argv[1]) === fileURLToPath(import.meta.url);
93
94if (isMain) {
95 const { version, rootDir, writeGithubOutput } = parseArgs(process.argv.slice(2));
96 const { changed } = updateReleasePackageVersions(
97 version,
98 rootDir === undefined ? {} : { rootDir },
99 );
100
101 if (!changed) {
102 console.log("All package.json versions already match release version.");
103 }
104
105 if (writeGithubOutput) {
106 const githubOutputPath = process.env.GITHUB_OUTPUT;
107 if (!githubOutputPath) {
108 throw new Error("GITHUB_OUTPUT is required when --github-output is set.");
109 }
110 appendFileSync(githubOutputPath, `changed=${changed}\n`);
111 }
112}