mirror of
https://github.com/github/codeql-action.git
synced 2026-10-03 09:14:58 +00:00
Move package.json helpers into their own file
This commit is contained in:
@@ -19,12 +19,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { execFileSync, type ExecFileSyncOptions } from "node:child_process";
|
import { execFileSync, type ExecFileSyncOptions } from "node:child_process";
|
||||||
import * as fs from "node:fs";
|
|
||||||
import { parseArgs } from "node:util";
|
import { parseArgs } from "node:util";
|
||||||
|
|
||||||
import { type ApiClient, getApiClient } from "./api-client";
|
import { type ApiClient, getApiClient } from "./api-client";
|
||||||
import * as changelog from "./changelog";
|
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.
|
* 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 !== "";
|
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). */
|
/** Represents commits returned by the GitHub API (relevant fields only). */
|
||||||
export interface GitHubCommit {
|
export interface GitHubCommit {
|
||||||
sha: string;
|
sha: string;
|
||||||
|
|||||||
44
pr-checks/versions.ts
Normal file
44
pr-checks/versions.ts
Normal 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`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user