-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
314 lines (269 loc) · 7.94 KB
/
cli.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env node
import path from "path";
import { exec } from "child_process";
import { fileURLToPath } from "url";
import cac from "cac";
import axios from "axios";
import c from "ansi-colors";
import log from "log-utils";
import FileHound from "filehound";
import Readline from "readline";
import { promisify } from "util";
import { resolve as moduleResolve } from "mlly";
import {
readDataFile,
validateQuestionObject,
shuffleWord,
successMsg,
errorMsg,
isFn,
} from "./src/index.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const execP = promisify(exec);
const cli = cac();
let roundNumber;
cli.option("--file [file]", "Use some specified data file for questions data", {
default: path.join(__dirname, "data", "default.json"),
});
cli.option(
"--suite [suite]",
"Use specified suite (several game rounds from all json files inside the directory) to run questions from"
);
cli.option(
"--suite-folder [suite]",
"Use specified suite (several game rounds from all json files inside the directory) to run questions from",
{
default: path.join(__dirname, "data", "suite"),
}
);
cli.option("--suite-item [suiteItem]", "Use specified suite by its name");
cli.option("--plugin <plugin>", "Use plugin as a source of questions");
cli.option("--http <http>", "Use http resource as a source of questions");
cli.option(
"--answer-display-time",
"How long to display each answer in summary when game was finished (in seconds)",
{
default: 7,
}
);
cli.option("--round-number <roundNumber>", "Round number");
let readline;
const askP = (questionText) => {
return new Promise((resolve) => {
readline.question(questionText, resolve);
});
};
let correctAnswerDisplayTime;
cli
.command("", "Start a new round of word snatchers game")
.action(async (options) => {
correctAnswerDisplayTime = options.answerDisplayTime;
if (correctAnswerDisplayTime < 1) {
correctAnswerDisplayTime = 1;
}
if (options.roundNumber) {
roundNumber = options.roundNumber;
}
if (options.suite) {
const { suiteItem, suiteFolder } = options;
let files;
if (suiteItem) {
files = [
path.join(
suiteFolder,
suiteItem.endsWith(".json") ? suiteItem : `${suiteItem}.json`
),
];
} else {
files = await FileHound.create().paths(suiteFolder).ext("json").find();
}
await runGameRounds(files);
} else if (options.plugin) {
const invert = (p) => new Promise((res, rej) => p.then(rej, res));
const firstOf = (ps) => invert(Promise.all(ps.map(invert)));
const resolvePlugin = async (globalDirLookupCmd) => {
return new Promise(async (resolve, reject) => {
try {
const globalDirLookupResult = await execP(globalDirLookupCmd);
resolve(
await moduleResolve(options.plugin, {
url: globalDirLookupResult.stdout,
})
);
} catch (err) {
reject(err);
}
});
};
try {
const pathToPlugin = await firstOf([
resolvePlugin("yarn global dir"),
resolvePlugin("npm root -g"),
]);
const { default: PluginKlass } = await import(pathToPlugin);
const pluginInstance = new PluginKlass();
runFromPlugin(pluginInstance);
} catch (err) {
errorMsg(
`It seems like a specified plugin \`${options.plugin}\` has not been installed globally.`
);
}
} else if (options.http) {
await runFromHttp(options.http);
} else {
await runGameRounds([options.file]);
}
});
const state = [];
const populateState = (obj) => {
try {
obj.map(validateQuestionObject);
} catch (err) {
errorMsg(
`An error occured while getting question from a source:\n ${err.message}\n`
);
console.log(
`${log.info}. Please check that question objects in a source have required keys.`
);
process.exit(1);
}
state.push({ items: obj });
};
const roundResults = [];
let curRoundIndex = 0;
let curQuestionIndex = 0;
const askRecursive = async (question) => {
validateQuestionObject(question);
console.clear();
let init = "";
if (state.length > 1) {
init = `Round #${curRoundIndex + 1}; Question #${curQuestionIndex + 1}\n`;
}
const msg =
init +
`
Definition: ${c.red(question.definition)}
Letters: ${c.cyan(shuffleWord(question.word))}
Type your answer: `;
const userAnswer = await askP(`${msg}`);
const { definition } = question;
const word = isFn(question.getCorrectWord)
? question.getCorrectWord()
: question.word;
const correct = isFn(question.checkAnswer)
? question.checkAnswer(userAnswer)
: word === userAnswer;
roundResults[curRoundIndex] ||= [];
roundResults[curRoundIndex].push({
word,
definition,
answer: userAnswer,
correct,
});
if (correct) {
successMsg(userAnswer);
} else {
errorMsg(userAnswer);
}
curQuestionIndex++;
if (curQuestionIndex + 1 > state[curRoundIndex].items.length) {
if (curRoundIndex + 1 === state.length) {
readline.close();
let passed = 0;
roundResults.map((curr, index) => {
const correctAnswerCount = curr.reduce(
(acc, obj) => (obj.correct ? acc + 1 : acc),
0
);
curr.map((questionItem, questionIndex) => {
passed++;
setTimeout(
() => {
console.clear();
let summaryMsg = "";
if (state.length > 1) {
summaryMsg += `Round #${index + 1} results.\n`;
}
summaryMsg += `Summary: ${c.bold(
correctAnswerCount
)} correct answer(s) out of ${c.bold(curr.length)}.\n\n`;
console.log(summaryMsg);
const askedQuestion = `${c.bgBlue.bold(
` #${questionIndex + 1} `
)} Definition: ${c.gray(questionItem.definition)}`;
const { word, answer, correct } = questionItem;
const status = correct ? log.success : log.error;
let answerPanel = `${status} ${answer}`;
if (!correct) {
answerPanel = `${answerPanel}\n${log.success} ${word}`;
}
console.log(`${askedQuestion}\n\n${answerPanel}`);
},
passed === 1 ? 500 : passed * correctAnswerDisplayTime * 500
);
});
});
return;
} else {
curRoundIndex++;
curQuestionIndex = 0;
}
}
setTimeout(async () => {
await askRecursive(state[curRoundIndex].items[curQuestionIndex]);
}, 500);
};
const runFromPlugin = async (pluginInstance) => {
const items = pluginInstance.build();
await startGame(items);
};
const runFromHttp = async (resourceOrResources) => {
const resp = await axios.get(resourceOrResources);
await startGame(resp.data);
};
const startGame = async (items) => {
populateStateFromItems(items);
initReadline();
await askRecursive(state[0].items[0]);
};
const populateStateFromItems = (items) => {
if (items.every(Array.isArray)) {
if (roundNumber !== undefined) {
const curr = items[roundNumber - 1];
populateNthRound(curr, 1);
} else {
items.map((itemList, i) => {
populateNthRound(itemList, i + 1);
});
}
} else {
populateNthRound(items, 1);
}
};
const initReadline = () => {
readline = Readline.createInterface({
input: process.stdin,
output: process.stdout,
});
};
const runGameRounds = async (files) => {
initReadline();
let roundNum = 1;
files.map((file) => {
const data = readDataFile(file);
const items = data.items;
populateState(items, roundNum);
});
await askRecursive(state[0].items[0]);
};
const populateNthRound = (items, n) => {
if ("items" in items[0]) {
populateState(items[0].items, n);
} else {
populateState(items, n);
}
};
cli.help();
cli.version("3.0.0");
cli.parse();