-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
361 lines (336 loc) · 10.2 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
const highlight = require('cli-highlight').highlight;
const languagesList = require('cli-highlight').listLanguages();
const fs = require('fs');
const _ = require('lodash');
const path = require('path');
const prompts = require('prompts');
const glob = require('glob');
/**
* @param {string} source - The source folder to read from
* @returns {string[]} - An array of folder names
*/
const getDirectories = source =>
fs.readdirSync(source, {
withFileTypes: true
})
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
/**
*
* @param {string} source - the node_modules folder
* @returns {string[]} - An array of folder names
*/
const getQuizzes = (source = 'linkedin-assessments-quizzes') => {
const dir = path.join(process.cwd(), 'node_modules', source);
const directories = getDirectories(dir).filter(directory => !directory.startsWith('.')).map(directory => ({
title: directory,
value: directory
}));
return directories;
}
const promptQuiz = async () => {
const {
value
} = await prompts({
type: 'autocomplete',
name: 'value',
message: 'Select a quiz',
choices: getQuizzes()
});
return value;
}
const promptTotal = async (max = 10) => {
const {
value
} = await prompts({
type: 'number',
name: 'value',
message: 'How many questions would you like to have?',
initial: 0,
style: 'default',
min: 2,
max
});
return value;
}
const newQuiz = async () => {
const name = await promptQuiz();
await startQuiz(name);
}
const defaultOptions = {
showAnswer: true,
showCorrectAnswer: true,
showReference: true,
showScore: true
}
const getQuizFile = async (name) => {
glob(path.join(process.cwd(), 'node_modules', 'linkedin-assessments-quizzes', name, '*quiz.md'), {windowsPathsNoEscape: true}, async (err, files) => {
if (files.length === 0) {
throw new Error(`No quiz file found in ${dir}`);
process.exit(1);
}
return files[0];
})
}
const startQuiz = async (name, opts = defaultOptions) => {
glob(path.join(process.cwd(), 'node_modules', 'linkedin-assessments-quizzes', name, '*quiz.md'), {windowsPathsNoEscape: true}, async (err, files) => {
if (err || files.length === 0) {
console.log(err || 'No quiz found');
process.exit(1);
}
const quizFile = files[0];
const questions = await extractQuestionsFromMd(`${quizFile}`);
const candidateAnswers = [];
const total = await promptTotal(questions.length);
const quizQuestions = _.sampleSize(questions, total);
for await (const node of quizQuestions) {
const question = await getQuestion(node);
const {
choices,
answers,
references
} = await getChoices(node);
const promptQuestion = await prepareQuestion(name, question, answers, choices);
console.clear();
const {
value
} = await prompts(promptQuestion);
candidateAnswers.push(await checkAnswer(answers, choices, value, references, opts));
}
if (opts.showScore) {
displayResult(candidateAnswers);
}
});
}
const getQuestion = async (node) => {
const chalk = (await import('chalk')).default;
const {
visit
} = await import('unist-util-visit')
let questionParts = [];
for await (const child of node.children) {
if (child.type === 'list') break;
if (['paragraph', 'heading', 'code'].includes(child.type)) {
visit(child, child.type, async (_node, _, parent) => {
if (_node.type === 'heading') {
visit(_node, 'heading', (_child, index, parent) => {
questionParts.push({
type: 'text',
value: chalk.dim.italic(_child.value || _child.children.map(node => node.value).join(''))
});
});
} else {
if (_node.type === 'code') {
questionParts.push({
type: 'code',
value: '\n\n' + highlight(_node.value, {
language: _node.lang,
ignoreIllegals: true,
lineNumbers: true
}) + '\n\n'
});
}
if (_node.type === 'paragraph') {
for await (const __node of _node.children) {
if (__node.type === 'inlineCode') {
questionParts.push({
type: 'inlineCode',
value: highlight(__node.value, {
language: __node.lang,
ignoreIllegals: true
})
})
} else if (__node.type === 'image') {
questionParts.push({
type: 'image',
value: __node.url
});;
}
}
}
}
})
}
}
return questionParts;
}
const getChoices = async (node) => {
const {
visit
} = await import('unist-util-visit')
const filtereChildren = [];
let listFound = false;
for (const [i, child] of node.children.entries()) {
if (child.type !== 'list' && !listFound) {
listFound = true
} else {
filtereChildren.push(child);
}
}
node.children = filtereChildren;
let choices = [];
const answers = [];
// @todo: add the references
const references = [];
visit(node, ['list', 'code'], (node, _, parent) => {
// This is a special case for the code block within the list. We need concatenate the code blocks to the choices.
if (node.type == 'code') {
choices[choices.length - 1] = choices[choices.length - 1] + '\n' + highlight(node.value, {
language: languagesList.includes[node.lang] ? node.lang : 'plaintext',
ignoreIllegals: true
});
}
if (!node.ordered) {
visit(node, 'paragraph', (node, _, parent) => {
choices.push(node.children.map(node => {
if (node.type === 'text' || node.type === 'inlineCode') {
return node.value;
} else {
if (node.type === 'code') {
return '\n' + highlight(node.value, {
language: node.lang,
ignoreIllegals: true
});
}
if (node.type === 'link')
return node.children.map(node => node.value).join('') + ': ' + node.url;
}
}).join(''));
});
} else {
visit(node, 'paragraph', (node, _, parent) => {
references.push(node.children.map(node => {
if (node.type === 'text' || node.type === 'inlineCode') {
return node.value;
} else {
if (node.type === 'link')
return node.children.map(node => node.value).join('') + ': ' + node.url;
}
}).join(''));
});
}
})
answers.push(choices.filter(value => value.includes('[x] ')).map(value => value.replace('[x] ', '')).join(''));
choices = choices.map(choice => choice.replace('[ ] ', '').replace('[x] ', ''));
return {
choices,
answers,
references
};
}
async function extractQuestionsFromMd(filename) {
const remark = await import('remark');
const remarkParse = await import('remark-parse');
const quizFile = fs.readFileSync(filename, 'utf8');
const content = remark.remark()
.use(remarkParse)
.parse(quizFile);
const questions = content.children.reduce((acc, node) => {
if (node.type == 'heading' && node.depth === 4) {
acc.push({
type: 'root',
children: [node]
});
} else {
if (acc.length > 0) {
acc[acc.length - 1].children.push(node);
}
}
return acc;
}, []);
return questions;
}
async function checkAnswer(answers, choices, value, references, opts) {
const boxen = (await import('boxen')).default;
const chalk = (await import('chalk')).default;
const isCorrect = answers.includes(choices[value]);
if (!opts.showAnswer) return isCorrect;
let message = '';
if (isCorrect) {
console.log(boxen(chalk.green.bold('✓ Correct!'), {
textAlignment: 'center',
borderColor: 'green',
borderStyle: 'round'
}));
} else {
if (opts.showCorrectAnswer) {
message = `${chalk.red.bold(`x`)} ${chalk.dim.italic(`Correct answer is:`)} ${chalk.green(answers.join(', '))}`;
} else {
message = `${chalk.red.bold(`x`)} ${chalk.red(`Incorrect`)}`;
}
console.log(boxen(message, {
textAlignment: 'center',
padding: 1,
borderColor: 'red',
borderStyle: 'round'
}));
}
if (opts.showReference) {
message = references.join('\n');
message && console.log(boxen(message, {
textAlignment: 'left',
borderColor: 'white',
borderStyle: 'round'
}));
await new Promise(resolve => setTimeout(resolve, opts.showReference? 2500:1500));
}
return isCorrect
}
async function displayResult(answers) {
const boxen = (await import('boxen')).default;
const chalk = (await import('chalk')).default;
const score = answers.reduce((acc, value) => {
if (value) {
acc++;
}
return acc;
}, 0);
let message = `${ score == answers.length? '🏆\n':''}You got ${chalk.green(score)} out of ${chalk.green(answers.length)} correct!\n`;
message += `${chalk.green.bold(`${score}/${answers.length}`)}\n`;
message += `Your score is ${Math.round((score / answers.length) * 100)}%`;
console.log(boxen(message, {
textAlignment: 'center',
padding: 1,
borderColor: 'green',
borderStyle: 'round'
}));
}
async function prepareQuestion(name, question, answers, choices) {
const message = await getQuestionText(question, name);
const promptQuestion = {
type: answers.length > 1 ? 'multiselect' : 'select',
name: 'value',
message: message.join(''),
choices: choices,
};
return promptQuestion;
}
async function getQuestionText(question, name) {
const terminalImage = (await import('terminal-image')).default;
return await (Promise.all(question.map(async (node) => {
if (node.type === 'image') {
return '\n' + await terminalImage.file(`${path.join(process.cwd(), 'node_modules', 'linkedin-assessments-quizzes', name)}/${node.value}`);
} else {
return node.value;
}
})));
}
(function () {
'use strict';
module.exports = {
getQuestionText,
prepareQuestion,
displayResult,
checkAnswer,
extractQuestionsFromMd,
getChoices,
getQuestion,
startQuiz,
newQuiz,
promptTotal,
promptQuiz,
getQuizzes,
getDirectories,
getQuizFile
};
}());