/*1Copyright (c) 2014, Yahoo! Inc. All rights reserved.2Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.3*/4var Report = require('./report'),5configuration = require('./config'),6inputError = require('./util/input-error');78/**9* convenience mechanism to write one or more reports ensuring that config10* options are respected.11* Usage12* -----13*14* var fs = require('fs'),15* reporter = new require('istanbul').Reporter(),16* collector = new require('istanbul').Collector(),17* sync = true;18*19* collector.add(JSON.parse(fs.readFileSync('coverage.json', 'utf8')));20* reporter.add('lcovonly');21* reporter.addAll(['clover', 'cobertura']);22* reporter.write(collector, sync, function () { console.log('done'); });23*24* @class Reporter25* @param {Configuration} cfg the config object, a falsy value will load the26* default configuration instead27* @param {String} dir the directory in which to write the reports, may be falsy28* to use config or global defaults29* @constructor30* @module main31*/32function Reporter(cfg, dir) {33this.config = cfg || configuration.loadFile();34this.dir = dir || this.config.reporting.dir();35this.reports = {};36}3738Reporter.prototype = {39/**40* adds a report to be generated. Must be one of the entries returned41* by `Report.getReportList()`42* @method add43* @param {String} fmt the format of the report to generate44*/45add: function (fmt) {46if (this.reports[fmt]) { // already added47return;48}49var config = this.config,50rptConfig = config.reporting.reportConfig()[fmt] || {};51rptConfig.verbose = config.verbose;52rptConfig.dir = this.dir;53rptConfig.watermarks = config.reporting.watermarks();54try {55this.reports[fmt] = Report.create(fmt, rptConfig);56} catch (ex) {57throw inputError.create('Invalid report format [' + fmt + ']');58}59},60/**61* adds an array of report formats to be generated62* @method addAll63* @param {Array} fmts an array of report formats64*/65addAll: function (fmts) {66var that = this;67fmts.forEach(function (f) {68that.add(f);69});70},71/**72* writes all reports added and calls the callback when done73* @method write74* @param {Collector} collector the collector having the coverage data75* @param {Boolean} sync true to write reports synchronously76* @param {Function} callback the callback to call when done. When `sync`77* is true, the callback will be called in the same process tick.78*/79write: function (collector, sync, callback) {80var reports = this.reports,81verbose = this.config.verbose,82handler = this.handleDone.bind(this, callback);8384this.inProgress = Object.keys(reports).length;8586Object.keys(reports).forEach(function (name) {87var report = reports[name];88if (verbose) {89console.error('Write report: ' + name);90}91report.on('done', handler);92report.writeReport(collector, sync);93});94},95/*96* handles listening on all reports to be completed before calling the callback97* @method handleDone98* @private99* @param {Function} callback the callback to call when all reports are100* written101*/102handleDone: function (callback) {103this.inProgress -= 1;104if (this.inProgress === 0) {105return callback();106}107}108};109110module.exports = Reporter;111112113