UNPKG

1.91 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const program = require('commander');
4const reporter = require('./lib/reporter');
5const pkg = require('./package.json');
6
7program
8 .version(pkg.version)
9 .option('-o, --output [output]', 'output file')
10 .option('-t, --template [ejs file]', 'ejs template file')
11 .option('--no-unique', 'show all vulnerability entries')
12 .option('--fatal-exit-code', 'exit with code 1 if vulnerabilities were found')
13 .parse();
14
15const genReport = (stdin, output = 'yarn-audit.html', template, showUnique = true, fatalExitCode = false) => {
16 if (!stdin) {
17 console.log('No JSON');
18 return process.exit(1);
19 }
20
21 const data = stdin.split(/\n/).filter((line) => line !== '');
22
23 let json;
24 try {
25 json = data.map(JSON.parse);
26 } catch (err) {
27 console.error('Failed to parse NPM Audit JSON!\n', err);
28 return process.exit(1);
29 }
30
31 const templateFile = template || `${__dirname}/templates/template.ejs`;
32
33 reporter(json, templateFile, output, showUnique)
34 .then((modifiedData) => {
35 if (modifiedData.summary.vulnerabilities > 0) {
36 console.log(`Vulnerability snapshot saved at ${output}`);
37 if (fatalExitCode) {
38 process.exit(1);
39 }
40 process.exit(0);
41 }
42
43 console.log('No vulnerabilities found.');
44 process.exit(0);
45 })
46 .catch((error) => {
47 console.log('An error occurred!');
48 console.error(error);
49 process.exit(1);
50 });
51};
52
53const options = program.opts();
54
55let stdin = '';
56process.stdin.on('readable', function () {
57 const chunk = this.read();
58
59 if (chunk !== null) {
60 stdin += chunk;
61 }
62});
63process.stdin.on('end', function () {
64 genReport(stdin, options.output, options.template, options.unique, options.fatalExitCode);
65});