-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.js
64 lines (55 loc) · 1.78 KB
/
read.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
/*property
clearLine, close, createInterface, empty_array, forEach, freeze, hidden,
input, length, line, moveCursor, on, output, prompt, setPrompt, stdin,
stdout, write
*/
import readline from "readline";
import utils from "./utils.js";
const {empty_array} = utils;
function read(callback, prompt, options = {}) {
const input = options.input || process.stdin;
const output = options.output || process.stdout;
const hidden = options.hidden || false;
prompt = prompt || "";
const rl = readline.createInterface({
input,
output
});
// hide the inserted input value with the asterisk replacement
// taken from https://stackoverflow.com/questions/24037545/how-to-hide-password-in-the-nodejs-console
// I've found this solution a better option than muting a stream
if (hidden === true) {
rl.input.on(
"keypress",
function () {
// get the number of characters entered so far:
const len = rl.line.length;
// move cursor back to the beginning of the input:
readline.moveCursor(rl.output, -len, 0);
// clear everything to the right of the cursor:
readline.clearLine(rl.output, 1);
// replace the original input with asterisks:
empty_array(len).forEach(function () {
rl.output.write("*");
});
}
);
}
rl.setPrompt(prompt);
rl.prompt();
rl.on(
"line",
function (input) {
rl.close();
callback(input);
}
);
rl.on(
"error",
function (error) {
rl.close();
callback(undefined, error);
}
);
}
export default Object.freeze(read);