All files / lib/utils setup-log.js

100% Statements 31/31
100% Branches 30/30
100% Functions 2/2
100% Lines 31/31

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                1x 1x   1x 8x   8x   8x 2x 1x 1x   1x     8x 1x   7x   8x   8x 8x 8x 8x   8x       8x       8x 5x   3x   8x 1x   7x     8x   8x 2x   6x   8x    
// module to set the appropriate log settings based on configs
// returns a boolean to say whether we should enable color on
// stdout or not.
//
// Also (and this is a really inexcusable kludge), we patch the
// log.warn() method so that when we see a peerDep override
// explanation from Arborist, we can replace the object with a
// highly abbreviated explanation of what's being overridden.
const log = require('npmlog')
const { explain } = require('./explain-eresolve.js')
 
module.exports = (config) => {
  const color = config.get('color')
 
  const { warn } = log
 
  log.warn = (heading, ...args) => {
    if (heading === 'ERESOLVE' && args[1] && typeof args[1] === 'object') {
      warn(heading, args[0])
      return warn('', explain(args[1]))
    }
    return warn(heading, ...args)
  }
 
  if (config.get('timing') && config.get('loglevel') === 'notice')
    log.level = 'timing'
  else
    log.level = config.get('loglevel')
 
  log.heading = config.get('heading') || 'npm'
 
  const stdoutTTY = process.stdout.isTTY
  const stderrTTY = process.stderr.isTTY
  const dumbTerm = process.env.TERM === 'dumb'
  const stderrNotDumb = stderrTTY && !dumbTerm
 
  const enableColorStderr = color === 'always' ? true
    : color === false ? false
    : stderrTTY
 
  const enableColorStdout = color === 'always' ? true
    : color === false ? false
    : stdoutTTY
 
  if (enableColorStderr)
    log.enableColor()
  else
    log.disableColor()
 
  if (config.get('unicode'))
    log.enableUnicode()
  else
    log.disableUnicode()
 
  // if it's more than error, don't show progress
  const quiet = log.levels[log.level] > log.levels.error
 
  if (config.get('progress') && stderrNotDumb && !quiet)
    log.enableProgress()
  else
    log.disableProgress()
 
  return enableColorStdout
}