-
Notifications
You must be signed in to change notification settings - Fork 214
/
commands.js
62 lines (51 loc) · 1.81 KB
/
commands.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
var _ = require('lodash');
// Helper function for print help
// indented output by spaces
function indent_output(n, name, description) {
if (!n) {
n = 0;
}
console.log(
_.repeat(' ', n)
+ name
+ _.repeat(' ', 32 - n * 4 - name.length)
+ description
);
}
// Print help for a list of commands
// It prints the command and its description, then all the options
function help(commands) {
_.each(commands, function(command) {
indent_output(1, command.name, command.description);
_.each(command.options || [], function(option) {
var after = [];
if (option.defaults !== undefined) after.push("Default is "+option.defaults);
if (option.values) after.push("Values are "+option.values.join(", "));
if (after.length > 0) after = "("+after.join("; ")+")";
else after = "";
indent_output(2, '--' + option.name, option.description + ' ' + after);
});
console.log('');
});
}
// Execute a command from a list
// with a specific set of args/kwargs
function exec(commands, command, args, kwargs) {
var cmd = _.find(commands, function(_cmd) {
return _.first(_cmd.name.split(" ")) == command;
});
// Command not found
if (!cmd) throw new Error('Command '+command+' doesn\'t exist, run "gitbook help" to list commands.');
// Apply defaults
_.each(cmd.options || [], function(option) {
kwargs[option.name] = (kwargs[option.name] === undefined)? option.defaults : kwargs[option.name];
if (option.values && !_.includes(option.values, kwargs[option.name])) {
throw new Error('Invalid value for option "'+option.name+'"');
}
});
return cmd.exec(args, kwargs);
}
module.exports = {
help: help,
exec: exec
};