-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
66 lines (55 loc) · 1.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var fs = require('fs');
var through = require('through');
var istanbul = require('istanbul');
var _ = require('lodash');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
module.exports = function(opts) {
// set sensible default options
var defaultDir = './coverage/';
opts = _.defaults(opts || {}, {
dir: defaultDir,
reporters: ['text'],
reporterOpts: {}
});
opts.reporterOpts = _.defaults(opts.reporterOpts, {
dir: opts.dir || defaultDir
});
var collector = new istanbul.Collector();
function collectFromFile(file) {
if (file.isNull()) {
return this.emit('error', new PluginError('gulp-istanbul-report', "Couldn't find coverage file " + file.path));
}
// we dont do streams (yet)
if (file.isStream()) {
return this.emit('error', new PluginError('gulp-istanbul-report', 'Streaming not supported'));
}
try {
var coverage = JSON.parse(file.contents);
collector.add(coverage);
} catch (Exception) {
return this.emit('error', new PluginError('gulp-istanbul-report', "Couldn't read coverage file " + file.path));
}
}
function createReports() {
// create reporters
var reporters = opts.reporters.map(function (repOpts) {
if (!_.isObject(repOpts)) {
repOpts = {name: repOpts};
}
repOpts = _.defaults(repOpts, opts.reporterOpts);
return istanbul.Report.create(repOpts.name || 'text', repOpts);
});
// write reports
reporters.forEach(function (report) {
try {
report.writeReport(collector, true);
}
catch (Exception) {
return this.emit('error', new PluginError('gulp-istanbul-report', "Couldn't write report file for writer " + report.opts.name));
}
}.bind(this));
this.emit('end');
}
return through(collectFromFile, createReports);
};