-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
106 lines (88 loc) · 1.97 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
/**
* Module dependencies.
*/
var keypress = require('keypress');
/**
* Expose `prompt`.
*/
exports = module.exports = prompt;
/**
* Prompt for user input.
*/
function prompt(msg){
return function(done){
process.stdout.write(msg);
process.stdin.setEncoding('utf8');
process.stdin.once('data', function(val){
done(null, val.trim());
}).resume();
}
}
/**
* Prompt for multi-line user input.
*/
exports.multiline = function(msg){
return function(done){
var buf = [];
console.log(msg);
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(val){
if ('\n' == val || '\r\n' == val) {
process.stdin.removeAllListeners('data');
done(null, buf.join('\n'));
} else {
buf.push(val.trimRight());
}
}).resume();
}
};
/**
* Prompt for confirmation.
*/
exports.confirm = function(msg){
return function(done){
prompt(msg)(function(err, val){
if (err) return done(err);
done(null, bool(val));
});
}
};
/**
* Prompt for password with optional mask.
*/
exports.password = function(msg, mask){
mask = null == mask ? '*' : mask;
return function(done){
var buf = '';
keypress(process.stdin);
process.stdin.setRawMode(true);
process.stdout.write(msg);
process.stdin.on('keypress', function(c, key){
if (key && 'return' == key.name) {
console.log();
process.stdin.pause();
process.stdin.removeAllListeners('keypress');
process.stdin.setRawMode(false);
if (!buf.trim().length) return exports.password(msg, mask)(done);
done(null, buf);
return;
}
if (key && key.ctrl && 'c' == key.name) {
console.log('%s', buf);
process.exit();
}
process.stdout.write(mask);
buf += c;
}).resume();
}
};
/**
* Parse a boolean `str`.
*
* @param {String} str
* @return {Boolean}
* @api private
*/
function bool(str) {
return /^y|yes|ok|true$/i.test(str);
}