config-util.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. 'use strict'
  2. const arrify = require('arrify')
  3. const fs = require('fs')
  4. const path = require('path')
  5. const findUp = require('find-up')
  6. const testExclude = require('test-exclude')
  7. const Yargs = require('yargs/yargs')
  8. var Config = {}
  9. function guessCWD (cwd) {
  10. cwd = cwd || process.env.NYC_CWD || process.cwd()
  11. const pkgPath = findUp.sync('package.json', {cwd: cwd})
  12. if (pkgPath) {
  13. cwd = path.dirname(pkgPath)
  14. }
  15. return cwd
  16. }
  17. Config.loadConfig = function (argv, cwd) {
  18. const rcPath = findUp.sync([argv.nycrcPath || '.nycrc', '.nycrc.json'], {cwd: guessCWD(cwd)})
  19. let config = {}
  20. if (rcPath) {
  21. config = JSON.parse(
  22. fs.readFileSync(rcPath, 'utf-8')
  23. )
  24. }
  25. if (config.require) config.require = arrify(config.require)
  26. if (config.extension) config.extension = arrify(config.extension)
  27. if (config.exclude) config.exclude = arrify(config.exclude)
  28. if (config.include) config.include = arrify(config.include)
  29. return config
  30. }
  31. // build a yargs object, omitting any settings
  32. // that would cause the application to exit early.
  33. Config.buildYargs = function (cwd) {
  34. cwd = guessCWD(cwd)
  35. return Yargs([])
  36. .usage('$0 [command] [options]')
  37. .usage('$0 [options] [bin-to-instrument]')
  38. .option('reporter', {
  39. alias: 'r',
  40. describe: 'coverage reporter(s) to use',
  41. default: 'text',
  42. global: false
  43. })
  44. .option('report-dir', {
  45. describe: 'directory to output coverage reports in',
  46. default: 'coverage',
  47. global: false
  48. })
  49. .option('silent', {
  50. alias: 's',
  51. default: false,
  52. type: 'boolean',
  53. describe: "don't output a report after tests finish running",
  54. global: false
  55. })
  56. .option('all', {
  57. alias: 'a',
  58. default: false,
  59. type: 'boolean',
  60. describe: 'whether or not to instrument all files of the project (not just the ones touched by your test suite)',
  61. global: false
  62. })
  63. .option('exclude', {
  64. alias: 'x',
  65. default: testExclude.defaultExclude,
  66. describe: 'a list of specific files and directories that should be excluded from coverage, glob patterns are supported, node_modules is always excluded',
  67. global: false
  68. })
  69. .option('exclude-after-remap', {
  70. default: true,
  71. type: 'boolean',
  72. description: 'should exclude logic be performed after the source-map remaps filenames?',
  73. global: false
  74. })
  75. .option('include', {
  76. alias: 'n',
  77. default: [],
  78. describe: 'a list of specific files that should be covered, glob patterns are supported',
  79. global: false
  80. })
  81. .option('cwd', {
  82. describe: 'working directory used when resolving paths',
  83. default: cwd
  84. })
  85. .option('require', {
  86. alias: 'i',
  87. default: [],
  88. describe: 'a list of additional modules that nyc should attempt to require in its subprocess, e.g., babel-register, babel-polyfill',
  89. global: false
  90. })
  91. .option('eager', {
  92. default: false,
  93. type: 'boolean',
  94. describe: 'instantiate the instrumenter at startup (see https://git.io/vMKZ9)',
  95. global: false
  96. })
  97. .option('cache', {
  98. alias: 'c',
  99. default: true,
  100. type: 'boolean',
  101. describe: 'cache instrumentation results for improved performance',
  102. global: false
  103. })
  104. .option('babel-cache', {
  105. default: false,
  106. type: 'boolean',
  107. describe: 'cache babel transpilation results for improved performance',
  108. global: false
  109. })
  110. .option('extension', {
  111. alias: 'e',
  112. default: [],
  113. describe: 'a list of extensions that nyc should handle in addition to .js',
  114. global: false
  115. })
  116. .option('check-coverage', {
  117. type: 'boolean',
  118. default: false,
  119. describe: 'check whether coverage is within thresholds provided',
  120. global: false
  121. })
  122. .option('branches', {
  123. default: 0,
  124. description: 'what % of branches must be covered?',
  125. global: false
  126. })
  127. .option('functions', {
  128. default: 0,
  129. description: 'what % of functions must be covered?',
  130. global: false
  131. })
  132. .option('lines', {
  133. default: 90,
  134. description: 'what % of lines must be covered?',
  135. global: false
  136. })
  137. .option('statements', {
  138. default: 0,
  139. description: 'what % of statements must be covered?',
  140. global: false
  141. })
  142. .option('source-map', {
  143. default: true,
  144. type: 'boolean',
  145. description: 'should nyc detect and handle source maps?',
  146. global: false
  147. })
  148. .option('per-file', {
  149. default: false,
  150. type: 'boolean',
  151. description: 'check thresholds per file',
  152. global: false
  153. })
  154. .option('produce-source-map', {
  155. default: false,
  156. type: 'boolean',
  157. description: "should nyc's instrumenter produce source maps?",
  158. global: false
  159. })
  160. .option('compact', {
  161. default: true,
  162. type: 'boolean',
  163. description: 'should the output be compacted?'
  164. })
  165. .option('preserve-comments', {
  166. default: true,
  167. type: 'boolean',
  168. description: 'should comments be preserved in the output?'
  169. })
  170. .option('instrument', {
  171. default: true,
  172. type: 'boolean',
  173. description: 'should nyc handle instrumentation?',
  174. global: false
  175. })
  176. .option('hook-run-in-context', {
  177. default: true,
  178. type: 'boolean',
  179. description: 'should nyc wrap vm.runInContext?',
  180. global: false
  181. })
  182. .option('hook-run-in-this-context', {
  183. default: true,
  184. type: 'boolean',
  185. description: 'should nyc wrap vm.runInThisContext?',
  186. global: false
  187. })
  188. .option('show-process-tree', {
  189. describe: 'display the tree of spawned processes',
  190. default: false,
  191. type: 'boolean',
  192. global: false
  193. })
  194. .option('clean', {
  195. describe: 'should the .nyc_output folder be cleaned before executing tests',
  196. default: true,
  197. type: 'boolean',
  198. global: false
  199. })
  200. .option('nycrc-path', {
  201. default: '.nycrc',
  202. description: 'specify a different .nycrc path',
  203. global: false
  204. })
  205. .option('temp-directory', {
  206. describe: 'directory to output raw coverage information to',
  207. default: './.nyc_output',
  208. global: false
  209. })
  210. .option('skip-empty', {
  211. describe: 'don\'t show empty files (no lines of code) in report',
  212. default: false,
  213. type: 'boolean',
  214. global: false
  215. })
  216. .pkgConf('nyc', cwd)
  217. .example('$0 npm test', 'instrument your tests with coverage')
  218. .example('$0 --require babel-core/register npm test', 'instrument your tests with coverage and transpile with Babel')
  219. .example('$0 report --reporter=text-lcov', 'output lcov report after running your tests')
  220. .epilog('visit https://git.io/vHysA for list of available reporters')
  221. .boolean('h')
  222. .boolean('version')
  223. .help(false)
  224. .version(false)
  225. }
  226. // we add operations that would make yargs
  227. // exit post-hoc, allowing for a multi-pass
  228. // parsing step.
  229. Config.addCommandsAndHelp = function (yargs) {
  230. return yargs
  231. .help('h')
  232. .alias('h', 'help')
  233. .version()
  234. .command(require('../lib/commands/check-coverage'))
  235. .command(require('../lib/commands/instrument'))
  236. .command(require('../lib/commands/report'))
  237. .command(require('../lib/commands/merge'))
  238. }
  239. module.exports = Config