diff --git a/.github/helpers/npm-audit/run-npm-audit.cjs b/.github/helpers/npm-audit/run-npm-audit.cjs index db529fa..b827d19 100644 --- a/.github/helpers/npm-audit/run-npm-audit.cjs +++ b/.github/helpers/npm-audit/run-npm-audit.cjs @@ -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) => { @@ -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, @@ -22,8 +27,8 @@ const parseDetails = (auditData) => { cvss: v?.cvss?.score, }; }), - range: vuln.range, - fixAvailable: vuln.fixAvailable, + range: vuln?.range, + fixAvailable: vuln?.fixAvailable, }; }); @@ -31,9 +36,11 @@ const parseDetails = (auditData) => { 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, @@ -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}`), }); @@ -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; } }