From 445126b8852428529147baace7ab1b617e8f9bbc Mon Sep 17 00:00:00 2001 From: AnupamKumar-1 Date: Sun, 24 May 2026 23:11:52 +0530 Subject: [PATCH] refactor: extract fix command utilities to src/utils/fix-runner.ts Closes #437 --- src/index.ts | 48 ++------------------------------------- src/utils/fix-runner.ts | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 46 deletions(-) create mode 100644 src/utils/fix-runner.ts diff --git a/src/index.ts b/src/index.ts index 178bb33..7a422b9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ #!/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"; @@ -21,6 +21,7 @@ import { formatAdvisoryDbFreshness } from "./utils/time.js"; import type { SuggestedFixCommandPlan, SuggestedFixTarget } from "./remediation/fix-commands.js"; import type { ParsedOptions } from "./types.js"; import type { Finding, SeverityLabel } from "./types.js"; +import { buildFixCommandParts, runInstallCommand, commandLabelForPackageManager } from "./utils/fix-runner.js"; import { formatAdvisorySourceLine, logInfo, @@ -425,51 +426,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; diff --git a/src/utils/fix-runner.ts b/src/utils/fix-runner.ts new file mode 100644 index 0000000..79efb44 --- /dev/null +++ b/src/utils/fix-runner.ts @@ -0,0 +1,50 @@ +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