nyc.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env node
  2. const configUtil = require('../lib/config-util')
  3. const foreground = require('foreground-child')
  4. var NYC
  5. try {
  6. NYC = require('../index.covered.js')
  7. } catch (e) {
  8. NYC = require('../index.js')
  9. }
  10. const processArgs = require('../lib/process-args')
  11. const sw = require('spawn-wrap')
  12. const wrapper = require.resolve('./wrap.js')
  13. // parse configuration and command-line arguments;
  14. // we keep these values in a few different forms,
  15. // used in the various execution contexts of nyc:
  16. // reporting, instrumenting subprocesses, etc.
  17. const yargs = configUtil.buildYargs()
  18. const instrumenterArgs = processArgs.hideInstrumenteeArgs()
  19. const config = configUtil.loadConfig(yargs.parse(instrumenterArgs))
  20. configUtil.addCommandsAndHelp(yargs)
  21. const argv = yargs.config(config).parse(instrumenterArgs)
  22. if ([
  23. 'check-coverage', 'report', 'instrument', 'merge'
  24. ].indexOf(argv._[0]) !== -1) {
  25. // look in lib/commands for logic.
  26. } else if (argv._.length) {
  27. // if instrument is set to false,
  28. // enable a noop instrumenter.
  29. if (!argv.instrument) argv.instrumenter = './lib/instrumenters/noop'
  30. else argv.instrumenter = './lib/instrumenters/istanbul'
  31. var nyc = (new NYC(argv))
  32. if (argv.clean) {
  33. nyc.reset()
  34. } else {
  35. nyc.createTempDirectory()
  36. }
  37. if (argv.all) nyc.addAllFiles()
  38. var env = {
  39. // Support running nyc as a user without HOME (e.g. linux 'nobody'),
  40. // https://github.com/istanbuljs/nyc/issues/951
  41. SPAWN_WRAP_SHIM_ROOT: process.env.SPAWN_WRAP_SHIM_ROOT || process.env.XDG_CACHE_HOME || require('os').homedir(),
  42. NYC_CONFIG: JSON.stringify(argv),
  43. NYC_CWD: process.cwd(),
  44. NYC_ROOT_ID: nyc.rootId,
  45. NYC_INSTRUMENTER: argv.instrumenter
  46. }
  47. if (argv['babel-cache'] === false) {
  48. // babel's cache interferes with some configurations, so is
  49. // disabled by default. opt in by setting babel-cache=true.
  50. env.BABEL_DISABLE_CACHE = process.env.BABEL_DISABLE_CACHE = '1'
  51. }
  52. sw([wrapper], env)
  53. // Both running the test script invocation and the check-coverage run may
  54. // set process.exitCode. Keep track so that both children are run, but
  55. // a non-zero exit codes in either one leads to an overall non-zero exit code.
  56. process.exitCode = 0
  57. foreground(processArgs.hideInstrumenterArgs(
  58. // use the same argv descrption, but don't exit
  59. // for flags like --help.
  60. configUtil.buildYargs().parse(process.argv.slice(2))
  61. ), function (done) {
  62. var mainChildExitCode = process.exitCode
  63. if (argv.checkCoverage) {
  64. checkCoverage(argv)
  65. process.exitCode = process.exitCode || mainChildExitCode
  66. if (!argv.silent) report(argv)
  67. return done()
  68. } else {
  69. if (!argv.silent) report(argv)
  70. return done()
  71. }
  72. })
  73. } else {
  74. // I don't have a clue what you're doing.
  75. yargs.showHelp()
  76. }
  77. function report (argv) {
  78. process.env.NYC_CWD = process.cwd()
  79. var nyc = new NYC(argv)
  80. nyc.report()
  81. }
  82. function checkCoverage (argv, cb) {
  83. process.env.NYC_CWD = process.cwd()
  84. ;(new NYC(argv)).checkCoverage({
  85. lines: argv.lines,
  86. functions: argv.functions,
  87. branches: argv.branches,
  88. statements: argv.statements
  89. }, argv['per-file'])
  90. }