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