All files / lib/report short.js

92.38% Statements 97/105
81.48% Branches 22/27
100% Functions 2/2
92.38% Lines 97/105

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 10627x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 15x 15x 15x 15x 15x 15x 10x 10x 10x 10x 1x 1x 10x 2x 2x 10x     10x 4x 4x 10x 2x 2x 10x 2x 2x 10x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 540x 15x 15x 15x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x             15x 1x 1x 15x 27x 14x 14x 14x 3x 3x 14x 9x 9x 14x 8x 8x 14x 14x 14x 14x 14x  
'use strict'
 
module.exports = shortReport
 
const summary = require('./summary')
const { moduleList, SEVERITY_RMAP } = require('./util')
 
const {
  COLORS,
  tooltip
} = require('../ncm-style')
const chalk = require('chalk')
const L = console.log
 
function shortReport (report, whitelist, dir, argv) {
  let filterSecurity = argv ? !!argv.security : false
  let filterCompliance = argv ? !!argv.compliance : false
  let filterLevel = SEVERITY_RMAP.indexOf('NONE')
 
  if (argv.filter) {
    const segments = argv.filter.split(',')
      .map(s => s.trim().toLowerCase())
 
    if (segments.includes('compliance')) {
      filterCompliance = true
    }
    if (segments.includes('security')) {
      filterSecurity = true
    }
    if (segments.includes('c') || segments.includes('critical')) {
      filterLevel = SEVERITY_RMAP.indexOf('CRITICAL')
    }
    if (segments.includes('h') || segments.includes('high')) {
      filterLevel = SEVERITY_RMAP.indexOf('HIGH')
    }
    if (segments.includes('m') || segments.includes('medium')) {
      filterLevel = SEVERITY_RMAP.indexOf('MEDIUM')
    }
    if (segments.includes('l') || segments.includes('low')) {
      filterLevel = SEVERITY_RMAP.indexOf('LOW')
    }
  }
 
  const filterOptions = {
    filterCompliance,
    filterSecurity,
    filterLevel
  }
 
  const riskCount = summary(report, dir, filterOptions) || [0, 0, 0, 0, 0]
  riskCount.shift()
 
  if (whitelist.length > 0) {
    L(chalk`  {${COLORS.yellow} !} ${whitelist.length} used modules whitelisted`)
    L('    ' + tooltip('Run `ncm whitelist --list` for a list'))
    L()
  }
 
  let maxSeverityOfReport = 0
  report.forEach(({ maxSeverity }) => {
    maxSeverityOfReport = Math.max(maxSeverity, maxSeverityOfReport)
  })
 
  if (filterCompliance || filterSecurity || filterLevel > 0) {
    const filterFormat = formatFilterOptions(filterOptions)
    if (whitelist.length > 0) {
      moduleList(
        whitelist,
        `Whitelisted Filtered Modules (${filterFormat})`,
        filterOptions
      )
 
      moduleList(
        report,
        `Non-whitelisted Filtered Modules (${filterFormat})`,
        filterOptions
      )
    } else {
      moduleList(
        report,
        `Filtered Modules (${filterFormat})`,
        filterOptions
      )
    }
  } else if (riskCount.reduce((a, b) => a + b, 0) && maxSeverityOfReport > 0) {
    moduleList(report.slice(0, 5), 'Top 5: Highest Risk Modules')
  }
}
 
function formatFilterOptions (filterOptions) {
  let str = '--filter='
  if (filterOptions.filterCompliance) {
    str += 'compliance,'
  }
  if (filterOptions.filterSecurity) {
    str += 'security,'
  }
  if (filterOptions.filterLevel) {
    str += SEVERITY_RMAP[filterOptions.filterLevel] + ','
  }
  if (str[str.length - 1] === ',') {
    str = str.slice(0, str.length - 1)
  }
  return str
}