This repository has been archived by the owner on Mar 26, 2020. It is now read-only.
/
cli.js
executable file
·254 lines (243 loc) · 7.79 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
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env node
const PHANTOMJS_VERSION = '~1.3.0 || ~1.4.0 || ~1.5.0 || ~1.6.0';
var fs = require('fs'),
path = require('path'),
optimist = require('optimist'),
console = require('../lib/console'),
events = require('../lib/events');
var defaults = optimist.
usage('Usage: $0 [OPTION...] PATH\n\nCommand-line arguments are evaluated before any included files.\n\nReport issues at https://github.com/StanAngeloff/lotte/issues.').
option('help', {
alias: 'h',
default: false,
description: 'give this help page'
}).
option('version', {
alias: 'v',
default: false,
description: 'print program version'
}).
option('concurrent', {
alias: 'c',
default: 4,
type: 'number',
description: 'limit of files to run in parallel'
}).
option('timeout', {
alias: 't',
default: 30 * 1000,
type: 'number',
description: 'timeout for individual files (in milliseconds)'
}).
option('include', {
alias: 'I',
type: 'string',
default: '**/lotte_*.{coffee,js}',
description: 'glob pattern to match files in PATH'
}).
option('exclude', {
alias: 'E',
type: 'string',
description: 'glob pattern to remove included files'
}).
option('lottefile', {
alias: 'f',
type: 'string',
default: 'Lottefile',
description: "look for 'lottefile' in PATH"
}).
option('verify', {
type: 'boolean',
default: ( ! process.env['LOTTE_NO_VERIFY']),
description: "verify PhantomJS version (expected " + PHANTOMJS_VERSION + ")"
}).
option('server', {
type: 'boolean',
default: ( ! process.env['LOTTE_NO_SERVER']),
description: 'start a HTTP server for IPC'
}).
option('address', {
alias: 'a',
default: '127.0.0.1',
type: 'string',
description: 'IP address for the HTTP server to bind to'
}).
option('port', {
alias: 'p',
default: 3838,
type: 'number',
description: 'port for HTTP server to listen on'
}).
option('phantom', {
type: 'string',
default: 'phantomjs',
description: 'executable for PhantomJS'
}).
option('coffee', {
type: 'string',
default: 'coffee',
description: 'executable for CofeeScript'
}).
argv;
if (defaults.help) {
console.log(optimist.help());
process.exit(0);
}
if (defaults.version) {
console.log('%s %s', defaults.$0, '0.2.1');
process.exit(0);
}
if (process.argv.indexOf('--') < 0 && defaults._.length) {
defaults.path = defaults._.shift();
}
defaults.path = path.resolve(defaults.path || process.env['LOTTE_PATH'] || process.cwd());
var lotteFile = path.join(defaults.path, defaults.lottefile);
fs.exists(lotteFile, function(exists) {
if (exists) {
load(defaults, lotteFile, function(e, options) {
if (e) {
console.error.exception(e);
process.exit(1 << 1);
}
main(options);
});
} else {
main(defaults);
}
});
function load(options, file, block) {
require('../lib/utils').toJavaScript(options, file, function(e, code) {
if (e) {
return block(e);
}
var context = {}, i,
symbols = ['Buffer', 'console', 'process', 'setTimeout', 'clearTimeout', 'setInterval', 'clearInterval'];
context['require'] = require;
for (i = 0; i < symbols.length; i ++) {
context[symbols[i]] = global[symbols[i]];
}
symbols = ['on', 'once', 'off', 'message'];
for (i = 0; i < symbols.length; i ++) {
context[symbols[i]] = events[symbols[i]];
}
for (var key in options) {
if (Object.prototype.hasOwnProperty.call(options, key)) {
context[key] = options[key];
}
}
try {
require('vm').runInNewContext(code, context, file);
if (context.path !== options.path) {
context.path = path.resolve(options.path, context.path);
}
} catch (e) {
console.error.exception(e);
process.exit(1 << 2);
}
for (var key in options) {
if (Object.prototype.hasOwnProperty.call(options, key)) {
options[key] = context[key];
}
}
block(null, options);
});
};
function main(options) {
options || (options = {});
verifyPhantomBinary(options, function() {
collect(options, function(files) {
if ( ! files.length) {
console.warn("No files were found in '%s'.", options.path);
process.exit(0);
}
glob(files, { mode: 'include', pattern: options.include }, function(files) {
if ( ! files.length) {
console.warn("Files were found in '%s', but none matched your include pattern '%s'.", options.path, options.include);
process.exit(0);
}
glob(files, { mode: 'exclude', pattern: options.exclude }, function(files) {
if ( ! files.length) {
console.warn("Files were found in '%s', but all matched your exclude pattern '%s'.", options.path, options.exclude);
process.exit(0);
}
var server = require('../lib/server');
files.sort(require('naturalsort').compare);
options.files = files;
if (options['server']) {
server.listen(options.port, options.address);
}
events.message('start', [options], function() {
require('../lib/lotte').process(options, function(e, code) {
code = (e ? 1 << 8 : code);
events.message('exit', [code], function() {
server.close();
process.exit(code);
});
});
});
});
});
});
});
};
function verifyPhantomBinary(options, resume) {
if ( ! options['verify']) {
options.phantomVersion = false;
return resume();
}
var command = options.phantom + ' --version';
require('child_process').exec(command, function(e, stdout, stderr) {
if (e) {
console.error.exception(e);
process.exit(1 << 3);
}
if (stderr) {
console.error("Command '%s' failed:\n\n%s", command, stderr);
process.exit(1 << 4);
}
var groups = /^([\d\.\-a-z]+)/.exec(stdout);
if ( ! groups) {
console.error("Could not parse version string '%s'", ('' + stdout).trim());
process.exit(1 << 5);
}
options.phantomVersion = groups[0];
var semver = require('semver');
if ( ! semver.satisfies(options.phantomVersion, PHANTOMJS_VERSION)) {
console.error("PhantomJS version '%s' does not meet requirement '%s'.", options.phantomVersion, PHANTOMJS_VERSION);
process.exit(1 << 6);
}
resume();
});
};
function collect(options, resume) {
var absolute = path.resolve(options.path);
fs.exists(absolute, function(exists) {
if ( ! exists) {
console.error("Could not resolve '%s'.", absolute);
process.exit(1 << 7);
}
var walk = require('walkdir')(options.path),
result = [];
walk.on('file', function(file) {
result.push(file);
});
walk.on('error', function(e) {
console.warn(e.toString().trim());
});
walk.on('end', function() {
resume(result);
});
});
};
function glob(files, options, resume) {
var minimatch = require('minimatch'),
result = files,
exclude = (options.mode === 'exclude');
Array.isArray(options.pattern) || (options.pattern = [options.pattern]);
options.pattern.forEach(function(pattern) {
result = result.filter(function(file) {
return (minimatch(file, path.normalize(pattern || '').replace(/\\/g, '/')) ^ exclude);
});
});
resume(result);
};