instrument.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var NYC
  2. try {
  3. NYC = require('../../index.covered.js')
  4. } catch (e) {
  5. NYC = require('../../index.js')
  6. }
  7. exports.command = 'instrument <input> [output]'
  8. exports.describe = 'instruments a file or a directory tree and writes the instrumented code to the desired output location'
  9. exports.builder = function (yargs) {
  10. return yargs
  11. .option('require', {
  12. alias: 'i',
  13. default: [],
  14. describe: 'a list of additional modules that nyc should attempt to require in its subprocess, e.g., @babel/register, @babel/polyfill.'
  15. })
  16. .option('extension', {
  17. alias: 'e',
  18. default: [],
  19. describe: 'a list of extensions that nyc should handle in addition to .js'
  20. })
  21. .option('source-map', {
  22. default: true,
  23. type: 'boolean',
  24. description: 'should nyc detect and handle source maps?'
  25. })
  26. .option('produce-source-map', {
  27. default: false,
  28. type: 'boolean',
  29. description: "should nyc's instrumenter produce source maps?"
  30. })
  31. .option('compact', {
  32. default: true,
  33. type: 'boolean',
  34. description: 'should the output be compacted?'
  35. })
  36. .option('preserve-comments', {
  37. default: true,
  38. type: 'boolean',
  39. description: 'should comments be preserved in the output?'
  40. })
  41. .option('instrument', {
  42. default: true,
  43. type: 'boolean',
  44. description: 'should nyc handle instrumentation?'
  45. })
  46. .option('exit-on-error', {
  47. default: false,
  48. type: 'boolean',
  49. description: 'should nyc exit when an instrumentation failure occurs?'
  50. })
  51. .example('$0 instrument ./lib ./output', 'instrument all .js files in ./lib with coverage and output in ./output')
  52. }
  53. exports.handler = function (argv) {
  54. // if instrument is set to false,
  55. // enable a noop instrumenter.
  56. if (!argv.instrument) argv.instrumenter = './lib/instrumenters/noop'
  57. else argv.instrumenter = './lib/instrumenters/istanbul'
  58. var nyc = new NYC({
  59. instrumenter: argv.instrumenter,
  60. sourceMap: argv.sourceMap,
  61. produceSourceMap: argv.produceSourceMap,
  62. extension: argv.extension,
  63. require: argv.require,
  64. compact: argv.compact,
  65. preserveComments: argv.preserveComments,
  66. exitOnError: argv.exitOnError
  67. })
  68. nyc.instrumentAllFiles(argv.input, argv.output, function (err) {
  69. if (err) {
  70. console.error(err.message)
  71. process.exit(1)
  72. } else {
  73. process.exit(0)
  74. }
  75. })
  76. }