Skip to content
Closed
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
48 changes: 2 additions & 46 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
50 changes: 50 additions & 0 deletions src/utils/fix-runner.ts
Original file line number Diff line number Diff line change
@@ -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";
}