-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
180 lines (157 loc) · 5.73 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict';
var gulp = require('gulp');
var fs = require('fs');
var babel = require('babel-core');
var coverage = require('istanbul-lib-coverage');
var report = require('istanbul-lib-report');
var reporters = require('istanbul-reports');
var sourceCache = undefined;
var finalSummary = undefined;
var sourceMapCache = {};
var coverageVariable = '__coverage__';
var pluginName = 'gulp-jsx-coverage';
// Never use node-jsx or other transform in your testing code!
var initModuleLoader = function (options) {
var Module = require('module');
var babelFiles = Object.assign({
include: /\.jsx?$/,
exclude: /node_modules/,
omitExt: false
}, options.babel);
var istanbulCfg = Object.assign({
exclude: /node_modules/
}, options.istanbul);
var moduleLoader = function (module, filename) {
var srcCache = sourceCache[filename];
var src = srcCache || fs.readFileSync(filename, {encoding: 'utf8'});
var bbl;
var cov;
var tmp;
if (srcCache) {
return;
}
bbl = (filename.match(babelFiles.include) && !filename.match(babelFiles.exclude));
cov = !filename.match(istanbulCfg.exclude);
if (bbl || cov) {
try {
tmp = babel.transform(src, Object.assign({
filename: filename,
babelrc: bbl,
sourceMap: 'both'
}, cov ? {plugins: [['istanbul', {include: '*', exclude: '/_NOT_ME_'}]]} : {}));
srcCache = tmp.map || 1;
src = tmp.code;
} catch (e) {
throw new Error('Error when transform es2015/jsx ' + filename + ': ' + e.toString());
}
}
if (srcCache) {
sourceCache[filename] = src;
sourceMapCache[filename] = srcCache;
}
module._compile(src, filename);
};
global[coverageVariable] = {};
sourceCache = {};
sourceMapCache = {};
Module._extensions['.js'] = moduleLoader;
if (babelFiles.omitExt) {
babelFiles.omitExt.forEach(function (V) {
Module._extensions[V] = moduleLoader;
});
}
try {
require('source-map-support').install({
handleUncaughtExceptions: false,
environment : 'node',
retrieveSourceMap: function (source) {
var map = sourceMapCache[source];
return map ? {
url: null,
map: map
} : null;
}
});
} catch (E) {
console.warn('Can not install "source-map-support" .');
}
};
var GJC = {
initModuleLoader: function (options) {
initModuleLoader(options);
},
collectIstanbulCoverage: function (options) {
return function () {
var map;
var context;
var tree;
var covCfg = Object.assign({
directory: 'coverage',
reporters: ['text']
}, options.coverage);
if (!global[coverageVariable]) {
this.emit('error', new (require('gulp-util').PluginError)({
plugin: pluginName,
message: 'No coverage info! Check your options.src or options.istanbul.exclude to ensure you include proper files'
}));
}
if (!covCfg.reporters || !covCfg.reporters.forEach) {
this.emit('error', new (require('gulp-util').PluginError)({
plugin: pluginName,
message: 'Bad config! Check your options.coverage.reports, it should be an array.'
}));
}
context = report.createContext({
dir: covCfg.directory
});
map = coverage.createCoverageMap(global[coverageVariable]);
tree = report.summarizers.pkg(map);
finalSummary = coverage.createCoverageSummary();
map.files().forEach(function (F) {
finalSummary.merge(map.fileCoverageFor(F).toSummary());
});
covCfg.reporters.forEach(function (R) {
try {
tree.visit(reporters.create(R), context);
} catch (E) {
this.emit('error', new (require('gulp-util').PluginError)({
plugin: pluginName,
message: 'ERROR when generate instanbul report ' + R + ':' + E.message
}));
}
});
if ('function' === (typeof options.cleanup)) {
options.cleanup(this);
}
if (options.threshold && ('function' === (typeof options.threshold.forEach))) {
options.threshold.forEach(function (O) {
GJC.failWithThreshold(O.min, O.type).apply(this);
}.bind(this));
}
};
},
failWithThreshold: function (threshold, type) {
return function () {
var T = type || 'lines';
if (!finalSummary || !threshold) {
return;
}
if (finalSummary[T].pct < threshold) {
this.emit('error', new (require('gulp-util').PluginError)({
plugin: pluginName,
message: T + ' coverage ' + finalSummary[T].pct + '% is lower than threshold ' + threshold + '%!'
}));
}
};
},
createTask: function (options) {
return function () {
GJC.initModuleLoader(options);
return gulp.src(options.src)
.pipe(require('gulp-mocha')(options.mocha))
.on('end', GJC.collectIstanbulCoverage(options));
};
}
};
module.exports = GJC;
require('object.assign').shim();