2 Copyright (c) 2012, Yahoo! Inc. All rights reserved.
3 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
6 var nopt = require('nopt'),
7 Report = require('../report'),
8 Reporter = require('../reporter'),
9 path = require('path'),
11 Collector = require('../collector'),
12 helpFormatter = require('../util/help-formatter'),
13 formatOption = helpFormatter.formatOption,
14 formatPara = helpFormatter.formatPara,
15 filesFor = require('../util/file-matcher').filesFor,
16 util = require('util'),
17 Command = require('./index'),
18 configuration = require('../config');
20 function ReportCommand() {
24 ReportCommand.TYPE = 'report';
25 util.inherits(ReportCommand, Command);
27 function printDeprecationMessage(pat, fmt) {
28 console.error('**********************************************************************');
29 console.error('DEPRECATION WARNING! You are probably using the old format of the report command');
30 console.error('This will stop working soon, see `istanbul help report` for the new command format');
31 console.error('Assuming you meant: istanbul report --include=' + pat + ' ' + fmt);
32 console.error('**********************************************************************');
35 Command.mix(ReportCommand, {
36 synopsis: function () {
37 return "writes reports for coverage JSON objects produced in a previous run";
41 console.error('\nUsage: ' + this.toolName() + ' ' + this.type() + ' <options> [ <format> ... ]\n\nOptions are:\n\n' +
43 formatOption('--config <path-to-config>', 'the configuration file to use, defaults to .istanbul.yml'),
44 formatOption('--root <input-directory>', 'The input root directory for finding coverage files'),
45 formatOption('--dir <report-directory>', 'The output directory where files will be written. This defaults to ./coverage/'),
46 formatOption('--include <glob>', 'The fileset pattern to select one or more coverage files, defaults to **/coverage*.json'),
47 formatOption('--verbose, -v', 'verbose mode')
51 console.error('<format> is one of ');
52 Report.getReportList().forEach(function (name) {
53 console.error(formatOption(name, Report.create(name).synopsis()));
56 console.error(formatPara([
57 'Default format is lcov unless otherwise specified in the config file.',
58 'In addition you can tweak the file names for various reports using the config file.',
59 'Type `istanbul help config` to see what can be tweaked.'
64 run: function (args, callback) {
73 opts = nopt(template, { v : '--verbose' }, args, 0),
74 includePattern = opts.include || '**/coverage*.json',
76 collector = new Collector(),
77 config = configuration.loadFile(opts.config, {
78 verbose: opts.verbose,
83 formats = opts.argv.remain,
84 reporter = new Reporter(config);
86 // Start: backward compatible processing
87 if (formats.length === 2 &&
88 Report.getReportList().indexOf(formats[1]) < 0) {
89 includePattern = formats[1];
90 formats = [ formats[0] ];
91 printDeprecationMessage(includePattern, formats[0]);
93 // End: backward compatible processing
95 if (formats.length === 0) {
96 formats = config.reporting.reports();
98 if (formats.length === 0) {
101 reporter.addAll(formats);
103 root = opts.root || process.cwd();
106 includes: [ includePattern ]
107 }, function (err, files) {
108 if (err) { throw err; }
109 files.forEach(function (file) {
110 var coverageObject = JSON.parse(fs.readFileSync(file, 'utf8'));
111 collector.add(coverageObject);
113 reporter.write(collector, false, function (err) {
115 return callback(err);
121 module.exports = ReportCommand;