Created
December 8, 2022 12:24
-
-
Save lobbin/9f74661b3a2b80fe76e24dff40519bae to your computer and use it in GitHub Desktop.
Mimic npm 6 --parseable with npm 8 json output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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