Files
t3code/scripts/update-release-package-versions.ts
Jason LasterandJulius Marminge 223c3dc800 chore(ci): add and apply fmt:check script (#779)
* Add  formatting conventions

* Add a fmt lint check

* rely on defaults

---------

Co-authored-by: Julius Marminge <[email protected]>
2026-03-10 00:17:18 -07:00

113 lines
2.9 KiB
TypeScript

import { appendFileSync, readFileSync, writeFileSync } from "node:fs";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
export const releasePackageFiles = [
"apps/server/package.json",
"apps/desktop/package.json",
"apps/web/package.json",
"packages/contracts/package.json",
] as const;
interface UpdateReleasePackageVersionsOptions {
readonly rootDir?: string;
}
interface MutablePackageJson {
version?: string;
[key: string]: unknown;
}
export function updateReleasePackageVersions(
version: string,
options: UpdateReleasePackageVersionsOptions = {},
): { changed: boolean } {
const rootDir = resolve(options.rootDir ?? process.cwd());
let changed = false;
for (const relativePath of releasePackageFiles) {
const filePath = resolve(rootDir, relativePath);
const packageJson = JSON.parse(readFileSync(filePath, "utf8")) as MutablePackageJson;
if (packageJson.version === version) {
continue;
}
packageJson.version = version;
writeFileSync(filePath, `${JSON.stringify(packageJson, null, 2)}\n`);
changed = true;
}
return { changed };
}
function parseArgs(argv: ReadonlyArray<string>): {
version: string;
rootDir: string | undefined;
writeGithubOutput: boolean;
} {
let version: string | undefined;
let rootDir: string | undefined;
let writeGithubOutput = false;
for (let index = 0; index < argv.length; index += 1) {
const argument = argv[index];
if (argument === undefined) {
continue;
}
if (argument === "--github-output") {
writeGithubOutput = true;
continue;
}
if (argument === "--root") {
rootDir = argv[index + 1];
if (!rootDir) {
throw new Error("Missing value for --root.");
}
index += 1;
continue;
}
if (argument.startsWith("--")) {
throw new Error(`Unknown argument: ${argument}`);
}
if (version !== undefined) {
throw new Error("Only one release version can be provided.");
}
version = argument;
}
if (!version) {
throw new Error(
"Usage: node scripts/update-release-package-versions.ts <version> [--root <path>] [--github-output]",
);
}
return { version, rootDir, writeGithubOutput };
}
const isMain =
process.argv[1] !== undefined && resolve(process.argv[1]) === fileURLToPath(import.meta.url);
if (isMain) {
const { version, rootDir, writeGithubOutput } = parseArgs(process.argv.slice(2));
const { changed } = updateReleasePackageVersions(
version,
rootDir === undefined ? {} : { rootDir },
);
if (!changed) {
console.log("All package.json versions already match release version.");
}
if (writeGithubOutput) {
const githubOutputPath = process.env.GITHUB_OUTPUT;
if (!githubOutputPath) {
throw new Error("GITHUB_OUTPUT is required when --github-output is set.");
}
appendFileSync(githubOutputPath, `changed=${changed}\n`);
}
}