-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
72 lines (57 loc) · 1.87 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
'use strict';
const prompt = require('./lib/prompt');
const getOptions = require('./lib/getOptions');
const promptly = module.exports;
promptly.prompt = (message, options) => {
options = getOptions(options);
return prompt(message, options);
};
promptly.password = (message, options) => {
options = getOptions({
silent: true, // Hide password chars
trim: false, // Do not trim so that spaces can be part of the password
default: '', // Allow empty passwords
...options,
});
return prompt(message, options);
};
promptly.confirm = (message, options) => {
options = getOptions({
trim: false, // Do not trim so that only exact matches pass the validator
...options,
});
// Unshift the validator that will coerse boolean values
options.validator.unshift((value) => {
value = value.toLowerCase();
switch (value) {
case 'y':
case 'yes':
case '1':
return true;
case 'n':
case 'no':
case '0':
return false;
default:
throw new Error(`Invalid choice: ${value}`);
}
});
return prompt(message, options);
};
promptly.choose = (message, choices, options) => {
options = getOptions({
trim: false, // Do not trim so that only exact matches pass the validator
...options,
});
// Unshift the validator that will validate the data against the choices
options.validator.unshift((value) => {
// Check if the value exists by comparing values loosely
// Additionally, use the coorced value
const index = choices.findIndex((choice) => value == choice); // eslint-disable-line eqeqeq
if (index === -1) {
throw new Error(`Invalid choice: ${value}`);
}
return choices[index];
});
return prompt(message, options);
};