reporter.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. const path = require('path');
  6. const libReport = require('istanbul-lib-report');
  7. const libReports = require('istanbul-reports');
  8. const minimatch = require('minimatch');
  9. const inputError = require('./input-error');
  10. const configuration = require('./config');
  11. function Reporter(cfg, opts) {
  12. opts = opts || {};
  13. this.config = cfg || configuration.loadFile();
  14. this.dir = path.resolve(this.config.reporting.dir());
  15. this.reports = {};
  16. let summarizer = opts.summarizer;
  17. const s = this.config.reporting.summarizer();
  18. if (summarizer && typeof summarizer === 'function') {
  19. this.summarizer = summarizer;
  20. } else {
  21. summarizer = libReport.summarizers[s];
  22. if (!summarizer) {
  23. throw inputError.create(
  24. 'Invalid summarizer in report config: ' + s
  25. );
  26. }
  27. this.summarizer = summarizer;
  28. }
  29. }
  30. Reporter.prototype = {
  31. /**
  32. * adds a report to be generated. Must be one of the entries returned
  33. * by `Report.getReportList()`
  34. * @method add
  35. * @param {String} fmt the format of the report to generate
  36. */
  37. add(fmt) {
  38. if (this.reports[fmt]) {
  39. // already added
  40. return;
  41. }
  42. const config = this.config;
  43. const rptConfig = config.reporting.reportConfig()[fmt] || {};
  44. rptConfig.verbose = config.verbose;
  45. try {
  46. if (this.config.verbose) {
  47. console.error('Create report', fmt, ' with', rptConfig);
  48. }
  49. this.reports[fmt] = libReports.create(fmt, rptConfig);
  50. } catch (ex) {
  51. throw inputError.create('Invalid report format [' + fmt + ']');
  52. }
  53. },
  54. /**
  55. * adds an array of report formats to be generated
  56. * @method addAll
  57. * @param {Array} fmts an array of report formats
  58. */
  59. addAll(fmts) {
  60. fmts.forEach(f => {
  61. this.add(f);
  62. });
  63. },
  64. /**
  65. * writes all reports added
  66. * @method write
  67. */
  68. write(coverageMap, opts) {
  69. opts = opts || {};
  70. const sourceFinder = opts.sourceFinder || null;
  71. const context = libReport.createContext({
  72. dir: this.dir,
  73. watermarks: this.config.reporting.watermarks(),
  74. sourceFinder
  75. });
  76. const excludes = this.config.instrumentation.excludes() || [];
  77. coverageMap.filter(
  78. file =>
  79. !excludes.some(exclude =>
  80. minimatch(file, exclude, { dot: true })
  81. )
  82. );
  83. const tree = this.summarizer(coverageMap);
  84. Object.keys(this.reports).forEach(name => {
  85. const report = this.reports[name];
  86. tree.visit(report, context);
  87. });
  88. }
  89. };
  90. module.exports = Reporter;