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 [handlebars file]', 'handlebars template file')
|
11 | .option('--no-unique', 'show all vulnerability entries');
|
12 |
|
13 | const genReport = (stdin, output = 'yarn-audit.html', template, showUnique = true) => {
|
14 | if (!stdin) {
|
15 | console.log('No JSON');
|
16 | return process.exit(1);
|
17 | }
|
18 |
|
19 | const data = stdin.split(/\n/).filter((line) => line !== '');
|
20 |
|
21 | let json;
|
22 | try {
|
23 | json = data.map(JSON.parse);
|
24 | } catch (err) {
|
25 | console.error('Failed to parse NPM Audit JSON!');
|
26 | return process.exit(1);
|
27 | }
|
28 |
|
29 | const templateFile = template || `${__dirname}/templates/template.hbs`;
|
30 |
|
31 | reporter(json, templateFile, output, showUnique)
|
32 | .then(() => {
|
33 | console.log(`Vulnerability snapshot saved at ${output}`);
|
34 | process.exit(0);
|
35 | })
|
36 | .catch((error) => {
|
37 | console.log('An error occurred!');
|
38 | console.error(error);
|
39 | process.exit(1);
|
40 | });
|
41 | };
|
42 |
|
43 | if (process.stdin.isTTY) {
|
44 | program.parse(process.argv);
|
45 | } else {
|
46 | let stdin = '';
|
47 | process.stdin.on('readable', function () {
|
48 | const chunk = this.read();
|
49 |
|
50 | if (chunk !== null) {
|
51 | stdin += chunk;
|
52 | }
|
53 | });
|
54 | process.stdin.on('end', function () {
|
55 | program.parse(process.argv);
|
56 |
|
57 | genReport(stdin, program.output, program.template, program.unique);
|
58 | });
|
59 | }
|