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 path = require('path'),
7 Writer = require('../util/file-writer'),
8 util = require('util'),
9 Report = require('./index');
11 * a `Report` implementation that produces a coverage JSON object.
16 * var report = require('istanbul').Report.create('json');
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()`
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;
32 JsonReport.TYPE = 'json';
33 util.inherits(JsonReport, Report);
35 Report.mix(JsonReport, {
36 synopsis: function () {
37 return 'prints the coverage object as JSON to a file';
39 getDefaultConfig: function () {
41 file: 'coverage-final.json'
44 writeReport: function (collector, sync) {
45 var outputFile = path.resolve(this.opts.dir, this.opts.file),
46 writer = this.opts.writer || new Writer(sync),
49 writer.on('done', function () { that.emit('done'); });
50 writer.writeFile(outputFile, function (contentWriter) {
52 contentWriter.println("{");
53 collector.files().forEach(function (key) {
57 contentWriter.println(",");
59 contentWriter.write(JSON.stringify(key));
60 contentWriter.write(":");
61 contentWriter.write(JSON.stringify(collector.fileCoverageFor(key)));
63 contentWriter.println("}");
69 module.exports = JsonReport;