• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const Utils = require('../lib/utils')
4
5module.exports = report
6function report (data, options) {
7  let msg = summary(data, options)
8  if (!Object.keys(data.advisories).length) {
9    return {
10      report: msg,
11      exitCode: 0
12    }
13  } else {
14    msg += '\n  run `npm audit fix` to fix them, or `npm audit` for details'
15    return {
16      report: msg,
17      exitCode: 1
18    }
19  }
20}
21
22module.exports.summary = summary
23function summary (data, options) {
24  const defaults = {
25    severityThreshold: 'info'
26  }
27
28  const config = Object.assign({}, defaults, options)
29
30  function clr (str, clr) { return Utils.color(str, clr, config.withColor) }
31  function green (str) { return clr(str, 'brightGreen') }
32  function red (str) { return clr(str, 'brightRed') }
33
34  let output = ''
35
36  const log = function (value) {
37    output = output + value + '\n'
38  }
39
40  output += 'found '
41
42  if (Object.keys(data.advisories).length === 0) {
43    log(`${green('0')} vulnerabilities`)
44    return output
45  } else {
46    const total = Utils.totalVulnCount(data.metadata.vulnerabilities)
47    const sev = Utils.severities(data.metadata.vulnerabilities)
48
49    if (sev.length > 1) {
50      const severities = sev.map((value) => {
51        return `${value[1]} ${Utils.severityLabel(value[0], config.withColor).toLowerCase()}`
52      }).join(', ')
53      log(`${red(total)} vulnerabilities (${severities})`)
54    } else {
55      const vulnCount = sev[0][1]
56      const vulnLabel = Utils.severityLabel(sev[0][0], config.withColor).toLowerCase()
57      log(`${vulnCount} ${vulnLabel} severity vulnerabilit${vulnCount === 1 ? 'y' : 'ies'}`)
58    }
59  }
60  return output.trim()
61}
62