-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (128 loc) · 4.38 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
'use strict';
function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
var rimraf = require('rimraf');
var onExit = require('signal-exit');
var stripBom = require('strip-bom');
var xtend = require("xtend");
var internals = {};
var PHL = {};
internals.defaultOpts = {
subprocessBin: path.resolve(__dirname, './bin/PHL.js'),
tempDirectory: './.phl_output',
cwd: process.env.PHL_CWD || process.cwd(),
reporter: 'text',
istanbul: require('istanbul')
};
internals.getPkgConfig = function getPkgConfig() {
var getConfigObj = require(path.resolve(internals.config.cwd, './package.json')).config || {};
return getConfigObj.phl;
};
internals.toList = function (xs) {
return Array.isArray(xs) ? xs : [xs];
};
internals.createRegexPaths = function (list) {
return internals.toList(list).map(function (path) {
return new RegExp(path);
});
};
internals.createInstrumenter = function () {
return new internals.config.istanbul.Instrumenter();
};
internals.addFile = function addFile(filename) {
var instrument = true;
var relFile = path.relative(internals.config.cwd, filename);
PHL.exclude.forEach(function (exclude) {
if (exclude.test(filename)) {
instrument = false;
}
});
PHL.include.forEach(function (include) {
if (include.test(filename)) {
instrument = true;
}
});
var content = stripBom(fs.readFileSync(filename, 'utf8'));
if (instrument) {
content = internals.instrumenter.instrumentSync(content, './' + relFile);
}
return {
instrument: instrument,
content: content,
relFile: relFile
};
};
internals.wrapRequire = function wrapRequire() {
require.extensions['.js'] = function (module, filename) {
var obj = internals.addFile(filename);
module._compile(obj.content, filename);
};
};
internals.createOutputDirectory = function () {
return mkdirp.sync(internals.tmpDirectory());
};
internals.wrapExit = function wrapExit() {
onExit(function () {
internals.writeCoverageFile();
}, {
alwaysLast: true
});
};
internals.writeCoverageFile = function writeCoverageFile() {
var coverage = global.__coverage__;
if ((typeof __coverage__ === 'undefined' ? 'undefined' : _typeof(__coverage__)) === 'object') {
coverage = __coverage__;
}
if (!coverage) {
return;
}
fs.writeFileSync(path.resolve(internals.tmpDirectory(), './', process.pid + '.json'), JSON.stringify(coverage), 'utf-8');
};
internals.loadReports = function loadReports() {
var files = fs.readdirSync(internals.tmpDirectory());
return files.map(function (f) {
try {
return JSON.parse(fs.readFileSync(path.resolve(internals.tmpDirectory(), './', f), 'utf-8'));
} catch (e) {
return {};
}
});
};
internals.tmpDirectory = function () {
return path.resolve(internals.config.cwd, './', internals.config.tempDirectory);
};
PHL.init = function init(opts) {
internals.config = xtend(internals.defaultOpts, opts);
var phlConfig = internals.getPkgConfig();
internals.reporterList = internals.toList(internals.config.reporter);
internals.exclude = phlConfig.exclude || ['node_modules/', 'test/', 'test.js'];
internals.include = phlConfig.include || [];
PHL.exclude = internals.createRegexPaths(internals.exclude);
PHL.include = internals.createRegexPaths(internals.include);
internals.instrumenter = internals.createInstrumenter();
internals.createOutputDirectory();
};
PHL.cleanup = function cleanup() {
if (!process.env.PHL_CWD) {
rimraf.sync(internals.tmpDirectory());
}
};
PHL.wrap = function wrap() {
internals.wrapRequire();
internals.wrapExit();
};
PHL.report = function report(next, _collector, _reporter) {
next = next || function () {};
var collector = _collector || new internals.config.istanbul.Collector();
var reporter = _reporter || new internals.config.istanbul.Reporter();
internals.loadReports().forEach(function (report) {
collector.add(report);
});
internals.reporterList.forEach(function (porter) {
reporter.add(porter);
});
reporter.write(collector, true, next);
};
module.exports = PHL;