This repository was archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathcli.js
More file actions
190 lines (172 loc) · 5.38 KB
/
cli.js
File metadata and controls
190 lines (172 loc) · 5.38 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/****************************************************************************
Copyright 2016 Apigee Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************************/
'use strict';
var _ = require('lodash');
var inquirer = require('inquirer');
var feedback = require('./feedback');
var config = require('../../config');
var yaml = require('js-yaml');
var util = require('util');
module.exports = {
requireAnswers: requireAnswers,
updateAnswers: updateAnswers,
printAndExit: printAndExit,
chooseOne: chooseOne,
validate: validate,
execute: execute,
confirm: confirm,
prompt: prompt,
version: version,
model: model2
};
function version() {
return require('../../package.json').version;
}
// questions are array of objects like these:
// { name: 'key', message: 'Your prompt?' }
// { name: 'key', message: 'Your prompt?', type: 'password' }
// { name: 'key', message: 'Your prompt?', type: 'list', choices: ['1', '2'] }
// results is an (optional) object containing existing results like this: { key: value }
function requireAnswers(questions, results, cb) {
if (!cb) { cb = results; results = {}; }
var unanswered = getUnanswered(questions, results);
if (unanswered.length === 0) {
return cb(results);
}
inquirer.prompt(unanswered, function(answers) {
_.extend(results, answers);
requireAnswers(questions, results, cb);
});
}
function updateAnswers(questions, results, cb) {
if (!cb) { cb = results; results = {}; }
for (var i = 0; i < questions.length; i++) {
var question = questions[i];
if (question.type !== 'password') {
question.default = results[question.name];
}
}
inquirer.prompt(questions, function(answers) {
_.extend(results, answers);
requireAnswers(questions, results, cb);
});
}
function getUnanswered(questions, results) {
var unanswered = [];
for (var i = 0; i < questions.length; i++) {
var question = questions[i];
if (!results[question.name]) {
unanswered.push(question);
}
}
return unanswered;
}
function printAndExit(err, output, code) {
if (err) {
print(err);
code = code || 1;
} else if (output !== null && output !== undefined) {
print(output);
}
process.exit(code || 0);
}
function print(object) {
if (util.isError(object)) {
console.log(config.debug ? object.stack : object);
} else if (_.isObject(object)) {
if (object.password) {
object.password = '******';
}
console.log(yaml.safeDump(object, { indent: 2 }));
} else if (object !== null && object !== undefined) {
console.log(object);
} else {
console.log();
}
}
// prompt: 'Your prompt?', choices: ['1', '2'] }
// result passed to cb() is the choice selected
function chooseOne(prompt, choices, cb) {
var questions = { name: 'x', message: prompt, type: 'list', choices: choices };
inquirer.prompt(questions, function(answers) {
cb(answers.x);
});
}
// defaultBool is optional (default == true)
// result passed to cb() is the choice selected
function confirm(prompt, defaultBool, cb) {
if (!cb) { cb = defaultBool; defaultBool = true; }
var question = { name: 'x', message: prompt, type: 'confirm', default: defaultBool};
inquirer.prompt(question, function(answers) {
cb(answers.x);
});
}
// defaultValue is optional
// result passed to cb() is the response
function prompt(prompt, defaultValue, cb) {
if (!cb) { cb = defaultValue; defaultValue = undefined; }
var question = { name: 'x', message: prompt, default: defaultValue};
inquirer.prompt(question, function(answers) {
cb(answers.x);
});
}
function validate(app) {
var commands = app.commands.map(function(command) { return command._name; });
if (!_.includes(commands, app.rawArgs[2])) {
if (app.rawArgs[2]) {
console.log();
console.log('error: invalid command: ' + app.rawArgs[2]);
}
app.help();
}
}
function execute(command, header) {
var cb = function(err, reply) {
if (header && !err) {
print(header);
print(Array(header.length + 1).join('='));
}
if (!reply && !err) { reply = 'done'; }
printAndExit(err, reply);
};
return function() {
try {
var args = Array.prototype.slice.call(arguments);
args.push(cb);
if (!command) {
return cb(new Error('missing command method'));
}
if (args.length !== command.length) {
return cb(new Error('incorrect arguments'));
}
var reply = command.apply(this, args);
if (reply) {
cb(null, reply);
}
} catch (err) {
cb(err);
}
}
}
if (typeof String.prototype.endsWith !== 'function') {
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
}
feedback.on(function(feedback) {
if (_.isString(feedback) && feedback.endsWith('\\')) {
process.stdout.write(feedback.substr(0, feedback.length - 1));
} else {
print(feedback);
}
});