Skip to content

Commit

Permalink
feat(cli): Add option to exit with non-zero code if there are vulnera…
Browse files Browse the repository at this point in the history
…bilities
  • Loading branch information
nprail committed Oct 22, 2020
2 parents 067125b + e412b63 commit 15fb781
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ You can also fully customize the generated report by providing `--template` opti
npm audit --json | npm-audit-html --template ./my-awesome-template.hbs
```

If you'd like the generator to exit with non-zero exit code when vulnerabilities are found, you can add the `--fatal-exit-code` option:
```bash
npm audit --json | npm-audit-html --fatal-exit-code
```

## ✍️ Authors <a name = "authors"></a>

- [@nprail](https://github.com/nprail) - Maintainer
Expand Down
18 changes: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ program
'template theme `dark` or `light` (defaults to `light`)'
)
.option('-t, --template [handlebars file]', 'handlebars template file')
.option('--fatal-exit-code', 'exit with code 1 if vulnerabilities were found')
.action(async (cmd, env) => {
try {
let data
Expand All @@ -33,7 +34,7 @@ program
return process.exit(1)
}

await genReport(data, cmd.output, cmd.template, cmd.theme)
await genReport(data, cmd.output, cmd.template, cmd.theme, cmd.fatalExitCode)
} catch (err) {
console.error('Failed to parse NPM Audit JSON!')
return process.exit(1)
Expand All @@ -44,7 +45,8 @@ const genReport = async (
data,
output = 'npm-audit.html',
template,
theme = 'light'
theme = 'light',
fatalExitCode = false
) => {
try {
if (!data) {
Expand All @@ -54,9 +56,17 @@ const genReport = async (

const templateFile = template || path.join(__dirname, '/templates/template.hbs')

await reporter(data, templateFile, output, theme)
const modifiedData = await reporter(data, templateFile, output, theme)
if (modifiedData.metadata.vulnerabilities.total > 0) {
console.log(`Vulnerability snapshot saved at ${output}`)
if (fatalExitCode) {
process.exit(1)
}
process.exit(0)
}

console.log(`Vulnerability snapshot saved at ${output}`)
console.log('No vulnerabilities found')
console.log(`Vulnerability snapshot save at ${output}`)
process.exit(0)
} catch (err) {
console.log('An error occurred!')
Expand Down
1 change: 1 addition & 0 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ module.exports = async (data, templateFile, outputFile, theme) => {
modifiedData.theme = theme
const report = await generateTemplate(modifiedData, templateFile)
await writeReport(report, outputFile)
return modifiedData
} catch (err) {
console.log(err)
}
Expand Down

0 comments on commit 15fb781

Please sign in to comment.