From 4ef98b6c4618807387506de3666a17bedaa3c7fc Mon Sep 17 00:00:00 2001 From: Kushaal Kankane Date: Sun, 24 May 2026 23:51:33 +0530 Subject: [PATCH 1/2] refactor: move fix command utilities to separate module --- src/index.ts | 56 ++++++----------------------------------- src/utils/fix-runner.ts | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 49 deletions(-) create mode 100644 src/utils/fix-runner.ts diff --git a/src/index.ts b/src/index.ts index 08048d3..6f8c35d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,4 @@ #!/usr/bin/env node -import { spawn } from "node:child_process"; import fs from "node:fs"; import path from "node:path"; import process from "node:process"; @@ -27,9 +26,9 @@ import { sslCertificateErrorHint, } from "./utils/network.js"; import { formatAdvisoryDbFreshness } from "./utils/time.js"; -import type { SuggestedFixCommandPlan, SuggestedFixTarget } from "./remediation/fix-commands.js"; +import type { SuggestedFixCommandPlan } from "./remediation/fix-commands.js"; import type { ParsedOptions } from "./types.js"; -import type { Finding, SeverityLabel } from "./types.js"; +import type { SeverityLabel } from "./types.js"; import { formatAdvisorySourceLine, logInfo, @@ -55,6 +54,11 @@ import { installSkill } from "./skills/install.js"; import { readDirectDependencyNames } from "./utils/package-json.js" +import { + buildFixCommandParts, + runInstallCommand, + commandLabelForPackageManager +} from "./utils/fix-runner.js"; let parsedArgs: ReturnType | null = null; try { @@ -442,52 +446,6 @@ async function applyFixesIfRequested(params: { }; } -function buildFixCommandParts( - packageManager: SuggestedFixCommandPlan["packageManager"], - targets: SuggestedFixTarget[], -): string[] { - if (packageManager === "npm") { - return ["npm", "install", ...targets.map(target => `${target.package}@${target.targetVersion}`)]; - } - if (packageManager === "pnpm") { - return ["pnpm", "add", ...targets.map(target => `${target.package}@${target.targetVersion}`)]; - } - if (packageManager === "bun") { - return ["bun", "add", ...targets.map(target => `${target.package}@${target.targetVersion}`)]; - } - return ["yarn", "add", ...targets.map(target => `${target.package}@${target.targetVersion}`)]; -} - -async function runInstallCommand( - command: string, - args: string[], - cwd: string, -): Promise<{ status: number | null; error: Error | null }> { - return await new Promise(resolve => { - const child = spawn(command, args, { - cwd, - stdio: ["ignore", "pipe", "pipe"], - }); - - child.stdout.on("data", () => {}); - child.stderr.on("data", () => {}); - - child.on("error", error => { - resolve({ status: null, error }); - }); - child.on("close", code => { - resolve({ status: code, error: null }); - }); - }); -} - -function commandLabelForPackageManager(packageManager: SuggestedFixCommandPlan["packageManager"]): string { - if (packageManager === "npm") return "npm install"; - if (packageManager === "pnpm") return "pnpm add"; - if (packageManager === "bun") return "bun add"; - return "yarn add"; -} - type FixExecutionResult = { appliedFixCount: number; skippedCount: number; diff --git a/src/utils/fix-runner.ts b/src/utils/fix-runner.ts new file mode 100644 index 0000000..4ad5bef --- /dev/null +++ b/src/utils/fix-runner.ts @@ -0,0 +1,48 @@ +import { spawn } from "node:child_process"; +import type { SuggestedFixCommandPlan, SuggestedFixTarget } from "../remediation/fix-commands.js"; + +export function buildFixCommandParts( + packageManager: SuggestedFixCommandPlan["packageManager"], + targets: SuggestedFixTarget[], +): string[] { + if (packageManager === "npm") { + return ["npm", "install", ...targets.map(target => `${target.package}@${target.targetVersion}`)]; + } + if (packageManager === "pnpm") { + return ["pnpm", "add", ...targets.map(target => `${target.package}@${target.targetVersion}`)]; + } + if (packageManager === "bun") { + return ["bun", "add", ...targets.map(target => `${target.package}@${target.targetVersion}`)]; + } + return ["yarn", "add", ...targets.map(target => `${target.package}@${target.targetVersion}`)]; +} + +export async function runInstallCommand( + command: string, + args: string[], + cwd: string, +): Promise<{ status: number | null; error: Error | null }> { + return await new Promise(resolve => { + const child = spawn(command, args, { + cwd, + stdio: ["ignore", "pipe", "pipe"], + }); + + child.stdout.on("data", () => {}); + child.stderr.on("data", () => {}); + + child.on("error", error => { + resolve({ status: null, error }); + }); + child.on("close", code => { + resolve({ status: code, error: null }); + }); + }); +} + +export function commandLabelForPackageManager(packageManager: SuggestedFixCommandPlan["packageManager"]): string { + if (packageManager === "npm") return "npm install"; + if (packageManager === "pnpm") return "pnpm add"; + if (packageManager === "bun") return "bun add"; + return "yarn add"; +} \ No newline at end of file From f7bfe77d3d3525a15afd55d80d62d87a4888dd41 Mon Sep 17 00:00:00 2001 From: Kushaal Kankane Date: Mon, 25 May 2026 21:03:45 +0530 Subject: [PATCH 2/2] fix: add newline at end of file in fix-runner.ts --- src/utils/fix-runner.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/fix-runner.ts b/src/utils/fix-runner.ts index 4ad5bef..16ccf0d 100644 --- a/src/utils/fix-runner.ts +++ b/src/utils/fix-runner.ts @@ -45,4 +45,4 @@ export function commandLabelForPackageManager(packageManager: SuggestedFixComman if (packageManager === "pnpm") return "pnpm add"; if (packageManager === "bun") return "bun add"; return "yarn add"; -} \ No newline at end of file +}