Skip to content

Commit

Permalink
Update run-npm-audit.cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyMitch authored Jul 12, 2024
1 parent e64b6d4 commit ba7d5c7
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions .github/helpers/npm-audit/run-npm-audit.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const { execSync } = require('child_process');
const path = require('path');
const path = require("path");
const { execSync } = require("child_process");

const parseDetails = (auditData) => {
if (!auditData.vulnerabilities) {
return { vulnerabilities: [], metadata: { vulnerabilities: 0 }, highestSeverity: 'none' };
return {
vulnerabilities: [],
metadata: { vulnerabilities: 0 },
highestSeverity: "none",
};
}

const vulnerabilities = Object.keys(auditData.vulnerabilities).map((key) => {
Expand All @@ -13,6 +17,7 @@ const parseDetails = (auditData) => {
severity: vuln.severity,
isDirect: vuln.isDirect,
via: vuln.via.map((v) => {
if (typeof v === "string") return v;
return {
title: v?.title,
severity: v?.severity,
Expand All @@ -22,18 +27,20 @@ const parseDetails = (auditData) => {
cvss: v?.cvss?.score,
};
}),
range: vuln.range,
fixAvailable: vuln.fixAvailable,
range: vuln?.range,
fixAvailable: vuln?.fixAvailable,
};
});

const highestSeverity =
vulnerabilities.length === 0
? null
: vulnerabilities.reduce((max, vuln) => {
const severities = ['low', 'moderate', 'high', 'critical'];
return severities.indexOf(vuln.severity) > severities.indexOf(max) ? vuln.severity : max;
}, 'low');
const severities = ["low", "moderate", "high", "critical"];
return severities.indexOf(vuln.severity) > severities.indexOf(max)
? vuln.severity
: max;
}, "low");

return {
vulnerabilities,
Expand All @@ -47,10 +54,9 @@ const parseDetails = (auditData) => {
// Runs 'npm audit --json' command and returns a modified output.
const runNpmAudit = async (directoryPath) => {
try {
execSync('npm i', { cwd: path.resolve(__dirname, `../../../${directoryPath}`) });
const stdout = execSync('npm audit --json', {
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'ignore'],
const stdout = execSync("npm audit --json", {
encoding: "utf-8",
stdio: ["pipe", "pipe", "ignore"],
cwd: path.resolve(__dirname, `../../../${directoryPath}`),
});

Expand All @@ -63,11 +69,11 @@ const runNpmAudit = async (directoryPath) => {

return parseDetails(auditData);
} catch (parseError) {
console.error('JSON parse error:', parseError);
console.error("JSON parse error:", parseError);
throw parseError;
}
} else {
console.error('Error running npm audit:', error);
console.error("Error running npm audit:", error);
throw error;
}
}
Expand Down

0 comments on commit ba7d5c7

Please sign in to comment.