-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (39 loc) · 1.13 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
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const { log, info, warn } = require('better-console');
const { clearState } = require('./utils/state');
const { getStep } = require('./utils/steps');
info('Type ? to see a list of commands');
const readLine = (stepNr = 1, showBefore = true) => {
const step = getStep(stepNr);
if (showBefore && step.before) {
step.before();
}
if(step.question) {
log(step.question());
}
rl.question('', (answer) => {
if (answer === 'exit') {
rl.close();
process.exit();
} else if (answer === 'cancel') {
clearState();
readLine();
} else if (answer === '?') {
info(`\nPossible commands: cancel / exit`);
readLine(stepNr);
} else {
try {
const goToStepNr = step.action(answer);
readLine(goToStepNr);
} catch (e) {
readLine(stepNr, false);
warn(e.message + '\n');
}
}
});
};
readLine();