-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathhelpers.js
163 lines (147 loc) · 5.27 KB
/
helpers.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
exports.init = function(grunt) {
'use strict';
var chalk = require('chalk');
var fs = require('fs');
var path = require('path');
var flow = require('nue').flow;
var as = require('nue').as;
var istanbul = require('istanbul');
function flowEnd(err, done) {
if (err) {
grunt.fail.fatal(err);
}
done();
}
function makeReporters(options) {
var result = [];
var reporters = options.reporters &&
typeof options.reporters === 'object' ? options.reporters : {};
Object.keys(reporters).forEach(function(n) {
if(reporters[n]) {
result.push({ type : n, options : reporters[n] });
}
});
var append = function(t) {
if(t && !reporters[t]) {
result.push({ type : t, options : options});
reporters[t] = true;
}
};
if (Array.isArray(options.type)) {
options.type.forEach(append);
} else {
append(options.type);
}
var mapping = {
'none' : [],
'detail': ['text'],
'both' : ['text', 'text-summary']
};
var a = mapping[options.print];
if(a) {
a.forEach(append);
} else {
append('text-summary');
}
return result;
}
return {
instrument : function(files, options, done) {
var outFile = function(file) {
return path.join(options.basePath, options.flatten === true ? path.basename(file) : file);
};
var getRelativePath = function (file) {
var cwd = options.cwd || '';
return path.join(cwd, file);
};
var tally = { instrumented : 0, skipped : 0 };
var instFlow = flow(
function instrumentFile(f) {
var code = grunt.file.read(getRelativePath(f.name));
var instrumenter = options.instrumenter ? new options.instrumenter(options) : new istanbul.Instrumenter(options);
instrumenter.instrument(code, getRelativePath(f.name), this.async({
name : f.name,
code : as(1)
}));
}, function write(result) {
var out = outFile(result.name);
grunt.file.write(out, result.code);
tally.instrumented++;
this.next();
}, function end() {
flowEnd(this.err, this.next.bind(this));
});
var dateCheckFlow = flow(
function readStat(f) {
if (grunt.file.exists(getRelativePath(f.name)) && grunt.file.exists(outFile(f.name))) {
grunt.log.debug('reading stat for ' + f.name);
fs.stat(getRelativePath(f.name), this.async({ name : f.name, stat : as(1) }));
fs.stat(outFile(f.name), this.async({ name : f.name, stat : as(1) }));
} else {
grunt.verbose.writeln('instrumented file does not exist ' + f.name);
this.end({ name : f.name, instrument : true });
}
}, function decision(i, o) {
var reinstrument = i.stat.mtime.getTime() > o.stat.mtime.getTime();
grunt.log.debug('make a decision about instrumenting ' + i.name + ': ' + reinstrument);
this.end({ name: i.name, instrument: reinstrument });
}, function end(f) {
grunt.log.debug(this.err);
if (f.instrument) {
this.exec(instFlow, { name : f.name }, this.async());
} else {
tally.skipped++;
flowEnd(this.err, this.next.bind(this));
}
});
flow(function(filelist) {
this.asyncEach(filelist, function(file, group) {
this.exec((options.lazy ? dateCheckFlow : instFlow), { name : file }, group.async(as(1)));
});
}, function outputSummary() {
grunt.log.write('Instrumented ' + chalk.cyan(tally.instrumented) + ' ' +
grunt.util.pluralize(tally.instrumented, 'file/files'));
if (options.lazy) {
grunt.log.write(' (skipped ' + chalk.cyan(tally.skipped) + ' ' +
grunt.util.pluralize(tally.skipped, 'file/files') + ')');
}
grunt.log.writeln();
this.next();
}, done)(files);
},
addUncoveredFiles: function(coverage, options, allFiles){
var instrumenter = new istanbul.Instrumenter({coverageVariable: options.coverageVar , preserveComments: false});
var transformer = instrumenter.instrumentSync.bind(instrumenter);
allFiles.forEach(function (file) {
if (!coverage[file]) {
transformer(fs.readFileSync(file, 'utf-8'), file);
coverage[file] = instrumenter.coverState;
}
});
},
storeCoverage : function(coverage, options, done) {
flow(function write_json(cov) {
var json = path.resolve(options.dir, options.json);
grunt.file.write(json, JSON.stringify(cov));
this.next();
}, function() {
flowEnd(this.err, done);
})(coverage);
},
makeReport : function(files, options, done) {
flow(function(filelist) {
var collector = new istanbul.Collector();
filelist.forEach(function(file) {
collector.add(grunt.file.readJSON(file));
});
makeReporters(options).forEach(function(repoDef) {
var reporter = istanbul.Report.create(repoDef.type, repoDef.options);
reporter.writeReport(collector, true);
});
this.next();
}, function() {
flowEnd(this.err, done);
})(files);
}
};
};