All files / lib/utils reify-output.js

100% Statements 81/81
100% Branches 83/83
100% Functions 6/6
100% Lines 81/81

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155                      9x 9x 9x 9x 9x 9x 9x 9x     9x   172x 1x   171x         171x   171x               171x 170x     822x   162x 162x   164x 164x   162x 162x   334x   488x 488x   822x       171x 170x 170x     171x 82x 55x     82x   89x 89x 89x               9x 89x 34x   55x 55x 55x   55x         55x 55x     9x 89x 89x 10x 10x 3x   79x 55x   79x 54x 36x   54x 4x   54x   79x 54x 48x   54x 16x   54x   79x 52x   89x 55x   89x 89x     9x 89x 87x   2x 2x 2x 2x 2x     9x  
// pass in an arborist object, and it'll output the data about what
// was done, what was audited, etc.
//
// added 351 packages, removed 132 packages, and audited 13388 packages in 19.157s
//
// 1 package is looking for funding
//   run `npm fund` for details
//
// found 37 vulnerabilities (5 low, 7 moderate, 25 high)
//   run `npm audit fix` to fix them, or `npm audit` for details
 
const npm = require('../npm.js')
const log = require('npmlog')
const output = require('./output.js')
const { depth } = require('treeverse')
const ms = require('ms')
const auditReport = require('npm-audit-report')
const { readTree: getFundingInfo } = require('libnpmfund')
const auditError = require('./audit-error.js')
 
// TODO: output JSON if flatOptions.json is true
const reifyOutput = arb => {
  // don't print any info in --silent mode
  if (log.levels[log.level] > log.levels.error)
    return
 
  const { diff, actualTree } = arb
 
  // note: fails and crashes if we're running audit fix and there was an error
  // which is a good thing, because there's no point printing all this other
  // stuff in that case!
  const auditReport = auditError(arb.auditReport) ? null : arb.auditReport
 
  const summary = {
    added: 0,
    removed: 0,
    changed: 0,
    audited: auditReport && !auditReport.error ? actualTree.inventory.size : 0,
    funding: 0,
  }
 
  if (diff) {
    depth({
      tree: diff,
      visit: d => {
        switch (d.action) {
          case 'REMOVE':
            summary.removed++
            break
          case 'ADD':
            actualTree.inventory.has(d.ideal) && summary.added++
            break
          case 'CHANGE':
            summary.changed++
            break
          default:
            return
        }
        const node = d.actual || d.ideal
        log.silly(d.action, node.location)
      },
      getChildren: d => d.children,
    })
  }
 
  if (npm.flatOptions.fund) {
    const fundingInfo = getFundingInfo(actualTree, { countOnly: true })
    summary.funding = fundingInfo.length
  }
 
  if (npm.flatOptions.json) {
    if (auditReport) {
      summary.audit = npm.command === 'audit' ? auditReport
        : auditReport.toJSON().metadata
    }
    output(JSON.stringify(summary, 0, 2))
  } else {
    packagesChangedMessage(summary)
    packagesFundingMessage(summary)
    printAuditReport(auditReport)
  }
}
 
// if we're running `npm audit fix`, then we print the full audit report
// at the end if there's still stuff, because it's silly for `npm audit`
// to tell you to run `npm audit` for details.  otherwise, use the summary
// report.  if we get here, we know it's not quiet or json.
const printAuditReport = report => {
  if (!report)
    return
 
  const reporter = npm.command !== 'audit' ? 'install' : 'detail'
  const defaultAuditLevel = npm.command !== 'audit' ? 'none' : 'low'
  const auditLevel = npm.flatOptions.auditLevel || defaultAuditLevel
 
  const res = auditReport(report, {
    reporter,
    ...npm.flatOptions,
    auditLevel,
  })
  process.exitCode = process.exitCode || res.exitCode
  output('\n' + res.report)
}
 
const packagesChangedMessage = ({ added, removed, changed, audited }) => {
  const msg = ['\n']
  if (added === 0 && removed === 0 && changed === 0) {
    msg.push('up to date')
    if (audited)
      msg.push(', ')
  } else {
    if (added)
      msg.push(`added ${added} package${added === 1 ? '' : 's'}`)
 
    if (removed) {
      if (added)
        msg.push(', ')
 
      if (added && !audited && !changed)
        msg.push('and ')
 
      msg.push(`removed ${removed} package${removed === 1 ? '' : 's'}`)
    }
    if (changed) {
      if (added || removed)
        msg.push(', ')
 
      if (!audited && (added || removed))
        msg.push('and ')
 
      msg.push(`changed ${changed} package${changed === 1 ? '' : 's'}`)
    }
    if (audited)
      msg.push(', and ')
  }
  if (audited)
    msg.push(`audited ${audited} package${audited === 1 ? '' : 's'}`)
 
  msg.push(` in ${ms(Date.now() - npm.started)}`)
  output(msg.join(''))
}
 
const packagesFundingMessage = ({ funding }) => {
  if (!funding)
    return
 
  output('')
  const pkg = funding === 1 ? 'package' : 'packages'
  const is = funding === 1 ? 'is' : 'are'
  output(`${funding} ${pkg} ${is} looking for funding`)
  output('  run `npm fund` for details')
}
 
module.exports = reifyOutput