1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | module.exports = comfort;
|
10 |
|
11 | function comfort(options) {
|
12 | options || (options = {});
|
13 | return new Comfort(options);
|
14 | }
|
15 |
|
16 |
|
17 | var node_path = require('path');
|
18 | var EE = require('events').EventEmitter;
|
19 | var util = require('util');
|
20 | var spawn = require('spawns');
|
21 | var fs = require('fs');
|
22 | var expand = require('fs-expand');
|
23 | var async = require('async');
|
24 | var clean = require('clean');
|
25 | var mix = require('mix2');
|
26 |
|
27 | var BUILTIN_COMMAND_ROOT = node_path.join(__dirname, 'lib', 'built-in', 'command');
|
28 | var BUILTIN_OPTION_ROOT = node_path.join(__dirname, 'lib', 'built-in', 'option');
|
29 |
|
30 |
|
31 | function Comfort(options) {
|
32 | this.options = options;
|
33 |
|
34 | this.commands = options.commands;
|
35 | options.offset = 'offset' in options ? options.offset : 3;
|
36 |
|
37 | this._context = {};
|
38 | this._commander = {};
|
39 | }
|
40 |
|
41 | util.inherits(Comfort, EE);
|
42 |
|
43 |
|
44 | Comfort.prototype.context = function (context) {
|
45 | mix(this._context, context);
|
46 | return this;
|
47 | };
|
48 |
|
49 |
|
50 | Comfort.prototype.setup = function(setup) {
|
51 | if (this.pending) {
|
52 | return this;
|
53 | }
|
54 |
|
55 | if (typeof setup === 'function') {
|
56 | this.pending = true;
|
57 | var done = function () {
|
58 | this.pending = false;
|
59 | this.emit('setup');
|
60 | }.bind(this);
|
61 |
|
62 | setup.call(this, done);
|
63 | }
|
64 |
|
65 | return this;
|
66 | };
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | var REGEX_REPLACE_EXTENSION = /\..+$/;
|
72 |
|
73 |
|
74 | Comfort.prototype._get_commands = function(callback) {
|
75 | if (this.commands) {
|
76 | return callback(null, this.commands);
|
77 | }
|
78 |
|
79 | var self = this;
|
80 | expand('*.js', {
|
81 | cwd: this.options.command_root
|
82 | }, function (err, files) {
|
83 | if (err) {
|
84 | return callback(err);
|
85 | }
|
86 |
|
87 | var commands = files.map(function(command) {
|
88 | return command.replace(REGEX_REPLACE_EXTENSION, '');
|
89 | });
|
90 |
|
91 | self.commands = commands;
|
92 | callback(null, commands);
|
93 | });
|
94 | };
|
95 |
|
96 |
|
97 | Comfort.prototype.parse = function(argv, callback) {
|
98 | var self = this;
|
99 | this._get_commands(function (err) {
|
100 | if (err) {
|
101 | return callback(err);
|
102 | }
|
103 |
|
104 | self._parse(argv, callback);
|
105 | });
|
106 | };
|
107 |
|
108 |
|
109 | var BUILTIN_COMMANDS = [
|
110 | 'help',
|
111 | 'version'
|
112 | ];
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | Comfort.prototype._parse = function(argv, callback, strict) {
|
119 |
|
120 |
|
121 | var command = argv[2];
|
122 | var is_entry = !command;
|
123 |
|
124 | var is_command = this._is_command(command);
|
125 | var is_normal = is_command && this._is_normal(command);
|
126 | var is_builtin = is_command && this._is_builtin(command);
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | if (is_command && !is_normal && !is_builtin) {
|
134 | if (strict) {
|
135 | return this._command_not_found(command, callback);
|
136 | }
|
137 |
|
138 | return callback(null, {
|
139 | argv: argv,
|
140 | command: command,
|
141 |
|
142 |
|
143 | type: 'plugin'
|
144 | });
|
145 | }
|
146 |
|
147 |
|
148 |
|
149 | if (
|
150 | command === 'version' ||
|
151 | ~argv.indexOf('-v') ||
|
152 | ~argv.indexOf('--version')
|
153 | ) {
|
154 | command = 'version';
|
155 | return callback(null, {
|
156 | argv: argv,
|
157 | command: command,
|
158 | options: {
|
159 | root: this.options.root
|
160 | },
|
161 | type: this._is_builtin(command)
|
162 | ? 'builtin'
|
163 | : 'normal'
|
164 | });
|
165 | }
|
166 |
|
167 |
|
168 |
|
169 | var index_h = argv.indexOf('-h');
|
170 | var index_help = argv.indexOf('--help');
|
171 |
|
172 | var help_extra_options = {
|
173 | commands : this.commands,
|
174 | builtins : BUILTIN_COMMANDS,
|
175 | name : this.options.name,
|
176 | normal_root : this.options.option_root,
|
177 | builtin_root : BUILTIN_OPTION_ROOT
|
178 | };
|
179 |
|
180 |
|
181 | if (
|
182 |
|
183 | ~index_h ||
|
184 |
|
185 | ~index_help ||
|
186 |
|
187 | is_entry ||
|
188 |
|
189 | !is_command
|
190 | ) {
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | var command_for_help =
|
196 | index_h !== 2 &&
|
197 | index_help !== 2 &&
|
198 | argv[2];
|
199 |
|
200 |
|
201 |
|
202 | if (!this._is_command(command_for_help)) {
|
203 | command_for_help = null;
|
204 | }
|
205 |
|
206 | command = 'help';
|
207 |
|
208 | if (is_entry) {
|
209 | this.emit('entry');
|
210 | }
|
211 |
|
212 | return callback(null, {
|
213 | argv: argv,
|
214 | command: command,
|
215 | type: this._is_builtin(command)
|
216 | ? 'builtin'
|
217 | : 'normal',
|
218 | options: mix({
|
219 | command: command_for_help,
|
220 |
|
221 | entry: is_entry
|
222 | }, help_extra_options)
|
223 | });
|
224 | }
|
225 |
|
226 |
|
227 |
|
228 | this._parse_argv(command, argv, function (err, result) {
|
229 | if (err) {
|
230 | return callback(err);
|
231 | }
|
232 |
|
233 | if (command === 'help') {
|
234 | mix(result.options, help_extra_options);
|
235 | }
|
236 |
|
237 | callback(null, result);
|
238 | });
|
239 | };
|
240 |
|
241 |
|
242 | Comfort.prototype._is_command = function(command) {
|
243 | return command && command.indexOf('-') !== 0;
|
244 | };
|
245 |
|
246 |
|
247 | Comfort.prototype._is_builtin = function(command) {
|
248 | return ~BUILTIN_COMMANDS.indexOf(command);
|
249 | };
|
250 |
|
251 |
|
252 | Comfort.prototype._is_normal = function(command) {
|
253 | return ~this.commands.indexOf(command);
|
254 | };
|
255 |
|
256 |
|
257 | Comfort.prototype._command_not_found = function(command, callback) {
|
258 | var name = this.options.name;
|
259 | callback({
|
260 | code: 'COMMAND_NOT_FOUND',
|
261 | message: name + ': "' + command + '" is not a "' + name + '" command. See "' + name + ' --help".',
|
262 | data: {
|
263 | name: name,
|
264 | command: command
|
265 | }
|
266 | });
|
267 | };
|
268 |
|
269 |
|
270 |
|
271 | Comfort.prototype._parse_argv = function(command, argv, callback) {
|
272 |
|
273 | var is_builtin = this._is_builtin(command);
|
274 | var option_root = is_builtin
|
275 | ? BUILTIN_OPTION_ROOT
|
276 | : this.options.option_root;
|
277 |
|
278 | var type = is_builtin
|
279 | ? 'builtin'
|
280 | : 'normal';
|
281 |
|
282 | var self = this;
|
283 | this._get_option_rule(command, option_root, function (err, rule) {
|
284 | if (err) {
|
285 | return callback(err);
|
286 | }
|
287 |
|
288 | if (!rule) {
|
289 | return callback(null, {
|
290 | argv: argv,
|
291 | command: command,
|
292 | type: type,
|
293 | options: {}
|
294 | });
|
295 | }
|
296 |
|
297 |
|
298 | clean({
|
299 | schema: rule.options,
|
300 | shorthands: rule.shorthands,
|
301 | offset: self.options.offset
|
302 |
|
303 | }).parse(argv, function(err, results, details) {
|
304 | callback(err, {
|
305 | command: command,
|
306 | options: results,
|
307 | argv: argv,
|
308 | type: type
|
309 | });
|
310 | });
|
311 | });
|
312 | };
|
313 |
|
314 |
|
315 | Comfort.prototype._get_option_rule = function(command, root, callback) {
|
316 | var file = node_path.join(root, command + '.js');
|
317 | fs.exists(file, function (exists) {
|
318 | if (!exists) {
|
319 | return callback(null, null);
|
320 | }
|
321 |
|
322 | var rule;
|
323 | try {
|
324 | rule = require(file);
|
325 | } catch(e) {
|
326 | return callback({
|
327 | code: 'FAIL_READ_OPTION',
|
328 | message: 'Fails to read option file "' + file + '": ' + e.stack,
|
329 | data: {
|
330 | command: command,
|
331 | file: file,
|
332 | error: e
|
333 | }
|
334 | });
|
335 | }
|
336 |
|
337 | callback(null, rule);
|
338 | });
|
339 | };
|
340 |
|
341 |
|
342 | Comfort.prototype._get_command = function(command, root, callback) {
|
343 | var file = node_path.join(root, command + '.js');
|
344 | fs.exists(file, function (exists) {
|
345 | if (!exists) {
|
346 | return this._command_not_found(command, callback);
|
347 | }
|
348 |
|
349 | var proto;
|
350 | try {
|
351 | proto = require(file);
|
352 | } catch(e) {
|
353 | return callback({
|
354 | code: 'FAIL_READ_COMMAND',
|
355 | message: 'Fails to read command file "' + file + '": ' + e.stack,
|
356 | data: {
|
357 | command: command,
|
358 | file: file,
|
359 | error: e
|
360 | }
|
361 | });
|
362 | }
|
363 |
|
364 | callback(null, proto);
|
365 | });
|
366 | };
|
367 |
|
368 |
|
369 |
|
370 | Comfort.prototype.run = function(argv, callback) {
|
371 | var self = this;
|
372 | this.parse(argv, function (err, result) {
|
373 | if (err) {
|
374 | return callback(err);
|
375 | }
|
376 |
|
377 | var command = result.command;
|
378 |
|
379 | if (result.type === 'plugin') {
|
380 | return self.plugin(command, result.argv.slice(3), callback);
|
381 | }
|
382 |
|
383 | self.command(command, result.options, callback);
|
384 | });
|
385 | };
|
386 |
|
387 |
|
388 |
|
389 | Comfort.prototype.command = function(command, options, callback) {
|
390 | this.commander(command, function (err, commander) {
|
391 | if (err) {
|
392 | return callback(err);
|
393 | }
|
394 |
|
395 | commander.run(options, callback);
|
396 | });
|
397 | };
|
398 |
|
399 |
|
400 |
|
401 | Comfort.prototype.plugin = function(command, args, callback) {
|
402 | this.emit('plugin', {
|
403 | name: name,
|
404 | command: command,
|
405 | args: args
|
406 | });
|
407 |
|
408 | var name = this.options.name;
|
409 | var bin = name + '-' + command;
|
410 | var paths = process.env.PATH.split(':').map(function (path) {
|
411 | return node_path.resolve(path, bin);
|
412 | });
|
413 |
|
414 | var self = this;
|
415 | this._try_files(paths, function (found) {
|
416 | if (!found) {
|
417 | return self._command_not_found(command, callback);
|
418 | }
|
419 |
|
420 | var plugin = spawn(found, args, {
|
421 | stdio: 'inherit',
|
422 |
|
423 |
|
424 | customFds: [0, 1, 2]
|
425 | });
|
426 |
|
427 |
|
428 | plugin.on('exit', function(code) {
|
429 | callback(code);
|
430 | });
|
431 | });
|
432 | };
|
433 |
|
434 |
|
435 | Comfort.prototype._try_files = function(files, callback) {
|
436 | async.eachSeries(files, function (file, done) {
|
437 | fs.exists(file, function (exists) {
|
438 | if (!exists) {
|
439 | return done(null);
|
440 | }
|
441 |
|
442 | fs.stat(file, function (err, stat) {
|
443 | if (!err && stat.isFile()) {
|
444 | return done(file);
|
445 | }
|
446 |
|
447 | done(null);
|
448 | });
|
449 | });
|
450 | }, callback);
|
451 | };
|
452 |
|
453 |
|
454 |
|
455 | Comfort.prototype.commander = function(command, callback) {
|
456 |
|
457 | var commander = this._commander[command];
|
458 | if (commander) {
|
459 | return callback(null, commander);
|
460 | }
|
461 |
|
462 | var is_builtin = this._is_builtin(command);
|
463 | var command_root = is_builtin
|
464 | ? BUILTIN_COMMAND_ROOT
|
465 | : this.options.command_root;
|
466 |
|
467 | var self = this;
|
468 | this._get_command(command, command_root, function (err, proto) {
|
469 | if (err) {
|
470 | return callback(err);
|
471 | }
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | var commander = self._commander[command] = Object.create(proto);
|
477 |
|
478 |
|
479 | mix(commander, self._context);
|
480 |
|
481 | callback(null, commander);
|
482 | });
|
483 | };
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 | Comfort.prototype._cli = function(argv) {
|
523 | argv = argv || process.argv;
|
524 |
|
525 | var self = this;
|
526 | this.run(argv, function (err) {
|
527 | if (err) {
|
528 | return self.emit('error', err);
|
529 | }
|
530 |
|
531 | self.emit('finish');
|
532 | });
|
533 | };
|
534 |
|
535 |
|
536 | Comfort.prototype.cli = function(argv) {
|
537 | if (this.pending) {
|
538 | this.on('setup', function () {
|
539 | this._cli(argv);
|
540 | }.bind(this));
|
541 | } else {
|
542 | this._cli(argv);
|
543 | }
|
544 | };
|