forked from jasonkneen/ti-i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·92 lines (71 loc) · 2.44 KB
/
bin.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
#!/usr/bin/env node
var program = require('commander'),
_ = require('underscore'),
config = require('./config'),
main = require('./main');
program
.version(config.package.version, '-v, --version')
.description(config.package.about.name)
.usage('command [options]');
// expose the module's commands to the program
_.each(config.commands, function(config, name) {
// for the CLI
if (config.cli !== false) {
var usage = name;
// args
if (config.args) {
_.each(config.args, function(arg) {
usage += (arg.required === true || (arg.required !== false && _.has(arg, 'default') === false)) ? ' <' + arg.name + '>' : ' [' + arg.name + ']';
});
}
// command
var command = program.command(usage)
.description(config.desc.grey)
.action(function() {
// format like we get it when being an hook
var args = _.flatten(arguments),
argv = args.pop();
argv._ = args;
// either called specified action
if (_.isFunction(config.action)) {
config.action(argv);
}
// or assume it's an exported method for the module
else {
main[name](argv);
}
});
// flags
if (config.flags) {
_.each(config.flags, function(cf, flag) {
var inst = [];
if (cf.abbr) {
inst.push('-' + cf.abbr);
}
inst.push('--' + flag);
command.option(inst.join(', '), cf.desc);
});
}
// options
if (config.options) {
_.each(config.options, function(cf, option) {
var inst = [];
if (cf.abbr) {
inst.push('-' + cf.abbr);
}
inst.push('--' + option + (cf.hint ? ' <' + cf.hint + '>' : ''));
command.option(inst.join(', '), cf.desc);
});
}
}
});
// display banner for help
if (_.intersection(process.argv, ['-h', '--help']).length > 0) {
config.banner(false);
}
program.parse(process.argv);
// show help when we don't have a valid arg
if (program.args.length === 0 || typeof program.args[program.args.length - 1] === 'string') {
config.banner(false);
program.help();
}