-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
58 lines (48 loc) · 1.5 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
var through = require('through');
var minimatch = require('minimatch');
var objectAssign = require('object-assign');
var defaultIgnore = ['**/node_modules/**', '**/bower_components/**', '**/test/**', '**/tests/**', '**/*.json'];
function shouldIgnoreFile(file, options) {
var ignore = options.defaultIgnore === false ? [] : defaultIgnore;
ignore = ignore.concat(options.ignore || []);
return ignore.some(function(pattern) {
return minimatch(file, pattern, options.minimatchOptions);
});
}
module.exports = function(options, extraOptions) {
var file;
options = options || {};
if (typeof options === 'string') {
file = options;
options = extraOptions || {};
return transform(options, file);
}
return transform.bind(null, options);
};
function transform(options, file) {
if (shouldIgnoreFile(file, options))
return through();
var instrumenterConfig = objectAssign({}, {
autoWrap: true,
coverageVariable: '__coverage__',
embedSource: true,
noCompact: false,
preserveComments: true,
produceSourceMap: true
}, options.instrumenterConfig);
var instrumenter = (options.instrumenter || require('istanbul-lib-instrument')).createInstrumenter(instrumenterConfig);
var data = '';
return through(function(buf) {
data += buf;
}, function() {
var self = this;
instrumenter.instrument(data, file, function(err, code) {
if (!err) {
self.queue(code);
} else {
self.emit('error', err);
}
self.queue(null);
});
});
}