Skip to content

Instantly share code, notes, and snippets.

@lobbin
Created December 8, 2022 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lobbin/9f74661b3a2b80fe76e24dff40519bae to your computer and use it in GitHub Desktop.
Save lobbin/9f74661b3a2b80fe76e24dff40519bae to your computer and use it in GitHub Desktop.
Mimic npm 6 --parseable with npm 8 json output
// npm audit --json | node formatJson.js
const fs = require('fs');
function findDependencyPath(json, moduleName) {
if (!json.vulnerabilities[moduleName].isDirect
&& json.vulnerabilities[moduleName].effects.length) {
return `${findDependencyPath(json, json.vulnerabilities[moduleName].effects[0])}>${moduleName}`
}
return moduleName;
}
function main() {
const jsonData = fs.readFileSync(0);
const json = JSON.parse(jsonData);
for (const vulnerabilityName of Object.keys(json.vulnerabilities)) {
const vulnerability = json.vulnerabilities[vulnerabilityName];
for (const via of vulnerability.via) {
if (typeof via === 'object') {
const dependencyPath = findDependencyPath(json, vulnerabilityName);
console.log(
`review\t${vulnerabilityName}\t${via.severity}\t${via.range}\t${via.title}\t${via.url}\t${dependencyPath}`
);
}
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment