Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 7 additions & 49 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -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<typeof parseArgs> | null = null;
try {
Expand Down Expand Up @@ -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;
Expand Down
48 changes: 48 additions & 0 deletions src/utils/fix-runner.ts
Original file line number Diff line number Diff line change
@@ -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";
}