-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (34 loc) · 971 Bytes
/
Copy pathindex.js
File metadata and controls
42 lines (34 loc) · 971 Bytes
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
module.exports = { router };
function router(cmds) {
return {
route: function () {
return route(cmds);
}
};
}
// Will route the process call to a function contained in the "cmds" object
async function route(cmds) {
var cmd = (process.argv.length >= 3) ? process.argv[2] : null;
var params = (process.argv.length >= 4) ? process.argv.slice(3) : [];
// TODO: probably need to slice the remaining arguments as parameters
// get the eventual fun for this command name (if empty, try to get the _default method)
var fn = (cmd) ? cmds[cmd] : cmds["_default"];
if (fn) {
try {
return await fn.apply(null, params);
} catch (ex) {
console.log(`FAIL to run ${fn.name}(${params}). Cause:\n${ex}`);
process.exit(1);
}
} else {
console.log("Wrong command '" + cmd + "' is not a function");
printCmds();
}
function printCmds() {
var msg = "Commands are:";
for (var label in cmds) {
msg += "\n\t" + label;
}
console.log(msg);
}
}