-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·120 lines (100 loc) · 2.72 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
#!/usr/bin/env node
/**
* @license MIT
* Copyright (c) 2016 Craig Monro (cmroanirgo)
* This file is part of ergo-cms, specifically, the package ergo-cli/cli.js
**/
"use strict";
var bluebird = require('bluebird');
var path = require('path');
var fs = require('fs');
var l = require('ergo-utils').log.module('ergo-cli');
var _ = require('ergo-utils')._;
var api = require('./api/index');
(function main(_argv) {
var argv = cli(_argv);
l.init(argv);
// process the options
if (argv._.length<1)
showHelp(argv.help ? undefined : "No command specified");
var apiName = argv._[0];
l.logd('api command: ' + apiName)
if (_.isDefined(api[apiName])) {
if (argv.help)
showComandHelp(apiName, api[apiName]);
try {
api[apiName]._cli_exec(_argv)
.catch(function(err) {
l.loge(_.niceStackTrace(err));
})
}
catch(e) {
// we catch errors that aren't promisified yet. These are the setup exceptions, such as missing parameters.
// This is a good thing
l.logd(_.niceStackTrace(e))
showComandHelp(apiName, api[apiName], e.message);
}
}
else
showHelp("Unknown ergo command '"+apiName+"'");
})(process.argv.slice(2));
function cli(args, options) {
options = options || {
alias:{
'f':'force'
, 'i':'init'
, 'q':'quiet'
, 'v':'verbose'
},
boolean:true
};
return require('minimist')(args, options);
}
function showHelp(str) {
if (str) {
l.loge(str);
}
var commandsStr = [];
var apiNames = Object.keys(api).sort();
for (var i=0; i<apiNames.length; i++) {
var str = apiNames[i];
var apiLib = api[str];
var paramNames = apiLib.getRequiredParamNames().join(' ');
if (paramNames.length)
paramNames = ' [' + paramNames + ']';
commandsStr.push('\t'+str + paramNames + '\n')
}
console.log(
"USAGE:\n" +
"\tergo command [options]\n" +
"\n"+
"Valid commands are:\n"+
commandsStr.join('')+
"\n"+
"\tUse 'ergo command --help', for help on a specific command\n" +
"\n"+
"WHERE:\n"+
"\t-q, --quiet No output, except for warnings and errors\n" +
"\t-v, --verbose Verbose output. Use -v2 or --verbose=2 for extra verbosity\n" +
"\t-h, --help Show help\n" +
"");
process.exit(-1);
}
function showComandHelp(apiName, apiLib, str) {
if (str)
l.loge(str);
var paramNames = apiLib.getRequiredParamNames().join(' ');
if (paramNames.length)
paramNames = ' ' + paramNames;
console.log(
"USAGE:\n" +
"\tergo "+apiName+paramNames+" [options]\n" +
"\n"+
"WHERE:\n"+
apiLib.getHelp()+
"\t-q, --quiet No output, except for warnings and errors\n" +
"\t-v, --verbose Verbose output. use -v2 or --verbose=2 for extra verbose\n" +
"\t-h, --help Show help for this command\n" +
"");
process.exit(-1);
}