Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80680 views
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
6
var path = require('path'),
7
objectUtils = require('../object-utils'),
8
Writer = require('../util/file-writer'),
9
util = require('util'),
10
Report = require('./index');
11
/**
12
* a `Report` implementation that produces a coverage JSON object with summary info only.
13
*
14
* Usage
15
* -----
16
*
17
* var report = require('istanbul').Report.create('json-summary');
18
*
19
*
20
* @class JsonSummaryReport
21
* @extends Report
22
* @module report
23
* @constructor
24
* @param {Object} opts optional
25
* @param {String} [opts.dir] the directory in which to write the `coverage-summary.json` file. Defaults to `process.cwd()`
26
*/
27
function JsonSummaryReport(opts) {
28
this.opts = opts || {};
29
this.opts.dir = this.opts.dir || process.cwd();
30
this.opts.file = this.opts.file || this.getDefaultConfig().file;
31
this.opts.writer = this.opts.writer || null;
32
}
33
JsonSummaryReport.TYPE = 'json-summary';
34
util.inherits(JsonSummaryReport, Report);
35
36
Report.mix(JsonSummaryReport, {
37
synopsis: function () {
38
return 'prints a summary coverage object as JSON to a file';
39
},
40
getDefaultConfig: function () {
41
return {
42
file: 'coverage-summary.json'
43
};
44
},
45
writeReport: function (collector, sync) {
46
var outputFile = path.resolve(this.opts.dir, this.opts.file),
47
writer = this.opts.writer || new Writer(sync),
48
that = this;
49
50
writer.on('done', function () { that.emit('done'); });
51
writer.writeFile(outputFile, function (contentWriter) {
52
var first = true;
53
contentWriter.println("{");
54
collector.files().forEach(function (key) {
55
if (first) {
56
first = false;
57
} else {
58
contentWriter.println(",");
59
}
60
contentWriter.write(JSON.stringify(key));
61
contentWriter.write(":");
62
contentWriter.write(JSON.stringify(objectUtils.summarizeFileCoverage(collector.fileCoverageFor(key))));
63
});
64
contentWriter.println("}");
65
});
66
writer.done();
67
}
68
});
69
70
module.exports = JsonSummaryReport;
71
72