merge.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict'
  2. const fs = require('fs')
  3. var NYC
  4. try {
  5. NYC = require('../../index.covered.js')
  6. } catch (e) {
  7. NYC = require('../../index.js')
  8. }
  9. exports.command = 'merge <input-directory> [output-file]'
  10. exports.describe = 'merge istanbul format coverage output in a given folder'
  11. exports.builder = function (yargs) {
  12. return yargs
  13. .positional('input-directory', {
  14. describe: 'directory containing multiple istanbul coverage files',
  15. type: 'text',
  16. default: './.nyc_output'
  17. })
  18. .positional('output-file', {
  19. describe: 'file to output combined istanbul format coverage to',
  20. type: 'text',
  21. default: 'coverage.json'
  22. })
  23. .option('temp-directory', {
  24. describe: 'directory to read raw coverage information from',
  25. default: './.nyc_output'
  26. })
  27. .example('$0 merge ./out coverage.json', 'merge together reports in ./out and output as coverage.json')
  28. }
  29. exports.handler = function (argv) {
  30. process.env.NYC_CWD = process.cwd()
  31. const nyc = new NYC(argv)
  32. let inputStat
  33. try {
  34. inputStat = fs.statSync(argv.inputDirectory)
  35. if (!inputStat.isDirectory()) {
  36. console.error(`${argv.inputDirectory} was not a directory`)
  37. process.exit(1)
  38. }
  39. } catch (err) {
  40. console.error(`failed access input directory ${argv.inputDirectory} with error:\n\n${err.message}`)
  41. process.exit(1)
  42. }
  43. const map = nyc.getCoverageMapFromAllCoverageFiles(argv.inputDirectory)
  44. fs.writeFileSync(argv.outputFile, JSON.stringify(map, null, 2), 'utf8')
  45. console.info(`coverage files in ${argv.inputDirectory} merged into ${argv.outputFile}`)
  46. }