|
19 | 19 | */ |
20 | 20 |
|
21 | 21 | import { execFileSync, type ExecFileSyncOptions } from "node:child_process"; |
22 | | -import * as fs from "node:fs"; |
23 | 22 | import { parseArgs } from "node:util"; |
24 | 23 |
|
25 | 24 | import { type ApiClient, getApiClient } from "./api-client"; |
26 | 25 | 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"; |
28 | 28 |
|
29 | 29 | /** |
30 | 30 | * NB: This exact commit message is used to find commits for reverting during backports. |
@@ -145,47 +145,6 @@ export function branchExistsOnRemote(branchName: string): boolean { |
145 | 145 | return result !== ""; |
146 | 146 | } |
147 | 147 |
|
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 | | - |
189 | 148 | /** Represents commits returned by the GitHub API (relevant fields only). */ |
190 | 149 | export interface GitHubCommit { |
191 | 150 | sha: string; |
|
0 commit comments