2020-10-02 17:52:19 -04:00
|
|
|
const Arborist = require('@npmcli/arborist')
|
|
|
|
const auditReport = require('npm-audit-report')
|
2020-11-17 15:37:44 -05:00
|
|
|
const reifyFinish = require('./utils/reify-finish.js')
|
2020-10-15 20:32:02 -04:00
|
|
|
const auditError = require('./utils/audit-error.js')
|
2021-03-11 17:54:23 -05:00
|
|
|
const BaseCommand = require('./base-command.js')
|
2018-04-20 18:26:37 -07:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
class Audit extends BaseCommand {
|
2021-03-23 14:58:11 -04:00
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
|
|
static get description () {
|
|
|
|
return 'Run a security audit'
|
|
|
|
}
|
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
|
|
static get name () {
|
|
|
|
return 'audit'
|
2020-10-02 17:52:19 -04:00
|
|
|
}
|
2019-01-29 14:43:00 -08:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
2021-03-23 14:58:11 -04:00
|
|
|
static get params () {
|
2021-03-11 17:54:23 -05:00
|
|
|
return [
|
2021-03-24 17:25:32 -04:00
|
|
|
'audit-level',
|
2021-03-23 14:58:11 -04:00
|
|
|
'dry-run',
|
|
|
|
'force',
|
|
|
|
'json',
|
|
|
|
'package-lock-only',
|
|
|
|
'production',
|
2021-03-11 17:54:23 -05:00
|
|
|
]
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
2018-04-20 18:26:37 -07:00
|
|
|
|
2021-03-23 14:58:11 -04:00
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
|
|
static get usage () {
|
|
|
|
return ['[fix]']
|
|
|
|
}
|
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
async completion (opts) {
|
|
|
|
const argv = opts.conf.argv.remain
|
2018-04-20 18:26:37 -07:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
if (argv.length === 2)
|
|
|
|
return ['fix']
|
2018-04-20 18:26:37 -07:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
switch (argv[2]) {
|
|
|
|
case 'fix':
|
|
|
|
return []
|
|
|
|
default:
|
|
|
|
throw new Error(argv[2] + ' not recognized')
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
exec (args, cb) {
|
|
|
|
this.audit(args).then(() => cb()).catch(cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
async audit (args) {
|
2021-03-23 14:58:11 -04:00
|
|
|
const reporter = this.npm.config.get('json') ? 'json' : 'detail'
|
|
|
|
const opts = {
|
2021-03-04 17:40:28 -05:00
|
|
|
...this.npm.flatOptions,
|
|
|
|
audit: true,
|
|
|
|
path: this.npm.prefix,
|
2021-03-23 14:58:11 -04:00
|
|
|
reporter,
|
|
|
|
}
|
|
|
|
const arb = new Arborist(opts)
|
2021-03-04 17:40:28 -05:00
|
|
|
const fix = args[0] === 'fix'
|
|
|
|
await arb.audit({ fix })
|
|
|
|
if (fix)
|
|
|
|
await reifyFinish(this.npm, arb)
|
|
|
|
else {
|
|
|
|
// will throw if there's an error, because this is an audit command
|
|
|
|
auditError(this.npm, arb.auditReport)
|
2021-03-23 14:58:11 -04:00
|
|
|
const result = auditReport(arb.auditReport, opts)
|
2021-03-04 17:40:28 -05:00
|
|
|
process.exitCode = process.exitCode || result.exitCode
|
2021-03-11 17:54:23 -05:00
|
|
|
this.npm.output(result.report)
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
2018-04-20 18:26:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
module.exports = Audit
|