Skip to content

Commit 10da9ac

Browse files
authored
refactor(scripts): reuse dependency report CLI helpers (#132866)
1 parent c6506e3 commit 10da9ac

2 files changed

Lines changed: 67 additions & 64 deletions

File tree

‎scripts/dependency-changes-report.mts‎

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
// Builds dependency change reports from lockfile and manifest diffs.
44
import { execFileSync } from "node:child_process";
5-
import { mkdir, readFile, writeFile } from "node:fs/promises";
5+
import { readFile } from "node:fs/promises";
66
import path from "node:path";
77
import process from "node:process";
8+
import { parseFlagArgs, stringFlag } from "./lib/arg-utils.mts";
9+
import { writeReportArtifact } from "./lib/report-cli-helpers.mts";
810
import {
911
collectAllResolvedPackagesFromLockfile,
1012
createBulkAdvisoryPayload,
@@ -251,14 +253,6 @@ function gitDiffDependencyFiles(baseRef: string, cwd: string) {
251253
});
252254
}
253255

254-
function readRequiredValue(argv: string[], index: number, flag: string) {
255-
const value = argv[index + 1];
256-
if (!value || value.startsWith("-")) {
257-
throw new Error(`${flag} requires a value`);
258-
}
259-
return value;
260-
}
261-
262256
export function parseArgs(argv: string[]) {
263257
const options = {
264258
rootDir: process.cwd(),
@@ -268,51 +262,31 @@ export function parseArgs(argv: string[]) {
268262
jsonPath: nullableString(null),
269263
markdownPath: nullableString(null),
270264
};
271-
const seen = new Set<string>();
272-
const setOnce = (flag: string, key: keyof typeof options, value: string) => {
273-
if (seen.has(flag)) {
274-
throw new Error(`${flag} was provided more than once.`);
275-
}
276-
seen.add(flag);
277-
Object.assign(options, { [key]: value });
278-
};
279-
for (let index = 0; index < argv.length; index += 1) {
280-
const arg = argv[index];
281-
if (arg === "--") {
282-
continue;
283-
}
284-
if (arg === "--root") {
285-
setOnce(arg, "rootDir", readRequiredValue(argv, index, "--root"));
286-
index += 1;
287-
continue;
288-
}
289-
if (arg === "--base-ref") {
290-
setOnce(arg, "baseRef", readRequiredValue(argv, index, "--base-ref"));
291-
index += 1;
292-
continue;
293-
}
294-
if (arg === "--base-lockfile") {
295-
setOnce(arg, "baseLockfile", readRequiredValue(argv, index, "--base-lockfile"));
296-
index += 1;
297-
continue;
298-
}
299-
if (arg === "--head-lockfile") {
300-
setOnce(arg, "headLockfile", readRequiredValue(argv, index, "--head-lockfile"));
301-
index += 1;
302-
continue;
303-
}
304-
if (arg === "--json") {
305-
setOnce(arg, "jsonPath", readRequiredValue(argv, index, "--json"));
306-
index += 1;
307-
continue;
308-
}
309-
if (arg === "--markdown") {
310-
setOnce(arg, "markdownPath", readRequiredValue(argv, index, "--markdown"));
311-
index += 1;
312-
continue;
313-
}
314-
throw new Error(`Unsupported argument: ${arg}`);
315-
}
265+
const flagEntries = [
266+
["--root", "rootDir"],
267+
["--base-ref", "baseRef"],
268+
["--base-lockfile", "baseLockfile"],
269+
["--head-lockfile", "headLockfile"],
270+
["--json", "jsonPath"],
271+
["--markdown", "markdownPath"],
272+
] satisfies Array<[string, keyof typeof options]>;
273+
parseFlagArgs(
274+
argv,
275+
options,
276+
flagEntries.map(([flag, key]) =>
277+
stringFlag<typeof options>(flag, key, {
278+
allowInline: false,
279+
missingValueMessage: `${flag} requires a value`,
280+
rejectShortOptions: true,
281+
}),
282+
),
283+
{
284+
duplicateOptionMessage: (flag) => `${flag} was provided more than once.`,
285+
onUnhandledArg(arg) {
286+
throw new Error(`Unsupported argument: ${arg}`);
287+
},
288+
},
289+
);
316290
const { baseRef, baseLockfile } = options;
317291
if (baseRef && baseLockfile) {
318292
throw new Error("Use either --base-ref or --base-lockfile, not both.");
@@ -326,14 +300,6 @@ export function parseArgs(argv: string[]) {
326300
throw new Error("Expected --base-ref <git-ref> or --base-lockfile <path>.");
327301
}
328302

329-
async function writeArtifact(filePath: string | null, content: string) {
330-
if (!filePath) {
331-
return;
332-
}
333-
await mkdir(path.dirname(filePath), { recursive: true });
334-
await writeFile(filePath, content, "utf8");
335-
}
336-
337303
/**
338304
* Generates and writes dependency change report artifacts.
339305
*/
@@ -360,8 +326,8 @@ async function runDependencyChangesReport(options: ReturnType<typeof parseArgs>)
360326
export async function main(argv = process.argv.slice(2)) {
361327
const options = parseArgs(argv);
362328
const report = await runDependencyChangesReport(options);
363-
await writeArtifact(options.jsonPath, `${JSON.stringify(report, null, 2)}\n`);
364-
await writeArtifact(options.markdownPath, renderMarkdownReport(report));
329+
await writeReportArtifact(options.jsonPath, `${JSON.stringify(report, null, 2)}\n`);
330+
await writeReportArtifact(options.markdownPath, renderMarkdownReport(report));
365331
const artifactHint =
366332
typeof options.markdownPath === "string" ? " See ".concat(options.markdownPath, ".") : "";
367333
process.stdout.write(

‎test/scripts/dependency-changes-report.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,43 @@ describe("dependency-changes-report", () => {
7878
expect(dependencyDiffPathspecs()).toContain(".github/release/vercel-cli/package-lock.json");
7979
});
8080

81+
it.each([
82+
{
83+
name: "git ref",
84+
baseArgs: ["--base-ref", "origin/main"],
85+
expectedBaseRef: "origin/main",
86+
expectedBaseLockfile: null,
87+
},
88+
{
89+
name: "lockfile",
90+
baseArgs: ["--base-lockfile", "base-lock.yaml"],
91+
expectedBaseRef: null,
92+
expectedBaseLockfile: "base-lock.yaml",
93+
},
94+
])("parses all report options with a $name base", (testCase) => {
95+
expect(
96+
parseArgs([
97+
"--root",
98+
"/repo",
99+
...testCase.baseArgs,
100+
"--head-lockfile",
101+
"head-lock.yaml",
102+
"--json",
103+
"artifacts/report.json",
104+
"--",
105+
"--markdown",
106+
"artifacts/report.md",
107+
]),
108+
).toEqual({
109+
rootDir: "/repo",
110+
baseRef: testCase.expectedBaseRef,
111+
baseLockfile: testCase.expectedBaseLockfile,
112+
headLockfile: "head-lock.yaml",
113+
jsonPath: "artifacts/report.json",
114+
markdownPath: "artifacts/report.md",
115+
});
116+
});
117+
81118
it("rejects missing report artifact path option values", () => {
82119
for (const flag of [
83120
"--root",

0 commit comments

Comments
 (0)