-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·472 lines (373 loc) · 12 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/**
* `directory` type prompt
*/
//core
var assert = require('assert');
var path = require('path');
var fs = require('fs');
//npm
var rx = require('rx-lite');
var _ = require('lodash');
var util = require('util');
var chalk = require('chalk');
var figures = require('figures');
var cliCursor = require('cli-cursor');
var Base = require('inquirer/lib/prompts/base');
var observe = require('inquirer/lib/utils/events');
var Paginator = require('inquirer/lib/utils/paginator');
var Choices = require('inquirer/lib/objects/choices');
var Separator = require('inquirer/lib/objects/separator');
const _suman = global.__suman = (global.__suman || {});
/**
* Module exports
*/
module.exports = Prompt;
/**
* Constants
*/
var CHOOSE_DIRECTORY = 'choose this directory';
var BACK = 'go back a directory';
// stores what index to use for a path if you go back a directory
/**
* Constructor
*/
function Prompt () {
Base.apply(this, arguments);
if (!this.opt.basePath) {
this.throwParamError('basePath');
}
this.pathIndexHash = {};
this.originalBaseDir = this.currentPath =
path.normalize(path.isAbsolute(this.opt.basePath) ?
path.resolve(this.opt.basePath) : path.resolve(process.cwd(), this.opt.basePath));
if (String(this.currentPath).endsWith(path.sep)) {
this.currentPath = String(this.currentPath).slice(0, -1);
}
this.onlyOneFile = !!this.opt.onlyOneFile;
this.opt.choices = new Choices(this.createChoices(this.currentPath), this.answers);
this.selected = 0;
if (this.opt.filterItems) {
assert(typeof this.opt.filterItems === 'function', ' "filterItems" option property must be a function.');
}
this.firstRender = true;
// Make sure no default is set (so it won't be printed)
this.opt.default = null;
this.searchTerm = '';
this.paginator = new Paginator();
}
util.inherits(Prompt, Base);
/**
* Start the Inquiry session
* @param {Function} cb Callback when prompt is done
* @return {this}
*/
Prompt.prototype._run = function (cb) {
var self = this;
self.searchMode = false;
this.done = cb;
var alphaNumericRegex = /\w|\.|\-/i;
var events = observe(this.rl);
var keyUps = events.keypress.filter(function (e) {
return e.key.name === 'up' || (!self.searchMode && e.key.name === 'k');
}).share();
var keyDowns = events.keypress.filter(function (e) {
return e.key.name === 'down' || (!self.searchMode && e.key.name === 'j');
}).share();
var keyLefts = events.keypress.filter(function (e) {
return e.key.name === 'left';
}).share();
var keyRights = events.keypress.filter(function (e) {
return e.key.name === 'right';
}).share();
var keySlash = events.keypress.filter(function (e) {
return e.value === '/';
}).share();
// var keyMinus = events.keypress.filter(function (e) {
// return e.value === '-';
// }).share();
// var alphaNumeric = events.keypress.filter(function (e) {
// return e.key.name === 'backspace' || alphaNumericRegex.test(e.value);
// }).share();
var alphaNumeric = events.keypress.filter(function (e) {
return e.value === '-' || alphaNumericRegex.test(e.value);
}).share();
var searchTerm = keySlash.flatMap(function (md) {
self.searchMode = true;
self.searchTerm = '';
self.render();
var end$ = new rx.Subject();
var done$ = rx.Observable.merge(events.line, end$);
return alphaNumeric.map(function (e) {
// if (e.key.name === 'backspace' && self.searchTerm.length) {
if (e.value === '-' && self.searchTerm.length) {
self.searchTerm = self.searchTerm.slice(0, -1);
} else if (e.value) {
self.searchTerm += e.value;
}
if (self.searchTerm === '') {
end$.onNext(true);
}
return self.searchTerm;
})
.takeUntil(done$)
.doOnCompleted(function () {
self.searchMode = false;
self.render();
return false;
});
}).share();
var outcome = this.handleSubmit(events.line);
// outcome.drill.forEach(this.handleDrill.bind(this));
// outcome.back.forEach(this.handleBack.bind(this));
keyUps.takeUntil(outcome.done).forEach(this.onUpKey.bind(this));
keyDowns.takeUntil(outcome.done).forEach(this.onDownKey.bind(this));
keyLefts.takeUntil(outcome.done).forEach(this.handleBack.bind(this));
keyRights.takeUntil(outcome.done).forEach(this.handleDrill.bind(this));
// keyMinus.takeUntil(outcome.done).forEach(this.handleBack.bind(this));
events.keypress.takeUntil(outcome.done).forEach(this.hideKeyPress.bind(this));
searchTerm.takeUntil(outcome.done).forEach(this.onKeyPress.bind(this));
outcome.done.forEach(this.onSubmit.bind(this));
// Init the prompt
cliCursor.hide();
this.render();
return this;
};
Prompt.prototype.render = function (msg) {
if(this.backspaceHit){
return;
}
var message = (msg) || '';
if (this.firstRender && this.status !== 'answered') {
message += this.getQuestion();
message += chalk.dim('(Use arrow keys)');
}
else {
message += '\n'
}
if (this.status === 'answered') {
message += chalk.magenta(path.join(this.currentPath, this.selectedValue));
}
else {
message += '\n\n' + chalk.bold(' Current directory: ') + chalk.black(path.join(this.currentPath, '/../'))
+ chalk.magenta(path.basename(this.currentPath));
var choicesStr = listRender(this.opt.choices, this.selected);
message += '\n\n\n' + this.paginator.paginate(choicesStr, this.selected, this.opt.pageSize);
if (this.searchMode) {
message += ('\n\n => Search: ' + this.searchTerm);
} else {
message += '\n\n (Use \'/\' key to search this directory)';
}
}
this.firstRender = false;
this.screen.render(message);
};
/**
* When user press `enter` key
*/
Prompt.prototype.handleSubmit = function (e) {
// if(this.backspaceHit){
// this.screen.done();
// this.rl.removeAllListeners();
// return;
// }
var self = this;
var obx = e.map(function () {
var val;
if (val = self.opt.choices.getChoice(self.selected).value) {
self.selectedValue = val;
return val;
}
}).share();
// here is a hack, but it seems to work
var done = obx.filter(function (choice) {
// return choice === self.currentPath;
if(self.backspaceHit){
return true;
}
choice = path.isAbsolute(choice) ? choice : path.resolve(self.currentPath + path.sep + choice);
if (self.onlyOneFile && !fs.statSync(choice).isFile()) {
self.render(chalk.red(' In this case, you must select a file (not a directory).'));
return false;
}
return true;
}).take(1);
// var back = obx.filter(function (choice) {
// return choice === BACK;
// }).takeUntil(done);
//
// var drill = obx.filter(function (choice) {
// return choice !== BACK && choice !== CHOOSE_DIRECTORY;
// }).takeUntil(done);
return {
done: done,
// back: back,
// drill: drill
};
};
/**
* when user selects to drill into a folder (by selecting folder name)
*/
Prompt.prototype.handleDrill = function () {
this.pathIndexHash[ this.currentPath ] = this.selected;
var choice = this.opt.choices.getChoice(this.selected);
this.currentPath = path.normalize(path.isAbsolute(choice.value) ? choice.value : path.join(this.currentPath, choice.value));
if (String(this.currentPath).endsWith(path.sep)) {
this.currentPath = String(this.currentPath).slice(0, -1);
}
if (fs.statSync(this.currentPath).isFile()) {
this.opt.choices = new Choices(this.createChoices(this.currentPath), this.answers);
this.selected = this.pathIndexHash[ this.currentPath ] || 0;
}
else {
this.opt.choices = new Choices(this.createChoices(this.currentPath), this.answers);
this.selected = this.pathIndexHash[ this.currentPath ] || 0;
this.render();
}
};
/**
* when user selects '.. back'
*/
Prompt.prototype.handleBack = function () {
if (!this.opt.allowNavigationAboveBaseDir) {
if (this.currentPath === this.originalBaseDir) {
return;
}
}
var choice = this.opt.choices.getChoice(this.selected);
// this.currentPath = path.dirname(this.currentPath);
this.pathIndexHash[ this.currentPath ] = this.selected;
this.currentPath = path.normalize(path.join(this.currentPath, '/../'));
if (String(this.currentPath).endsWith(path.sep)) {
this.currentPath = String(this.currentPath).slice(0, -1);
}
this.opt.choices = new Choices(this.createChoices(this.currentPath), this.answers);
this.selected = this.pathIndexHash[ this.currentPath ] || 0;
this.render();
};
Prompt.prototype.onSubmit = function (value) {
if(this.backspaceHit){
this.rl.emit('close-this-shiz');
this.screen.done();
this.rl.removeAllListeners();
return;
}
const potentialDoneVal = path.join(this.currentPath, this.selectedValue);
this.status = 'answered';
this.render();
this.screen.done();
cliCursor.show();
this.done(this.opt.mapValue(potentialDoneVal));
};
Prompt.prototype.onBackspace = function () {
if (this.opt.onLeftKey) {
this.opt.onLeftKey.bind(this)();
}
};
Prompt.prototype.hideKeyPress = function () {
if (!this.searchMode) {
if (fs.statSync(this.currentPath).isFile()) {
this.render(); // not currently used
}
else {
this.render();
}
}
};
Prompt.prototype.onUpKey = function () {
var len = this.opt.choices.realLength;
this.selected = (this.selected > 0) ? this.selected - 1 : len - 1;
this.render();
};
Prompt.prototype.onDownKey = function () {
var len = this.opt.choices.realLength;
this.selected = (this.selected < len - 1) ? this.selected + 1 : 0;
this.render();
};
Prompt.prototype.onSlashKey = function (e) {
this.render();
};
Prompt.prototype.onKeyPress = function (e) {
var index = findIndex.call(this, this.searchTerm);
if (index >= 0) {
this.selected = index;
}
this.render();
};
function findIndex (term) {
var item;
for (var i = 0; i < this.opt.choices.realLength; i++) {
item = this.opt.choices.realChoices[ i ].name.toLowerCase();
if (item.indexOf(term) === 0) {
return i;
}
}
return -1;
}
/**
* Helper to create new choices based on previous selection.
*/
Prompt.prototype.createChoices = function (basePath) {
return getDirectories(basePath, this.opt.includeFiles, this.opt.filterItems);
};
/**
* Function for rendering list choices
* @param {Number} pointer Position of the pointer
* @return {String} Rendered content
*/
function listRender (choices, pointer) {
var output = '';
var separatorOffset = 0;
choices.forEach(function (choice, i) {
if (choice.type === 'separator') {
separatorOffset++;
output += ' ' + choice + '\n';
return;
}
var isSelected = (i - separatorOffset === pointer);
var line = (isSelected ? figures.pointer + ' ' : ' ') + choice.name;
if (isSelected) {
line = chalk.cyan(line);
}
output += line + ' \n';
});
return output.replace(/\n$/, '');
}
function allOK () {
return true;
}
/**
* Function for getting list of folders in directory
* @param {String} basePath the path the folder to get a list of containing folders
* @return {Array} array of folder names inside of basePath
*/
function getDirectories (basePath, includeFiles, userSuppliedFilterFn) {
var items;
try {
items = fs.readdirSync(basePath);
}
catch (err) {
// probably a file not a dir, but let's check error message to be sure
if (includeFiles && String(err.stack).match(/ENOTDIR/)) {
return [ basePath ];
}
// something is wrong, perhaps directory read permissions problem
// maybe throw err instead
console.error(err.stack);
return [];
}
return items.filter(function (file) {
var absPath = path.join(basePath, file);
var stats = fs.lstatSync(absPath);
if (stats.isSymbolicLink()) {
return false;
}
var isDir = includeFiles ? true : stats.isDirectory();
var isNotDotFile = path.basename(file).indexOf('.') !== 0;
return isDir && isNotDotFile && (userSuppliedFilterFn || allOK)(absPath);
})
.map(function (item) {
// return path.join(basePath, item);
return item;
})
.sort();
}