Skip to content

Commit a464bf1

Browse files
committed
Move package.json helpers into their own file
1 parent 9b314f4 commit a464bf1

2 files changed

Lines changed: 46 additions & 43 deletions

File tree

‎pr-checks/update-release-branch.ts‎

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
*/
2020

2121
import { execFileSync, type ExecFileSyncOptions } from "node:child_process";
22-
import * as fs from "node:fs";
2322
import { parseArgs } from "node:util";
2423

2524
import { type ApiClient, getApiClient } from "./api-client";
2625
import * as changelog from "./changelog";
27-
import { DryRunOption, PACKAGE_JSON, REPO_ROOT } from "./config";
26+
import { DryRunOption, REPO_ROOT } from "./config";
27+
import { getCurrentVersion, replaceVersionInPackageJson } from "./versions";
2828

2929
/**
3030
* NB: This exact commit message is used to find commits for reverting during backports.
@@ -145,47 +145,6 @@ export function branchExistsOnRemote(branchName: string): boolean {
145145
return result !== "";
146146
}
147147

148-
/** Reads the current version from `package.json`. */
149-
export function getCurrentVersion(): string | undefined {
150-
const pkg: { version: string } = JSON.parse(
151-
fs.readFileSync(PACKAGE_JSON, "utf8"),
152-
);
153-
return pkg.version;
154-
}
155-
156-
/**
157-
* Replaces the version in `package.json` textually. Only updates the version
158-
* field that immediately follows the `"name": "codeql"` line.
159-
* `npm version` doesn't always work because of merge conflicts, so we
160-
* replace the version in package.json textually.
161-
*/
162-
export function replaceVersionInPackageJson(
163-
options: DryRunOption,
164-
prevVersion: string,
165-
newVersion: string,
166-
): void {
167-
const lines = fs.readFileSync(PACKAGE_JSON, "utf8").split("\n");
168-
let prevLineIsCodeql = false;
169-
const output: string[] = [];
170-
171-
for (const line of lines) {
172-
if (prevLineIsCodeql && line.includes(`"version": "${prevVersion}"`)) {
173-
output.push(line.replace(prevVersion, newVersion));
174-
} else {
175-
output.push(line);
176-
}
177-
prevLineIsCodeql = line.includes('"name": "codeql",');
178-
}
179-
180-
if (!options.dryRun) {
181-
fs.writeFileSync(PACKAGE_JSON, `${output.join("\n")}\n`, "utf8");
182-
} else {
183-
console.info(
184-
`[DRY RUN] Would have replaced '${prevVersion}' with '${newVersion}' in package.json`,
185-
);
186-
}
187-
}
188-
189148
/** Represents commits returned by the GitHub API (relevant fields only). */
190149
export interface GitHubCommit {
191150
sha: string;

‎pr-checks/versions.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as fs from "node:fs";
2+
3+
import { DryRunOption, PACKAGE_JSON } from "./config";
4+
5+
/** Reads the current version from `package.json`. */
6+
export function getCurrentVersion(): string | undefined {
7+
const pkg: { version: string } = JSON.parse(
8+
fs.readFileSync(PACKAGE_JSON, "utf8"),
9+
);
10+
return pkg.version;
11+
}
12+
13+
/**
14+
* Replaces the version in `package.json` textually. Only updates the version
15+
* field that immediately follows the `"name": "codeql"` line.
16+
* `npm version` doesn't always work because of merge conflicts, so we
17+
* replace the version in package.json textually.
18+
*/
19+
export function replaceVersionInPackageJson(
20+
options: DryRunOption,
21+
prevVersion: string,
22+
newVersion: string,
23+
): void {
24+
const lines = fs.readFileSync(PACKAGE_JSON, "utf8").split("\n");
25+
let prevLineIsCodeql = false;
26+
const output: string[] = [];
27+
28+
for (const line of lines) {
29+
if (prevLineIsCodeql && line.includes(`"version": "${prevVersion}"`)) {
30+
output.push(line.replace(prevVersion, newVersion));
31+
} else {
32+
output.push(line);
33+
}
34+
prevLineIsCodeql = line.includes('"name": "codeql",');
35+
}
36+
37+
if (!options.dryRun) {
38+
fs.writeFileSync(PACKAGE_JSON, `${output.join("\n")}\n`, "utf8");
39+
} else {
40+
console.info(
41+
`[DRY RUN] Would have replaced '${prevVersion}' with '${newVersion}' in package.json`,
42+
);
43+
}
44+
}

0 commit comments

Comments
 (0)