-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadLine.js
60 lines (55 loc) · 1.08 KB
/
readLine.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
const readline = require('readline');
const questsArr = [];
let rl;
let index = 0;
const funs = {
exit: close
};
function close() {
console.log('谢谢使用');
rl.close();
}
function next(i) {
index = typeof i === 'undefined' ? index + 1 : i;
if (index <= questsArr.length) {
start(index);
} else {
close();
}
}
function start(i = 0) {
const item = questsArr[i];
rl.question(item.question, (data) => {
if (funs[data]) {
funs[data]();
return;
}
const type = item.fun(data, close, next);
if (type === 1) {
return;
}
if (type === false || (index + 1) >= questsArr.length) {
close();
} else {
next();
}
});
}
function quests(obj) {
if (!obj || typeof obj !== 'object') {
console.error('请注意入参');
return;
}
if (Array.isArray(obj)) {
obj.forEach(v => questsArr.push(v));
}
if (typeof obj === 'object' && obj.question) {
questsArr.push(obj);
}
rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
start();
}
module.exports = quests;