All files / lib/report module.js

86.87% Statements 192/221
68.57% Branches 24/35
100% Functions 3/3
86.87% Lines 192/221

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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 22227x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 3x 3x 3x 3x 3x 3x 3x 3x 51x 51x 1x 1x 51x 51x 7x 7x 51x 51x     51x 3x 3x       3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x                     3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 3x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x     1x 3x 2x 2x 3x 3x 3x 3x 3x 3x 7x 3x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x       7x 7x 3x 7x 4x 4x 3x 3x     3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 3x 3x 27x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 9x 9x 9x 9x 9x 58x     58x 58x 58x 9x 9x  
'use strict'
 
module.exports = moduleReport
 
const {
  W,
  SEVERITY_RMAP,
  SEVERITY_COLOR,
  severityMeter,
  shortVulnerabilityList
} = require('./util')
const {
  COLORS,
  boxbox
} = require('../ncm-style')
const chalk = require('chalk')
const L = console.log
 
function moduleReport (report) {
  const { scores, failures = [], license, requirePaths } = report
 
  let moduleRisks = []
  const securityVulnerabilities = []
  const codeQuality = []
 
  for (const score of scores) {
    if (score.group === 'risk' &&
        score.pass === false) {
      moduleRisks.push(score)
    }
    if (score.group === 'quality' &&
        score.pass === false) {
      codeQuality.push(score)
    }
    if (score.group === 'security' &&
    score.pass === false) {
      securityVulnerabilities.push(score)
    }
  }
 
  moduleRisks = moduleRisks.sort((a, b) => {
    if (SEVERITY_RMAP.indexOf(a.severity) > SEVERITY_RMAP.indexOf(b.severity)) return -1
    if (SEVERITY_RMAP.indexOf(b.severity) > SEVERITY_RMAP.indexOf(a.severity)) return 1
    return 0
  })
 
  const maxLength = W.reduce((a, b) => a + b)
 
  /* Module Risk */
  let moduleRisk = 'NONE'
  for (const { severity } of failures) {
    if (SEVERITY_RMAP.indexOf(severity) > SEVERITY_RMAP.indexOf(moduleRisk)) {
      moduleRisk = severity
    }
  }
  const riskColor = SEVERITY_COLOR[moduleRisk]
 
  L()
  L(boxbox(
    severityMeter(moduleRisk),
    `${moduleRisk[0].toUpperCase() + moduleRisk.slice(1).toLowerCase()} Risk`,
    riskColor,
    4 // Need to override the symbol length because unicode.
  ))
 
  /* Security Overview */
  L()
  L(chalk`{bold Security Risk:}`)
  shortVulnerabilityList([report])
  L()
 
  /* Security Detail */
  if (securityVulnerabilities.length > 0) {
    for (const { severity, title, data } of securityVulnerabilities) {
      L(boxbox(
        severity[0].toUpperCase(),
        title,
        SEVERITY_COLOR[severity]
      ))

      L(chalk`{${COLORS.light1} Versions ${data.vulnerable.join(' ')} (Source: Synk)}`)
      L(chalk`{${COLORS.blue} |➔ https://snyk.io/vuln/${data.id}}`)
    }
  } else {
    L(boxbox('✓', 'No Security Vulnerabilities', COLORS.green))
  }
 
  /* License */
  L()
  L(chalk`{bold License Risk:}`)
  const licenseName = license &&
                      license.data &&
                      license.data.spdx
    ? license.data.spdx
    : 'UNKNOWN'
  if (license && license.pass === true) {
    L(boxbox('✓', licenseName, COLORS.green))
  } else {
    let msg = 'Unknown license'
    if (licenseName) {
      msg = `Noncompliant license: ${licenseName}`
    }
    L(boxbox('X', msg, COLORS.red))
  }
 
  /* Module Risk */
  L()
  L(chalk`{bold Module Risk:}`)
  if (moduleRisks.length > 0) {
    moduleRisks.forEach(({ title, severity }, ind) => {
      if (ind === 0) {
        L(chalk`{${COLORS.light1} ┌──────┬─${'─'.repeat(maxLength)}─┐}`)
      }
 
      severity = severityTextLabel(severity)
 
      const lines = lineWrap(title, maxLength)
 
      {
        const line = lines.shift()
        L(chalk`{${COLORS.light1} │} {${COLORS.red} ${severity}} {${COLORS.light1} │}` +
        chalk`{white  ${line}} ${' '.repeat(maxLength - line.length)}{${COLORS.light1} │}`)
      }
 
      for (const line of lines) {
        L(chalk`{${COLORS.light1} │}      {${COLORS.light1} │}` +
        chalk`{white  ${line}} ${' '.repeat(maxLength - line.length)}{${COLORS.light1} │}`)
      }
 
      if (ind === moduleRisks.length - 1) {
        L(chalk`{${COLORS.light1} └──────┴─${'─'.repeat(maxLength)}─┘}`)
      } else {
        L(chalk`{${COLORS.light1} ├──────┼─${'─'.repeat(maxLength)}─┤}`)
      }
    })
  } else {
    L(boxbox('✓', 'No Module Risk', COLORS.green))
  }
 
  /* Code Quality */
  L()
  L(chalk`{bold Code Quality} (does not affect risk score){bold :}`)
  if (codeQuality.length > 0) {
    codeQuality.forEach(({ title }, ind) => {
      if (ind === 0) {
        L(chalk`{${COLORS.light1} ┌───┬─${'─'.repeat(maxLength)}─┐}`)
      }
 
      const lines = lineWrap(title, maxLength)
 
      {
        const line = lines.shift()
        L(chalk`{${COLORS.light1} │} {${COLORS.yellow} !} {${COLORS.light1} │}` +
        chalk`{white  ${line}} ${' '.repeat(maxLength - line.length)}{${COLORS.light1} │}`)
      }
 
      for (const line of lines) {
        L(chalk`{${COLORS.light1} │}      {${COLORS.light1} │}` +
        chalk`{white  ${line}} ${' '.repeat(maxLength - line.length)}{${COLORS.light1} │}`)
      }
 
      if (ind === codeQuality.length - 1) {
        L(chalk`{${COLORS.light1} └───┴─${'─'.repeat(maxLength)}─┘}`)
      } else {
        L(chalk`{${COLORS.light1} ├───┼─${'─'.repeat(maxLength)}─┤}`)
      }
    })
  } else {
    L(boxbox('✓', 'Passes all criteria', COLORS.green))
  }
  if (requirePaths.length > 0) {
    /* Required By */
    L()
    L(chalk`{bold Required By} (leftmost is directly in your package){bold :}`)
    requirePaths.sort().forEach((path, ind) => {
      if (ind === 0) {
        L(chalk`{${COLORS.light1} ┌─${'─'.repeat(maxLength)}─┐}`)
      }
 
      let formatted = path.map(({ data }) => `${data.name} @ ${data.version}`).join(' / ')
      if (!formatted) formatted = '(Directly in your package)'
      const lines = lineWrap(formatted, maxLength, '/')
 
      for (const line of lines) {
        L(chalk`{${COLORS.light1} │} ${line} ${' '.repeat(maxLength - line.length)}{${COLORS.light1} │}`)
      }
 
      if (ind === requirePaths.length - 1) {
        L(chalk`{${COLORS.light1} └─${'─'.repeat(maxLength)}─┘}`)
      } else {
        L(chalk`{${COLORS.light1} ├─${'─'.repeat(maxLength)}─┤}`)
      }
    })
  }
  L()
}
 
function severityTextLabel (severity) {
  const severityLabel = {
    CRITICAL: 'Crit',
    HIGH: 'High',
    MEDIUM: 'Med ',
    LOW: 'Low ',
    NONE: 'None'
  }
  return chalk`{${SEVERITY_COLOR[severity]} ${severityLabel[severity]}}`
}
 
function lineWrap (str, maxLength, split = ' ') {
  const words = str.split(split)
  const lines = []
  let lineIdx = 0
  for (const word of words) {
    while ((lines[lineIdx] + word).length >= maxLength) {
      lineIdx++
    }
    if (!lines[lineIdx]) lines[lineIdx] = lineIdx > 0 ? [''] : []
    lines[lineIdx].push(word)
  }
  return lines.map(line => line.join(split).trim())
}