|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| const { log, output } = require('proc-log')
|
| const { depth } = require('treeverse')
|
| const ms = require('ms')
|
| const npmAuditReport = require('npm-audit-report')
|
| const { readTree: getFundingInfo } = require('libnpmfund')
|
| const auditError = require('./audit-error.js')
|
|
|
|
|
| const reifyOutput = (npm, arb) => {
|
| const { diff, actualTree } = arb
|
|
|
|
|
| const auditReport = auditError(npm, arb.auditReport) ? null : arb.auditReport
|
|
|
|
|
| if (npm.silent) {
|
| getAuditReport(npm, auditReport)
|
| return
|
| }
|
|
|
| const summary = {
|
| add: [],
|
| added: 0,
|
|
|
| audited: auditReport && !auditReport.error ? actualTree.inventory.size : 0,
|
| change: [],
|
| changed: 0,
|
| funding: 0,
|
| remove: [],
|
| removed: 0,
|
| }
|
|
|
| if (diff) {
|
| const showDiff = npm.config.get('dry-run') || npm.config.get('long')
|
| const chalk = npm.chalk
|
|
|
| depth({
|
| tree: diff,
|
| visit: d => {
|
| switch (d.action) {
|
| case 'REMOVE':
|
| if (showDiff) {
|
| output.standard(`${chalk.blue('remove')} ${d.actual.name} ${d.actual.package.version}`)
|
| }
|
| summary.removed++
|
| summary.remove.push({
|
| name: d.actual.name,
|
| version: d.actual.package.version,
|
| path: d.actual.path,
|
| })
|
| break
|
| case 'ADD':
|
| if (showDiff) {
|
| output.standard(`${chalk.green('add')} ${d.ideal.name} ${d.ideal.package.version}`)
|
| }
|
| if (actualTree.inventory.has(d.ideal)) {
|
| summary.added++
|
| summary.add.push({
|
| name: d.ideal.name,
|
| version: d.ideal.package.version,
|
| path: d.ideal.path,
|
| })
|
| }
|
| break
|
| case 'CHANGE':
|
| if (showDiff) {
|
| output.standard(`${chalk.cyan('change')} ${d.actual.name} ${d.actual.package.version} => ${d.ideal.package.version}`)
|
| }
|
| summary.changed++
|
| summary.change.push({
|
| from: {
|
| name: d.actual.name,
|
| version: d.actual.package.version,
|
| path: d.actual.path,
|
| },
|
| to: {
|
| name: d.ideal.name,
|
| version: d.ideal.package.version,
|
| path: d.ideal.path,
|
| },
|
| })
|
| 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) {
|
|
|
| getAuditReport(npm, auditReport)
|
| summary.audit = npm.command === 'audit' ? auditReport
|
| : auditReport.toJSON().metadata
|
| }
|
| output.buffer(summary)
|
| } else {
|
| packagesChangedMessage(npm, summary)
|
| packagesFundingMessage(npm, summary)
|
| printAuditReport(npm, auditReport)
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
| const printAuditReport = (npm, report) => {
|
| const res = getAuditReport(npm, report)
|
| if (!res || !res.report) {
|
| return
|
| }
|
| output.standard(`\n${res.report}`)
|
| }
|
|
|
| const getAuditReport = (npm, report) => {
|
| if (!report) {
|
| return
|
| }
|
|
|
|
|
|
|
| const reporter = npm.silent ? 'quiet'
|
| : npm.flatOptions.json ? 'quiet'
|
| : npm.command !== 'audit' ? 'install'
|
| : 'detail'
|
| const defaultAuditLevel = npm.command !== 'audit' ? 'none' : 'low'
|
| const auditLevel = npm.flatOptions.auditLevel || defaultAuditLevel
|
|
|
| const res = npmAuditReport(report, {
|
| reporter,
|
| ...npm.flatOptions,
|
| auditLevel,
|
| chalk: npm.chalk,
|
| })
|
| if (npm.command === 'audit') {
|
| process.exitCode = process.exitCode || res.exitCode
|
| }
|
| return res
|
| }
|
|
|
| const packagesChangedMessage = (npm, { 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.standard(msg.join(''))
|
| }
|
|
|
| const packagesFundingMessage = (npm, { funding }) => {
|
| if (!funding) {
|
| return
|
| }
|
|
|
| output.standard()
|
| const pkg = funding === 1 ? 'package' : 'packages'
|
| const is = funding === 1 ? 'is' : 'are'
|
| output.standard(`${funding} ${pkg} ${is} looking for funding`)
|
| output.standard(' run `npm fund` for details')
|
| }
|
|
|
| module.exports = reifyOutput
|
|
|