index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. 'use strict';
  6. const { ReportBase } = require('istanbul-lib-report');
  7. class JsonSummaryReport extends ReportBase {
  8. constructor(opts) {
  9. super();
  10. this.file = opts.file || 'coverage-summary.json';
  11. this.contentWriter = null;
  12. this.first = true;
  13. }
  14. onStart(root, context) {
  15. this.contentWriter = context.writer.writeFile(this.file);
  16. this.contentWriter.write('{');
  17. }
  18. writeSummary(filePath, sc) {
  19. const cw = this.contentWriter;
  20. if (this.first) {
  21. this.first = false;
  22. } else {
  23. cw.write(',');
  24. }
  25. cw.write(JSON.stringify(filePath));
  26. cw.write(': ');
  27. cw.write(JSON.stringify(sc));
  28. cw.println('');
  29. }
  30. onSummary(node) {
  31. if (!node.isRoot()) {
  32. return;
  33. }
  34. this.writeSummary('total', node.getCoverageSummary());
  35. }
  36. onDetail(node) {
  37. this.writeSummary(
  38. node.getFileCoverage().path,
  39. node.getCoverageSummary()
  40. );
  41. }
  42. onEnd() {
  43. const cw = this.contentWriter;
  44. cw.println('}');
  45. cw.close();
  46. }
  47. }
  48. module.exports = JsonSummaryReport;