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 objectUtils = require('../object-utils'),
8 Writer = require('../util/file-writer'),
9 util = require('util'),
10 Report = require('./index');
12 * a `Report` implementation that produces a coverage JSON object with summary info only.
17 * var report = require('istanbul').Report.create('json-summary');
20 * @class JsonSummaryReport
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()`
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;
33 JsonSummaryReport.TYPE = 'json-summary';
34 util.inherits(JsonSummaryReport, Report);
36 Report.mix(JsonSummaryReport, {
37 synopsis: function () {
38 return 'prints a summary coverage object as JSON to a file';
40 getDefaultConfig: function () {
42 file: 'coverage-summary.json'
45 writeReport: function (collector, sync) {
46 var outputFile = path.resolve(this.opts.dir, this.opts.file),
47 writer = this.opts.writer || new Writer(sync),
52 collector.files().forEach(function (file) {
53 summaries.push(objectUtils.summarizeFileCoverage(collector.fileCoverageFor(file)));
55 finalSummary = objectUtils.mergeSummaryObjects.apply(null, summaries);
57 writer.on('done', function () { that.emit('done'); });
58 writer.writeFile(outputFile, function (contentWriter) {
59 contentWriter.println("{");
60 contentWriter.write('"total":');
61 contentWriter.write(JSON.stringify(finalSummary));
63 collector.files().forEach(function (key) {
64 contentWriter.println(",");
65 contentWriter.write(JSON.stringify(key));
66 contentWriter.write(":");
67 contentWriter.write(JSON.stringify(objectUtils.summarizeFileCoverage(collector.fileCoverageFor(key))));
69 contentWriter.println("}");
75 module.exports = JsonSummaryReport;