2 Copyright (c) 2014, Yahoo! Inc. All rights reserved.
3 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
5 var Report = require('./report'),
6 configuration = require('./config'),
7 inputError = require('./util/input-error');
10 * convenience mechanism to write one or more reports ensuring that config
11 * options are respected.
15 * var fs = require('fs'),
16 * reporter = new require('istanbul').Reporter(),
17 * collector = new require('istanbul').Collector(),
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'); });
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
33 function Reporter(cfg, dir) {
34 this.config = cfg || configuration.loadFile();
35 this.dir = dir || this.config.reporting.dir();
39 Reporter.prototype = {
41 * adds a report to be generated. Must be one of the entries returned
42 * by `Report.getReportList()`
44 * @param {String} fmt the format of the report to generate
47 if (this.reports[fmt]) { // already added
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();
56 this.reports[fmt] = Report.create(fmt, rptConfig);
58 throw inputError.create('Invalid report format [' + fmt + ']');
62 * adds an array of report formats to be generated
64 * @param {Array} fmts an array of report formats
66 addAll: function (fmts) {
68 fmts.forEach(function (f) {
73 * writes all reports added and calls the callback when done
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.
80 write: function (collector, sync, callback) {
81 var reports = this.reports,
82 verbose = this.config.verbose,
83 handler = this.handleDone.bind(this, callback);
85 this.inProgress = Object.keys(reports).length;
87 Object.keys(reports).forEach(function (name) {
88 var report = reports[name];
90 console.error('Write report: ' + name);
92 report.on('done', handler);
93 report.writeReport(collector, sync);
97 * handles listening on all reports to be completed before calling the callback
100 * @param {Function} callback the callback to call when all reports are
103 handleDone: function (callback) {
104 this.inProgress -= 1;
105 if (this.inProgress === 0) {
111 module.exports = Reporter;