-
Notifications
You must be signed in to change notification settings - Fork 18
/
cover.js
executable file
·221 lines (160 loc) · 4.79 KB
/
cover.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
#! /usr/bin/env node
"use strict";
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
var Async = require('Supersonic').Async;
var Queue = require('Supersonic').Queue;
var colors = require('colors');
var pack = require('../package.json');
var Instrument = require('../lib/Instrument');
// options
var files = [];
var dir;
var recursive = false;
var excludes = [];
var template;
var result;
var args = process.argv.slice(2);
if (!args.length) args.push('--help');
for (var i = 0; i < args.length; i++){
var arg = args[i];
if (arg == '--version' || arg == '-v'){
console.log(pack.version);
process.exit(0);
break;
}
if (arg == '--output' || arg == '-o'){
dir = args[++i];
continue;
}
if (arg == '--recursive' || arg == '-r'){
recursive = true;
continue;
}
if (arg == '--exclude' || arg == '-e'){
excludes.push(args[++i]);
continue;
}
if (arg == '--template' || arg == '-t'){
template = args[++i];
continue;
}
if (arg == '--result'){
result = args[++i];
continue;
}
if (arg == '--help' || arg == '-h'){
var help = '\n\n' +
' Usage: coverjs [options] <files>\n\n' +
' Options:\n\n' +
' -h, --help output usage information\n' +
' -v, --version output version information\n' +
' -o, --output <directory> directory to output the instrumented files\n' +
' -r, --recursive recurse in subdirectories\n' +
' -x, --exclude <directories> exclude these directories\n' +
' -t, --template <template> which template should be used which exports the __$coverObject variable\n' +
' --result <file> if --template option is node, also a result file should be given where the output is written to.' +
'\n\n';
console.log(help);
process.exit(0);
break;
}
files.push(arg);
}
// normalize dir
if (!dir){
console.warn('the --output option is required');
process.exit(1);
} else {
dir = path.normalize(dir);
}
// normalize exclude files/directories
excludes.map(function(dir){
return path.normalize(dir);
});
// async flow
var flow = new Async();
// return a function which executes a series of actions
var processFile = function(file, outFile){
file = path.normalize(file);
var ext = path.extname(file);
if (fs.statSync(file).isFile() && ext && ext != '.js'){
console.warn('ERROR:'.red.inverse + ' ' + file + ' is not a JavaScript file');
return;
}
return function(ready){
var queue = new Queue();
var code, instrumented;
queue.push(function(next, finish){
fs.stat(file, function(err, stat){
if (err) console.warn(('ERROR:').red.invert + ('Could not open ' + file).red);
else if (stat.isFile()){
next();
} else if (recursive && stat.isDirectory()){
console.warn(('Recursing into ' + file).yellow);
fs.readdir(file, function(err, files){
if (err) throw err;
files.forEach(function(_file){
var __file = path.normalize(file + '/' + _file);
if (_file.indexOf('.') !== 0 && _file != '..' && _file != '.' && excludes.indexOf(__file) == -1){
var fn = processFile(file + '/' + _file, outFile + '/' + _file);
if (fn) flow.push(fn);
}
});
finish(false);
});
} else if (stat.isDirectory()){
console.warn('ERROR: '.red.inverse + ' ' + file + ' is a directory, maybe use the --recursive option');
finish(false);
} else {
console.warn('ERROR: '.red.inverse + ' ' + file + ' is not a file nor a directory');
finish(false);
}
});
}).push(function(next, finish){
// read the file
fs.readFile(file, function(err, data){
if (err){
console.warn(('ERROR:').red.inverse + (' Could not open ' + file).red);
finish(false);
} else {
code = data + '';
next();
}
});
}).push(function(next){
// instrument the code
console.warn(('instrumenting ' + file).blue);
var instrument = new Instrument(code, {
template: template,
result: result && path.resolve(result),
name: file
});
instrumented = instrument.instrument();
next();
}).push(function(next){
// create the target directory
var newDir = path.dirname(outFile);
mkdirp.sync(newDir, 511);
next();
}).push(function(next){
// and write the file
fs.writeFile(outFile, instrumented, function(err){
if (err) throw err;
next();
});
});
queue.invoke(function(log){
if (log !== false) console.warn(('READY:').green.inverse + (' ' + file + ' has been instrumented').green);
ready();
});
};
};
files.forEach(function(file){
var fn = processFile(file, path.join(dir, path.basename(file)));
if (fn) flow.push(fn);
});
flow.invoke(function(){
console.warn(('READY:').green.inverse + (' Instrumented code has been written to ' + dir).green);
});