diff --git a/pr-checks/update-release-branch.ts b/pr-checks/update-release-branch.ts index 501f9596f..d6cc7670a 100755 --- a/pr-checks/update-release-branch.ts +++ b/pr-checks/update-release-branch.ts @@ -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; diff --git a/pr-checks/versions.ts b/pr-checks/versions.ts new file mode 100644 index 000000000..359868350 --- /dev/null +++ b/pr-checks/versions.ts @@ -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`, + ); + } +}