45 lines
1.6 KiB
JavaScript
45 lines
1.6 KiB
JavaScript
import { execSync } from "node:child_process";
|
|
|
|
const maxRetries = 3;
|
|
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
try {
|
|
const output = execSync("npm audit --audit-level=moderate --json", {
|
|
encoding: "utf8",
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
});
|
|
const report = JSON.parse(output);
|
|
const vuln = report.metadata?.vulnerabilities || {};
|
|
const total = (vuln.moderate || 0) + (vuln.high || 0) + (vuln.critical || 0);
|
|
if (total > 0) {
|
|
console.error(`npm audit found ${total} moderate/high/critical vulnerabilities.`);
|
|
process.exit(1);
|
|
}
|
|
console.log("found 0 vulnerabilities");
|
|
process.exit(0);
|
|
} catch (error) {
|
|
const stdout = error.stdout?.toString() || "";
|
|
if (stdout.includes('"vulnerabilities"')) {
|
|
try {
|
|
const report = JSON.parse(stdout);
|
|
const vuln = report.metadata?.vulnerabilities || {};
|
|
const total = (vuln.moderate || 0) + (vuln.high || 0) + (vuln.critical || 0);
|
|
if (total > 0) {
|
|
console.error(`npm audit found ${total} moderate/high/critical vulnerabilities.`);
|
|
process.exit(1);
|
|
}
|
|
console.log("found 0 vulnerabilities");
|
|
process.exit(0);
|
|
} catch {
|
|
// Fallback to retry
|
|
}
|
|
}
|
|
if (attempt < maxRetries) {
|
|
console.warn(`npm audit network error on attempt ${attempt}/${maxRetries}. Retrying...`);
|
|
execSync("node -e \"setTimeout(() => {}, 2000)\"");
|
|
continue;
|
|
}
|
|
console.error("npm audit unavailable: verification is incomplete. Retry with network access.");
|
|
process.exit(1);
|
|
}
|
|
}
|