-
Notifications
You must be signed in to change notification settings - Fork 0
/
question.js
88 lines (75 loc) · 1.84 KB
/
question.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
// Copyright 2024 Joseph P Medley
'use strict';
const actions = require('./actions');
const { Confirm, Input } = require('enquirer');
const utils = require('./utils.js');
const INPUT_TYPES = {
"Confirm": Confirm,
"Input": Input
}
class _Question {
constructor(wireFrameName) {
const wireframe = utils.WIREFRAMES[wireFrameName];
for (let w in wireframe) {
this[w] = wireframe[w];
}
this.name = wireFrameName;
this.answer = null;
}
_validate() {
if (!this.question.pattern) { return true; }
const regex = RegExp(this.question.pattern, 'g');
let answer = new String(this.result());
answer = answer.valueOf();
const result = regex.exec(answer);
if (!result) {
return this.question.help;
}
return true;
}
_format(value) {
switch (this.question.type) {
case 'Confirm':
return value ? 'yes' : 'no';
default:
return value;
}
}
async _prompt() {
const Prompt = INPUT_TYPES[this.type];
const prompt = new Prompt({
name: 'question',
message: this.question,
initial: this.default,
validate: this._validate,
format: this._format,
question: this
});
this.answer = await prompt.run();
}
async ask(forPage) {
try {
await this._prompt(this.text);
} catch(e) {
throw e;
} finally {
// Start Here: Which property tells me whether the action can run.
if (this.action) {
await actions[this.action.name].run(forPage, this);
}
forPage.contents = forPage.contents.replace(this.token, this.answer);
}
}
get text() {
let text = "\n" + this.question;
if (this.default) {
text += (" (" + this.default + ")");
}
text += "\n";
return text;
}
get token() {
return "[[" + this.name + "]]";
}
}
module.exports.Question = _Question;