Repository navigation
Expand file tree
/
Copy pathvalidate-content.mjs
More file actions
54 lines (46 loc) · 1.91 KB
/
Copy pathvalidate-content.mjs
File metadata and controls
54 lines (46 loc) · 1.91 KB
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
import { access } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { tutorial } from "../tutorial-data.js";
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const failures = [];
if (!tutorial?.lessons?.length) {
failures.push("Tutorial data must contain at least one lesson.");
}
for (const lesson of tutorial?.lessons ?? []) {
const label = lesson.id || "unnamed lesson";
if (!/^[A-Za-z_$][A-Za-z0-9_$]*\.java$/.test(lesson.exercise?.filename || "")) {
failures.push(`${label}: exercise needs a Java filename.`);
}
for (const check of lesson.exercise?.checks ?? []) {
if (!new RegExp(check.pattern, "m").test(lesson.exercise.solutionCode)) {
failures.push(`${label}: the provided solution fails the check “${check.message}”`);
}
}
if (lesson.exercise?.guide) {
const guidePath = lesson.exercise.guide.image.split(/[?#]/)[0];
try {
await access(resolve(root, guidePath));
} catch {
failures.push(`${label}: guide image does not exist: ${guidePath}`);
}
}
for (const question of lesson.questions ?? []) {
if (question.type === "radio") {
if (!Array.isArray(question.choices) || !Number.isInteger(question.correct) || !question.choices[question.correct]) {
failures.push(`${label}: radio question ${question.id} has an invalid correct answer.`);
}
} else if (question.type === "text") {
if (!Array.isArray(question.accepted) || question.accepted.length === 0) {
failures.push(`${label}: text question ${question.id} needs at least one accepted answer.`);
}
} else {
failures.push(`${label}: question ${question.id} uses unsupported type ${question.type}.`);
}
}
}
if (failures.length) {
console.error(failures.join("\n"));
process.exit(1);
}
console.log(`Validated ${tutorial.lessons.length} lessons and their solutions.`);