-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompty.js
55 lines (46 loc) · 1.47 KB
/
prompty.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
/*property
ask, freeze, input, label, map, name, output, series, stdin, stdout
*/
import async from "./async.js";
import read from "./read.js";
function prompty(spec = {}) {
const input = spec.input || process.stdin;
const output = spec.output || process.stdout;
function ask(done, scheme) {
const questions = scheme.map(
function (config) {
const name = config.name;
const label = config.label + " ";
const hidden = config.hidden;
return function question(next, results = {}) {
read(
function (answer, error) {
if (error !== undefined) {
next(undefined, error);
return;
}
results[name] = answer;
next(results);
},
label,
{
input,
output,
hidden
}
);
};
}
);
return async.series(questions)(function (results, error) {
if (error) {
done(undefined, error);
}
done(results);
});
}
return Object.freeze({
ask
});
}
export default Object.freeze(prompty);