-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (37 loc) · 1.08 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
/**
* Import(s)
*/
var path = require('path');
var fs = require('fs');
var read = fs.readFileSync;
var write = fs.writeFileSync;
var debug = require('debug')('component:jscoverage');
var generateJsCoverage = require('jscoverage').process;
/**
* Export(s)
*/
module.exports = plugin;
function plugin (builder) {
builder.hook('before scripts', function (pkg, next) {
if (!pkg.root) return next();
var scripts = pkg.config.scripts;
debug('before scripts: %j', scripts);
if (!scripts) return next();
var mapper = {};
scripts.forEach(function (script) {
var ext = path.extname(script);
if (ext !== '.js') return;
var script_path = pkg.path(script);
var str = read(script_path, 'utf8').toString();
var content = generateJsCoverage(script_path, str);
mapper[script] = content;
});
debug('after scripts: %j', scripts);
Object.keys(mapper).forEach(function (script) {
pkg.removeFile('scripts', script);
pkg.addFile('scripts', script, mapper[script]);
});
debug('build scripts: %j', scripts);
next();
});
}