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