UNPKG

4.69 kBJavaScriptView Raw
1/*
2 Copyright (c) 2012, Yahoo! Inc. All rights reserved.
3 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
4 */
5
6var nopt = require('nopt'),
7 Report = require('../report'),
8 Reporter = require('../reporter'),
9 path = require('path'),
10 fs = require('fs'),
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');
19
20function ReportCommand() {
21 Command.call(this);
22}
23
24ReportCommand.TYPE = 'report';
25util.inherits(ReportCommand, Command);
26
27function 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('**********************************************************************');
33}
34
35Command.mix(ReportCommand, {
36 synopsis: function () {
37 return "writes reports for coverage JSON objects produced in a previous run";
38 },
39
40 usage: function () {
41 console.error('\nUsage: ' + this.toolName() + ' ' + this.type() + ' <options> [ <format> ... ]\n\nOptions are:\n\n' +
42 [
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 glob pattern to select one or more coverage files, defaults to **/coverage*.json'),
47 formatOption('--verbose, -v', 'verbose mode')
48 ].join('\n\n'));
49
50 console.error('\n');
51 console.error('<format> is one of ');
52 Report.getReportList().forEach(function (name) {
53 console.error(formatOption(name, Report.create(name).synopsis()));
54 });
55 console.error("");
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.'
60 ].join(' ')));
61 console.error('\n');
62 },
63
64 run: function (args, callback) {
65
66 var template = {
67 config: path,
68 root: path,
69 dir: path,
70 include: String,
71 verbose: Boolean
72 },
73 opts = nopt(template, { v : '--verbose' }, args, 0),
74 includePattern = opts.include || '**/coverage*.json',
75 root,
76 collector = new Collector(),
77 config = configuration.loadFile(opts.config, {
78 verbose: opts.verbose,
79 reporting: {
80 dir: opts.dir
81 }
82 }),
83 formats = opts.argv.remain,
84 reporter = new Reporter(config);
85
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]);
92 }
93 // End: backward compatible processing
94
95 if (formats.length === 0) {
96 formats = config.reporting.reports();
97 }
98 if (formats.length === 0) {
99 formats = [ 'lcov' ];
100 }
101 reporter.addAll(formats);
102
103 root = opts.root || process.cwd();
104 filesFor({
105 root: root,
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);
112 });
113 reporter.write(collector, false, function (err) {
114 console.log('Done');
115 return callback(err);
116 });
117 });
118 }
119});
120
121module.exports = ReportCommand;
122
123