setup-log.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // module to set the appropriate log settings based on configs
  2. // returns a boolean to say whether we should enable color on
  3. // stdout or not.
  4. //
  5. // Also (and this is a really inexcusable kludge), we patch the
  6. // log.warn() method so that when we see a peerDep override
  7. // explanation from Arborist, we can replace the object with a
  8. // highly abbreviated explanation of what's being overridden.
  9. const log = require('npmlog')
  10. const { explain } = require('./explain-eresolve.js')
  11. module.exports = (config) => {
  12. const color = config.get('color')
  13. const { warn } = log
  14. const stdoutTTY = process.stdout.isTTY
  15. const stderrTTY = process.stderr.isTTY
  16. const dumbTerm = process.env.TERM === 'dumb'
  17. const stderrNotDumb = stderrTTY && !dumbTerm
  18. // this logic is duplicated in the config 'color' flattener
  19. const enableColorStderr = color === 'always' ? true
  20. : color === false ? false
  21. : stderrTTY
  22. const enableColorStdout = color === 'always' ? true
  23. : color === false ? false
  24. : stdoutTTY
  25. log.warn = (heading, ...args) => {
  26. if (heading === 'ERESOLVE' && args[1] && typeof args[1] === 'object') {
  27. warn(heading, args[0])
  28. return warn('', explain(args[1], enableColorStdout, 2))
  29. }
  30. return warn(heading, ...args)
  31. }
  32. if (config.get('timing') && config.get('loglevel') === 'notice')
  33. log.level = 'timing'
  34. else
  35. log.level = config.get('loglevel')
  36. log.heading = config.get('heading') || 'npm'
  37. if (enableColorStderr)
  38. log.enableColor()
  39. else
  40. log.disableColor()
  41. if (config.get('unicode'))
  42. log.enableUnicode()
  43. else
  44. log.disableUnicode()
  45. // if it's more than error, don't show progress
  46. const quiet = log.levels[log.level] > log.levels.error
  47. if (config.get('progress') && stderrNotDumb && !quiet)
  48. log.enableProgress()
  49. else
  50. log.disableProgress()
  51. }