run-reports.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. var Reporter = require('./reporter'),
  6. fs = require('fs'),
  7. filesFor = require('./file-matcher').filesFor,
  8. libCoverage = require('istanbul-lib-coverage');
  9. function run(formats, config, opts, callback) {
  10. if (!callback && typeof(opts) === 'function') {
  11. callback = opts;
  12. opts = {};
  13. }
  14. opts = opts || {};
  15. var root,
  16. coverageMap = libCoverage.createCoverageMap(),
  17. includePattern = opts.include || '**/coverage*.raw.json',
  18. reporter = new Reporter(config);
  19. if (!formats || formats.length === 0) {
  20. formats = config.reporting.reports();
  21. }
  22. try {
  23. reporter.addAll(formats);
  24. } catch (ex) {
  25. ex.inputError = true;
  26. return callback(ex);
  27. }
  28. root = opts.root || process.cwd();
  29. filesFor({
  30. root: root,
  31. includes: [ includePattern ]
  32. }, function (err, files) {
  33. /* istanbul ignore if */
  34. if (err) {
  35. return callback(err);
  36. }
  37. files.forEach(function (file) {
  38. var coverageObject = JSON.parse(fs.readFileSync(file, 'utf8'));
  39. coverageMap.merge(coverageObject);
  40. });
  41. reporter.write(coverageMap, {});
  42. return callback();
  43. });
  44. }
  45. module.exports = {
  46. run: run
  47. };