forked from eventOneHQ/npm-audit-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (49 loc) · 1.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env node
const program = require('commander')
const updateNotifier = require('update-notifier')
const reporter = require('./lib/reporter')
const pkg = require('./package.json')
updateNotifier({ pkg }).notify()
program
.version(pkg.version)
.option('-o, --output [output]', 'output file')
.option('-t, --template [handlebars file]', 'handlebars template file')
const genReport = (stdin, output = 'npm-audit.html', template) => {
if (!stdin) {
console.log('No JSON')
return process.exit(1)
}
let json
try {
json = JSON.parse(stdin)
} catch (err) {
console.error('Failed to parse NPM Audit JSON!')
return process.exit(1)
}
const templateFile = template || `${__dirname}/templates/template.hbs`
reporter(json, templateFile, output)
.then(() => {
console.log(`Vulnerability snapshot saved at ${output}`)
process.exit(0)
})
.catch(err => {
console.log('An error occurred!')
console.log(err)
process.exit(1)
})
}
if (process.stdin.isTTY) {
program.parse(process.argv)
} else {
let stdin = ''
process.stdin.on('readable', function () {
const chunk = this.read()
if (chunk !== null) {
stdin += chunk
}
})
process.stdin.on('end', function () {
program.parse(process.argv)
genReport(stdin, program.output, program.template)
})
}