-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (83 loc) · 3 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
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
89
90
91
92
93
94
95
96
const inquirer = require('inquirer')
const chalk = require('chalk')
class story {
constructor(data) {
this.data = data
this.vars = {}
this.end = () => {}
}
/**
* Used internally, Runs a function.
* @param {string} func
*/
runFunc(func) {
const types = ["message", "choices", "prompt"]
if (!this.data[func]) {console.log(chalk.red("Cannot load function \"" + func + "\"")); return}
func = this.data[func]
const character = func.character ? func.character + ": " : ""
let message = character + func.message
message.split(" ").forEach(segment => {
if (segment.startsWith("$")) {
if (!Object.keys(this.vars).includes(segment.slice(1))) {console.log(chalk.red("Cannot get variable \"" + segment.slice(1) + "\"")); return}
message = message.replace(segment, this.vars[segment.slice(1)])
} else {
message = message
}
})
if (func.func) {func.func()}
if (!func.type) {func.type = "message"}
if (!types.includes(func.type)) {console.log(chalk.red("Type \"" + func.type + "\" Does not exist")); return}
if (!func.options && func.type != "message") {console.log(chalk.red("Did not find options for type", func.type)); return}
switch (func.type) {
case "message":
console.log(chalk.bold(chalk.green("!"), message))
if (!func.next) {this.end()}
if (func.next) {this.runFunc(func.next)}
case "choices":
if (func.type != "choices") {return}
inquirer
.prompt([
{
type: 'list',
name: 'choice',
message,
choices: Object.keys(func.options[0])
}
])
.then(choice => {
this.runFunc(func.options[0][choice.choice])
})
case "prompt":
inquirer
.prompt([
{
type: 'input',
name: 'answer',
message,
}
])
.then(choice => {
this.vars[func.options[0]] = choice.answer
if (!func.next) {this.end()}
if (func.next) {this.runFunc(func.next)}
})
default:
}
}
/**
* Starts the story
* @param {boolean} clearAll Toggles if it will clear the console
*/
async start(clearAll) {
if (clearAll) {console.clear()}
this.runFunc('start')
}
/**
* Sets what should happen after the story is done running
* @param {Function} func
*/
setEnd(func) {
this.end = func
}
}
module.exports = story