This repository has been archived by the owner on Dec 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
cli.js
244 lines (199 loc) · 5.87 KB
/
cli.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//
import {existsSync, writeFileSync, statSync, readdirSync} from 'fs';
import path from 'path';
import Module from 'module';
//
import which from 'which';
import mkdirp from 'mkdirp';
import partial from 'lodash.partial';
import nomnom from 'nomnom';
import {hook, Collector, Reporter, matcherFor, config as configuration} from 'istanbul';
import {Instrumenter} from './instrumenter';
//
//
nomnom.command('cover')
.help("transparently adds coverage information to a node command. Saves coverage.json and reports at the end of execution")
.option('cmd', {
required: true,
position: 1,
help: 'ES6 js files to cover (using babel)'
})
.option('config', {
metavar: '<path-to-config>',
help: 'the configuration file to use, defaults to .istanbul.yml'
})
.option('report', {
default: 'lcv',
metavar: '<format>',
list: true,
help: `report format, defaults to ['lcv']`
})
.option('root', {
metavar: '<path>',
help: 'the root path to look for files to instrument, defaults to .'
})
.option('include', {
default: ['**/*.js'],
metavar: '<include-pattern>',
abbr: 'i',
help: 'one or more fileset patterns e.g. \'**/*.js\''
})
.option('verbose', {
flag: true,
abbr: 'v',
help: 'verbose mode'
})
.callback(opts => {
let args = opts._,
files = [],
cmdArgs = [];
args.forEach(arg => {
let file = lookupFiles(arg);
if (file) files = files.concat(file);
});
opts.include = [opts.include];
opts.include = opts.include.concat(files);
coverCmd(opts);
});
;
nomnom.nom();
function lookupFiles (path) {
if (existsSync(path)) {
let stat = statSync(path);
if (stat.isFile()) return path;
}
}
function callback(err){
if (err){
console.error(err);
process.exit(1);
}
process.exit(0);
}
function coverCmd(opts) {
let config = overrideConfigWith(opts);
let reporter = new Reporter(config);
let { cmd } = opts;
let cmdArgs = opts['--'] || [];
if (!existsSync(cmd)) {
try {
cmd = which.sync(cmd);
} catch (ex) {
return callback(`Unable to resolve file [${cmd}]`);
}
} else {
cmd = path.resolve(cmd);
}
if (opts.verbose) console.error('Isparta options : \n ', opts);
let excludes = config.instrumentation.excludes(true);
enableHooks();
////
function overrideConfigWith(opts){
let overrides = {
verbose: opts.verbose,
instrumentation: {
root: opts.root,
'default-excludes': opts['default-excludes'],
excludes: opts.x,
'include-all-sources': opts['include-all-sources'],
'preload-sources': opts['preload-sources']
},
reporting: {
reports: opts.report,
print: opts.print,
dir: opts.dir
},
hooks: {
'hook-run-in-context': opts['hook-run-in-context'],
'post-require-hook': opts['post-require-hook'],
'handle-sigint': opts['handle-sigint']
}
};
return configuration.loadFile(opts.config, overrides);
}
function enableHooks() {
opts.reportingDir = path.resolve(config.reporting.dir());
mkdirp.sync(opts.reportingDir);
reporter.addAll(config.reporting.reports());
if (config.reporting.print() !== 'none') {
switch (config.reporting.print()) {
case 'detail':
reporter.add('text');
break;
case 'both':
reporter.add('text');
reporter.add('text-summary');
break;
default:
reporter.add('text-summary');
break;
}
}
excludes.push(path.relative(process.cwd(), path.join(opts.reportingDir, '**', '*')));
matcherFor({
root: config.instrumentation.root() || process.cwd(),
includes: opts.include,
excludes: excludes
}, (err, matchFn) => {
if (err){
return callback(err);
}
prepareCoverage(matchFn);
runCommandFn();
});
}
function prepareCoverage(matchFn) {
let coverageVar = `$$cov_${Date.now()}$$`;
let instrumenter = new Instrumenter({ coverageVariable : coverageVar });
let transformer = instrumenter.instrumentSync.bind(instrumenter);
hook.hookRequire(matchFn, transformer, { verbose : opts.verbose });
global[coverageVar] = {};
if (config.hooks.handleSigint()) {
process.once('SIGINT', process.exit);
}
process.once('exit', () => {
let file = path.resolve(opts.reportingDir, 'coverage.json');
let cov, collector;
if (typeof global[coverageVar] === 'undefined' || Object.keys(global[coverageVar]).length === 0) {
console.error('No coverage information was collected, exit without writing coverage information');
return;
} else {
cov = global[coverageVar];
}
mkdirp.sync(opts.reportingDir);
if (config.reporting.print() !== 'none') {
console.error(Array(80 + 1).join('='));
console.error(`Writing coverage object [${file}]`);
}
writeFileSync(file, JSON.stringify(cov), 'utf8');
collector = new Collector();
collector.add(cov);
if (config.reporting.print() !== 'none') {
console.error(`Writing coverage reports at [${opts.reportingDir}]`);
console.error(Array(80 + 1).join('='));
}
reporter.write(collector, true, callback);
});
if (config.instrumentation.includeAllSources()) {
matchFn.files.forEach(function (file) {
if (opts.verbose) { console.error('Preload ' + file); }
try {
require(file);
} catch (ex) {
if (opts.verbose) {
console.error('Unable to preload ' + file);
}
// swallow
}
});
}
}
function runCommandFn() {
process.argv = ["node", cmd].concat(cmdArgs);
if (opts.verbose) {
console.log('Running: ' + process.argv.join(' '));
}
process.env.running_under_istanbul=1;
Module.runMain(cmd, null, true);
}
}