Move package.json helpers into their own file

This commit is contained in:
Michael B. Gale
2026-07-14 15:33:11 +01:00
parent 9b314f4394
commit a464bf19e9
2 changed files with 46 additions and 43 deletions

View File

@@ -19,12 +19,12 @@
*/
import { execFileSync, type ExecFileSyncOptions } from "node:child_process";
import * as fs from "node:fs";
import { parseArgs } from "node:util";
import { type ApiClient, getApiClient } from "./api-client";
import * as changelog from "./changelog";
import { DryRunOption, PACKAGE_JSON, REPO_ROOT } from "./config";
import { DryRunOption, REPO_ROOT } from "./config";
import { getCurrentVersion, replaceVersionInPackageJson } from "./versions";
/**
* NB: This exact commit message is used to find commits for reverting during backports.
@@ -145,47 +145,6 @@ export function branchExistsOnRemote(branchName: string): boolean {
return result !== "";
}
/** Reads the current version from `package.json`. */
export function getCurrentVersion(): string | undefined {
const pkg: { version: string } = JSON.parse(
fs.readFileSync(PACKAGE_JSON, "utf8"),
);
return pkg.version;
}
/**
* Replaces the version in `package.json` textually. Only updates the version
* field that immediately follows the `"name": "codeql"` line.
* `npm version` doesn't always work because of merge conflicts, so we
* replace the version in package.json textually.
*/
export function replaceVersionInPackageJson(
options: DryRunOption,
prevVersion: string,
newVersion: string,
): void {
const lines = fs.readFileSync(PACKAGE_JSON, "utf8").split("\n");
let prevLineIsCodeql = false;
const output: string[] = [];
for (const line of lines) {
if (prevLineIsCodeql && line.includes(`"version": "${prevVersion}"`)) {
output.push(line.replace(prevVersion, newVersion));
} else {
output.push(line);
}
prevLineIsCodeql = line.includes('"name": "codeql",');
}
if (!options.dryRun) {
fs.writeFileSync(PACKAGE_JSON, `${output.join("\n")}\n`, "utf8");
} else {
console.info(
`[DRY RUN] Would have replaced '${prevVersion}' with '${newVersion}' in package.json`,
);
}
}
/** Represents commits returned by the GitHub API (relevant fields only). */
export interface GitHubCommit {
sha: string;

44
pr-checks/versions.ts Normal file
View File

@@ -0,0 +1,44 @@
import * as fs from "node:fs";
import { DryRunOption, PACKAGE_JSON } from "./config";
/** Reads the current version from `package.json`. */
export function getCurrentVersion(): string | undefined {
const pkg: { version: string } = JSON.parse(
fs.readFileSync(PACKAGE_JSON, "utf8"),
);
return pkg.version;
}
/**
* Replaces the version in `package.json` textually. Only updates the version
* field that immediately follows the `"name": "codeql"` line.
* `npm version` doesn't always work because of merge conflicts, so we
* replace the version in package.json textually.
*/
export function replaceVersionInPackageJson(
options: DryRunOption,
prevVersion: string,
newVersion: string,
): void {
const lines = fs.readFileSync(PACKAGE_JSON, "utf8").split("\n");
let prevLineIsCodeql = false;
const output: string[] = [];
for (const line of lines) {
if (prevLineIsCodeql && line.includes(`"version": "${prevVersion}"`)) {
output.push(line.replace(prevVersion, newVersion));
} else {
output.push(line);
}
prevLineIsCodeql = line.includes('"name": "codeql",');
}
if (!options.dryRun) {
fs.writeFileSync(PACKAGE_JSON, `${output.join("\n")}\n`, "utf8");
} else {
console.info(
`[DRY RUN] Would have replaced '${prevVersion}' with '${newVersion}' in package.json`,
);
}
}