|
| 1 | +const { execSync } = require("child_process"); |
| 2 | +const fs = require("fs"); |
| 3 | +const path = require("path"); |
| 4 | +const chokidar = require("chokidar"); |
| 5 | + |
| 6 | +const srcPath = path.resolve(__dirname, "../src"); |
| 7 | +const tsconfigPath = path.resolve(__dirname, "../tsconfig.json"); |
| 8 | + |
| 9 | +const [, , exercise] = process.argv; |
| 10 | + |
| 11 | +if (!exercise) { |
| 12 | + console.log("Please specify an exercise"); |
| 13 | + process.exit(1); |
| 14 | +} |
| 15 | + |
| 16 | +const allExercises = fs.readdirSync(srcPath); |
| 17 | + |
| 18 | +let pathIndicator = ".problem."; |
| 19 | + |
| 20 | +if (process.env.SOLUTION) { |
| 21 | + pathIndicator = ".solution."; |
| 22 | +} |
| 23 | + |
| 24 | +const exercisePath = allExercises.find( |
| 25 | + (exercisePath) => |
| 26 | + exercisePath.startsWith(exercise) && exercisePath.includes(pathIndicator), |
| 27 | +); |
| 28 | + |
| 29 | +if (!exercisePath) { |
| 30 | + console.log(`Exercise ${exercise} not found`); |
| 31 | + process.exit(1); |
| 32 | +} |
| 33 | + |
| 34 | +const exerciseFile = path.resolve(srcPath, exercisePath); |
| 35 | + |
| 36 | +// One-liner for current directory |
| 37 | +chokidar.watch(exerciseFile).on("all", (event, path) => { |
| 38 | + const fileContents = fs.readFileSync(exerciseFile, "utf8"); |
| 39 | + |
| 40 | + const containsVitest = fileContents.includes("vitest"); |
| 41 | + try { |
| 42 | + console.clear(); |
| 43 | + if (containsVitest) { |
| 44 | + console.log("Running tests..."); |
| 45 | + execSync(`vitest run "${exerciseFile}" --passWithNoTests`, { |
| 46 | + stdio: "inherit", |
| 47 | + }); |
| 48 | + } |
| 49 | + console.log("Checking types..."); |
| 50 | + execSync(`tsc "${exerciseFile}" --noEmit --strict`, { |
| 51 | + stdio: "inherit", |
| 52 | + }); |
| 53 | + console.log("Typecheck complete. You finished the exercise!"); |
| 54 | + } catch (e) { |
| 55 | + console.log("Failed. Try again!"); |
| 56 | + } |
| 57 | +}); |
0 commit comments