-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
341 lines (300 loc) · 13 KB
/
index.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
var _ = require('underscore');
var readline = require('readline');
var util = require('util');
var CommandAutocomplete = require('./lib/autocomplete');
var CommandHistory = require('./lib/history');
var CommandParser = require('./lib/parser');
/**
* Start a command prompt loop.
*
* @param {Object} [options={}] Optional read arguments
* @param {Stream} [options.input=process.stdin] The input stream to read a command from
* @param {Stream} [options.output=process.stdout] The output stream to display prompts to
* @param {Function} [options.ps1="> "] A function to retrieve the ps1 prompt on
* each command iteration
* @param {Function} [options.ps2="> "] A function to retrieve the ps2 prompt on
* each command iteration
* @param {Function} [options.autocomplete] The autocomplete function to use
* @param {String[]} [options.autocomplete.args] The args that are being autocompleted,
* similar to `readline` docs for `completer`
* @param {Function} [options.autocomplete.callback] Invoke with an `err` parameter (if any)
* followed by an array of potential
* replacements (if any)
* @param {String[]} [options.history=[]] The command history to use for toggling
* command output with up and down
* @param {Function} onCommand Invoked each time the user has input a
* command
* @param {Error} onCommand.err An error occurred receiving the command, if
* any
* @param {String} onCommand.err.code The type of error. Known codes: "SIGINT" The
* user pressed CTRL+C
* @param {String[]} onCommand.args The parsed command arguments from the user
* @param {String} onCommand.str The raw input string from the user
* @param {Function} onCommand.next Invoke this function to get the next
* command. If you want to exit the loop,
* simply don't invoke this, everything is
* already cleaned up
*/
var loop = module.exports.loop = function(options, onCommand) {
if (_.isFunction(options) && !onCommand) {
onCommand = options;
options = null;
}
options = options || {};
options.input = options.input || process.stdin;
options.output = options.output || process.stdout;
options.ps1 = _psToFunction(options.ps1);
options.ps2 = _psToFunction(options.ps2);
return _loop(options, onCommand, options.history);
};
/**
* Read a single multi-line command from the user.
*
* @param {Object} [options={}] Optional read arguments
* @param {Stream} [options.input=process.stdin] The input stream to read a command from
* @param {Stream} [options.output=process.stdout] The output stream to display prompts to
* @param {String} [options.ps1="> "] The PS1 prompt label for the first line of
* the command
* @param {String} [options.ps2="> "] The PS2 prompt label for subsequent lines of
* the command
* @param {String[]} [options.history=[]] The command history to use for toggling
* command output with up and down
* @param {Function} [options.autocomplete] The autocomplete function to use
* @param {String[]} [options.autocomplete.args] The args that are being autocompleted,
* similar to `readline` docs for `completer`
* @param {Function} [options.autocomplete.callback] Invoke with an `err` parameter (if any)
* followed by an array of potential
* replacements (if any)
* @param {Function} callback Invoked when a command has been read by the
* user
* @param {Error} callback.err An error that occurred, if any
* @param {String} callback.err.code The type of error. Known codes: "SIGINT" The
* user pressed CTRL+C
* @param {String[]} callback.args The parsed command arguments
* @param {String} callback.str The raw input string from the user
*/
var read = module.exports.read = function(options, callback) {
if (_.isFunction(options) && !callback) {
callback = options;
options = null;
}
options = options || {};
options.input = options.input || process.stdin;
options.output = options.output || process.stdout;
options.ps1 = options.ps1 || '> ';
options.ps2 = options.ps2 || '> ';
options.history = options.history || [];
callback = callback || function() {};
var state = {
'input': options.input,
'output': options.output,
'rl': null,
'ps1': options.ps1,
'ps2': options.ps2,
'history': new CommandHistory(options.history),
'autocomplete': options.autocomplete,
'callback': callback,
'currentCommand': '',
'currentLine': '',
'onFirstLine': true
};
// Create the readline instance and set it up for cool things!
_resetReadLine(state);
// This isn't done in _resetReadLineAndRead because it binds to the input, not the readline
// instance
_bindKeypress(state);
return _read(state);
};
/*!
* Do the heavy lifting of looping, asking for commands and managing the command history.
*/
function _loop(options, onCommand, _history) {
_history = _history || [];
var readOptions = _.extend({}, options, {
'ps1': options.ps1(),
'ps2': options.ps2(),
'history': _history
});
read(readOptions, function(err, args, str) {
// If the user entered an actual command, put the string in the command history
if (str) {
_history.push(str.split('\n').shift());
}
// Provide the args to the caller
onCommand(err, args, str, function() {
// Recursively ask for the next command
return _loop(options, onCommand, _history);
});
});
}
/*!
* Perform the heavy lifting for reading the first and subsequent command lines
*/
function _read(state) {
var ps = (state.onFirstLine) ? state.ps1 : state.ps2;
state.rl.question(ps, function(str) {
// Append this input to the current full command string that we'll try and parse
var commandToParse = state.currentCommand + str;
// Parse the current full command
var result = CommandParser.parse(commandToParse);
if (!result.open) {
// The multi-line command is completed. Send the result to the caller
return _sendResult(state, null, result.args, result.str);
}
// We didn't close out the command. We should use the processed string that the command
// parser wants us to continue with, so append that to the parser with the new-line the
// user input, as that will now be a part of the command string
state.currentCommand = result.str + '\n';
// Read a second line of input
state.onFirstLine = false;
return _read(state);
});
// If we started with a line, clear it so subsequent reads don't start with this. `currentLine`
// is necessary for up-down history replacement
if (state.currentLine) {
state.rl.write(state.currentLine);
state.currentLine = '';
}
}
/*!
* Reset the state of the readline instance to get a new prompt
*/
function _resetReadLine(state) {
if (state.rl) {
state.rl.close();
}
state.rl = readline.createInterface({
'input': state.input,
'output': state.output,
'completer': function(line, callback) {
if (!_.isFunction(state.autocomplete)) {
// No autocomplete was provided, do nothing
return callback(null, [[], line]);
}
CommandAutocomplete.getAutocompleteArguments(state.currentCommand, line, function(abort, args) {
if (abort) {
return callback(null, [[], line]);
}
state.autocomplete(args, function(err, replacements) {
if (err) {
return callback(err);
}
CommandAutocomplete.getAutocompleteReplacements(state.currentCommand, line, replacements, function(replacements, toReplace) {
// Convert the arguments into what node-readline expects
return callback(null, [replacements, toReplace]);
});
});
});
}
});
/*!
* Monkey-patch the setPrompt method to properly calculate the string length when colors are
* used. :(
*
* http://stackoverflow.com/questions/12075396/adding-colors-to-terminal-prompt-results-in-large-white-space
*/
var rl = state.rl;
rl._setPrompt = rl.setPrompt;
rl.setPrompt = function(prompt, length) {
var strippedLength = null;
if (length) {
strippedLength = length;
} else {
var stripped = prompt.split(/[\r\n]/).pop().stripColors;
if (stripped) {
strippedLength = stripped.length;
}
}
rl._setPrompt(prompt, strippedLength);
};
_bindSigint(state);
}
/*!
* Bind the SIGINT handling to the readline instance, which effectively returns with an empty
* command.
*/
function _bindSigint(state) {
state.rl.once('SIGINT', function() {
// Mock a new-line and send an empty result
state.output.write('\n');
return _sendResult(state, _.extend(new Error('User pressed CTRL+C'), {'code': 'SIGINT'}));
});
}
/*!
* Bind the keypress handling to the input stream to handle navigating command history
*/
function _bindKeypress(state) {
state.onKeypress = function(ch, key) {
if (!key || !state.onFirstLine) {
// Ignore the up/down history searcing when we've extended to a new line
return;
}
var replace = null;
if (_keyIs(key, 'up')) {
replace = state.history.prev();
} else if (_keyIs(key, 'down')) {
replace = state.history.next();
}
if (_.isString(replace)) {
// This will close the current prompt, so we can safely get a new one by re-invoking
// `_read`
_resetReadLine(state);
state.currentLine = replace;
return _read(state);
}
};
state.input.on('keypress', state.onKeypress);
}
/*!
* Unbind the keypress handler from the input stream
*/
function _unbindKeypress(state) {
state.input.removeListener('keypress', state.onKeypress);
}
/*!
* Send the arguments result to the caller and clean up after ourselves
*/
function _sendResult(state, err, args, str) {
_unbindKeypress(state);
state.rl.close();
// At this point, we should be able to parse a closed command. If not, something is not right
return state.callback(err, _.pluck(_getFinalArguments(args), 'str'), str);
}
/*!
* Clean vestigial arguments out of the arguments array so that it may be sent to the caller
* as a completed command
*/
function _getFinalArguments(args) {
return _.filter(args, function(arg) {
// We filter out empty arguments, but only if they weren't explicitly specified with
// quotes. E.g., "--verbose ''" will retain the empty string, while "--verbose " will have
// it stripped
return (arg.quote || arg.str !== '');
});
}
/*!
* Convenience function to convert a potential string or undefined ps string to a function that
* returns the same string.
*/
function _psToFunction(ps) {
var result = null;
if (_.isString(ps)) {
result = function() {
return ps;
};
} else if (_.isFunction(ps)) {
result = ps;
} else {
result = function() {
return '> ';
};
}
return result;
}
/*!
* Convenience method to determine if the provided keypress key is a verbatim key. Returns false if
* it's not the specified key name or it has been executed with shift, ctrl or a meta key
*/
function _keyIs(key, name) {
return (key.name === name && !key.ctrl && !key.shift && !key.meta);
}