22
33// Builds dependency change reports from lockfile and manifest diffs.
44import { execFileSync } from "node:child_process" ;
5- import { mkdir , readFile , writeFile } from "node:fs/promises" ;
5+ import { readFile } from "node:fs/promises" ;
66import path from "node:path" ;
77import process from "node:process" ;
8+ import { parseFlagArgs , stringFlag } from "./lib/arg-utils.mts" ;
9+ import { writeReportArtifact } from "./lib/report-cli-helpers.mts" ;
810import {
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-
262256export 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>)
360326export 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 (
0 commit comments