forked from total-typescript/beginners-typescript-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise.js
More file actions
57 lines (46 loc) · 1.46 KB
/
exercise.js
File metadata and controls
57 lines (46 loc) · 1.46 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
55
56
57
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const chokidar = require("chokidar");
const srcPath = path.resolve(__dirname, "../src");
const tsconfigPath = path.resolve(__dirname, "../tsconfig.json");
const [, , exercise] = process.argv;
if (!exercise) {
console.log("Please specify an exercise");
process.exit(1);
}
const allExercises = fs.readdirSync(srcPath);
let pathIndicator = ".problem.";
if (process.env.SOLUTION) {
pathIndicator = ".solution.";
}
const exercisePath = allExercises.find(
(exercisePath) =>
exercisePath.startsWith(exercise) && exercisePath.includes(pathIndicator),
);
if (!exercisePath) {
console.log(`Exercise ${exercise} not found`);
process.exit(1);
}
const exerciseFile = path.resolve(srcPath, exercisePath);
// One-liner for current directory
chokidar.watch(exerciseFile).on("all", (event, path) => {
const fileContents = fs.readFileSync(exerciseFile, "utf8");
const containsVitest = fileContents.includes("vitest");
try {
console.clear();
if (containsVitest) {
console.log("Running tests...");
execSync(`vitest run "${exerciseFile}" --passWithNoTests`, {
stdio: "inherit",
});
}
console.log("Checking types...");
execSync(`tsc "${exerciseFile}" --noEmit --strict`, {
stdio: "inherit",
});
console.log("Typecheck complete. You finished the exercise!");
} catch (e) {
console.log("Failed. Try again!");
}
});