UNPKG

956 BJavaScriptView Raw
1var rl = require('readline');
2
3// Convention: "no" should be the conservative choice.
4// If you mistype the answer, we'll always take it as a "no".
5// You can control the behavior on <Enter> with `isYesDefault`.
6function prompt(question, isYesDefault) {
7 if (typeof isYesDefault !== 'boolean') {
8 throw new Error('Provide explicit boolean isYesDefault as second argument.');
9 }
10 return new Promise(resolve => {
11 var rlInterface = rl.createInterface({
12 input: process.stdin,
13 output: process.stdout,
14 });
15
16 var hint = isYesDefault === true ? '[Y/n]' : '[y/N]';
17 var message = question + ' ' + hint + '\n';
18
19 rlInterface.question(message, function (answer) {
20 rlInterface.close();
21
22 var useDefault = answer.trim().length === 0;
23 if (useDefault) {
24 return resolve(isYesDefault);
25 }
26
27 var isYes = answer.match(/^(yes|y)$/i);
28 return resolve(isYes);
29 });
30 });
31};
32
33module.exports = prompt;