Skip to content

Commit 244b183

Browse files
luislobozkat
authored andcommitted
audit: add support for --parseable output (#20554)
PR-URL: npm/npm#20554 Credit: @luislobo Reviewed-By: @zkat Reviewed-By: @iarna
1 parent 7381783 commit 244b183

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

doc/cli/npm-audit.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ npm-audit(1) -- Run a security audit
33

44
## SYNOPSIS
55

6-
npm audit [--json]
6+
npm audit [--json|--parseable]
77
npm audit fix [--force|--package-lock-only|--dry-run|--production|--only=dev]
88

99
## EXAMPLES
@@ -48,6 +48,18 @@ Get the detailed audit report in JSON format:
4848
$ npm audit --json
4949
```
5050

51+
Get the detailed audit report in plain text result, separated by tab characters, allowing for
52+
future reuse in scripting or command line post processing, like for example, selecting
53+
some of the columns printed:
54+
```
55+
$ npm audit --parseable
56+
```
57+
58+
To parse columns, you can use for example `awk`, and just print some of them:
59+
```
60+
$ npm audit --parseable | awk -F $'\t' '{print $1,$4}'
61+
```
62+
5163
## DESCRIPTION
5264

5365
The audit command submits a description of the dependencies configured in

lib/audit.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function maybeReadFile (name) {
104104
}
105105
})
106106
.catch({code: 'ENOENT'}, () => null)
107-
.catch(ex => {
107+
.catch((ex) => {
108108
ex.file = file
109109
throw ex
110110
})
@@ -156,7 +156,7 @@ function auditCmd (args, cb) {
156156
(pkgJson && pkgJson.dependencies) || {},
157157
(pkgJson && pkgJson.devDependencies) || {}
158158
)
159-
return lockVerify(npm.prefix).then(result => {
159+
return lockVerify(npm.prefix).then((result) => {
160160
if (result.status) return audit.generate(sw, requires)
161161

162162
const lockFile = shrinkwrap ? 'npm-shrinkwrap.json' : 'package-lock.json'
@@ -167,7 +167,7 @@ function auditCmd (args, cb) {
167167
})
168168
}).then((auditReport) => {
169169
return audit.submitForFullReport(auditReport)
170-
}).catch(err => {
170+
}).catch((err) => {
171171
if (err.statusCode === 404 || err.statusCode >= 500) {
172172
const ne = new Error(`Your configured registry (${npm.config.get('registry')}) does not support audit requests.`)
173173
ne.code = 'ENOAUDIT'
@@ -262,7 +262,11 @@ function auditCmd (args, cb) {
262262
auditResult.metadata.vulnerabilities.high +
263263
auditResult.metadata.vulnerabilities.critical
264264
if (vulns > 0) process.exitCode = 1
265-
return audit.printFullReport(auditResult)
265+
if (npm.config.get('parseable')) {
266+
return audit.printParseableReport(auditResult)
267+
} else {
268+
return audit.printFullReport(auditResult)
269+
}
266270
}
267271
}).asCallback(cb)
268272
}

lib/install/audit.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports.generateFromInstall = generateFromInstall
44
exports.submitForInstallReport = submitForInstallReport
55
exports.submitForFullReport = submitForFullReport
66
exports.printInstallReport = printInstallReport
7+
exports.printParseableReport = printParseableReport
78
exports.printFullReport = printFullReport
89

910
const Bluebird = require('bluebird')
@@ -112,6 +113,15 @@ function printFullReport (auditResult) {
112113
}).then(result => output(result.report))
113114
}
114115

116+
function printParseableReport (auditResult) {
117+
return auditReport(auditResult, {
118+
log: output,
119+
reporter: 'parseable',
120+
withColor: npm.color,
121+
withUnicode: npm.config.get('unicode')
122+
}).then(result => output(result.report))
123+
}
124+
115125
function generate (shrinkwrap, requires, diffs, install, remove) {
116126
const sw = cloneDeep(shrinkwrap)
117127
delete sw.lockfileVersion

0 commit comments

Comments
 (0)