• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// certain assertions we should do only when testing arborist itself, because
2// they are too expensive or aggressive and would break user programs if we
3// miss a situation where they are actually valid.
4//
5// call like this:
6//
7// /* istanbul ignore next - debug check */
8// debug(() => {
9//   if (someExpensiveCheck)
10//     throw new Error('expensive check should have returned false')
11// })
12
13// run in debug mode if explicitly requested, running arborist tests,
14// or working in the arborist project directory.
15
16const debug = process.env.ARBORIST_DEBUG !== '0' && (
17  process.env.ARBORIST_DEBUG === '1' ||
18  /\barborist\b/.test(process.env.NODE_DEBUG || '') ||
19  process.env.npm_package_name === '@npmcli/arborist' &&
20  ['test', 'snap'].includes(process.env.npm_lifecycle_event) ||
21  process.cwd() === require('path').resolve(__dirname, '..')
22)
23
24module.exports = debug ? fn => fn() : () => {}
25const red = process.stderr.isTTY ? msg => `\x1B[31m${msg}\x1B[39m` : m => m
26module.exports.log = (...msg) => module.exports(() => {
27  const { format } = require('util')
28  const prefix = `\n${process.pid} ${red(format(msg.shift()))} `
29  msg = (prefix + format(...msg).trim().split('\n').join(prefix)).trim()
30  console.error(msg)
31})
32