1 | /*
|
2 | Copyright (c) 2014, Yahoo! Inc. All rights reserved.
|
3 | Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
|
4 | */
|
5 | var Report = require('./report'),
|
6 | configuration = require('./config'),
|
7 | inputError = require('./util/input-error');
|
8 |
|
9 | /**
|
10 | * convenience mechanism to write one or more reports ensuring that config
|
11 | * options are respected.
|
12 | * Usage
|
13 | * -----
|
14 | *
|
15 | * var fs = require('fs'),
|
16 | * reporter = new require('istanbul').Reporter(),
|
17 | * collector = new require('istanbul').Collector(),
|
18 | * sync = true;
|
19 | *
|
20 | * collector.add(JSON.parse(fs.readFileSync('coverage.json', 'utf8')));
|
21 | * reporter.add('lcovonly');
|
22 | * reporter.addAll(['clover', 'cobertura']);
|
23 | * reporter.write(collector, sync, function () { console.log('done'); });
|
24 | *
|
25 | * @class Reporter
|
26 | * @param {Configuration} cfg the config object, a falsy value will load the
|
27 | * default configuration instead
|
28 | * @param {String} dir the directory in which to write the reports, may be falsy
|
29 | * to use config or global defaults
|
30 | * @constructor
|
31 | * @module main
|
32 | */
|
33 | function Reporter(cfg, dir) {
|
34 | this.config = cfg || configuration.loadFile();
|
35 | this.dir = dir || this.config.reporting.dir();
|
36 | this.reports = {};
|
37 | }
|
38 |
|
39 | Reporter.prototype = {
|
40 | /**
|
41 | * adds a report to be generated. Must be one of the entries returned
|
42 | * by `Report.getReportList()`
|
43 | * @method add
|
44 | * @param {String} fmt the format of the report to generate
|
45 | */
|
46 | add: function (fmt) {
|
47 | if (this.reports[fmt]) { // already added
|
48 | return;
|
49 | }
|
50 | var config = this.config,
|
51 | rptConfig = config.reporting.reportConfig()[fmt] || {};
|
52 | rptConfig.verbose = config.verbose;
|
53 | rptConfig.dir = this.dir;
|
54 | rptConfig.watermarks = config.reporting.watermarks();
|
55 | try {
|
56 | this.reports[fmt] = Report.create(fmt, rptConfig);
|
57 | } catch (ex) {
|
58 | throw inputError.create('Invalid report format [' + fmt + ']');
|
59 | }
|
60 | },
|
61 | /**
|
62 | * adds an array of report formats to be generated
|
63 | * @method addAll
|
64 | * @param {Array} fmts an array of report formats
|
65 | */
|
66 | addAll: function (fmts) {
|
67 | var that = this;
|
68 | fmts.forEach(function (f) {
|
69 | that.add(f);
|
70 | });
|
71 | },
|
72 | /**
|
73 | * writes all reports added and calls the callback when done
|
74 | * @method write
|
75 | * @param {Collector} collector the collector having the coverage data
|
76 | * @param {Boolean} sync true to write reports synchronously
|
77 | * @param {Function} callback the callback to call when done. When `sync`
|
78 | * is true, the callback will be called in the same process tick.
|
79 | */
|
80 | write: function (collector, sync, callback) {
|
81 | var reports = this.reports,
|
82 | verbose = this.config.verbose,
|
83 | handler = this.handleDone.bind(this, callback);
|
84 |
|
85 | this.inProgress = Object.keys(reports).length;
|
86 |
|
87 | Object.keys(reports).forEach(function (name) {
|
88 | var report = reports[name];
|
89 | if (verbose) {
|
90 | console.error('Write report: ' + name);
|
91 | }
|
92 | report.on('done', handler);
|
93 | report.writeReport(collector, sync);
|
94 | });
|
95 | },
|
96 | /*
|
97 | * handles listening on all reports to be completed before calling the callback
|
98 | * @method handleDone
|
99 | * @private
|
100 | * @param {Function} callback the callback to call when all reports are
|
101 | * written
|
102 | */
|
103 | handleDone: function (callback) {
|
104 | this.inProgress -= 1;
|
105 | if (this.inProgress === 0) {
|
106 | return callback();
|
107 | }
|
108 | }
|
109 | };
|
110 |
|
111 | module.exports = Reporter;
|