-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoxlint.watch.js
More file actions
45 lines (38 loc) · 1.21 KB
/
oxlint.watch.js
File metadata and controls
45 lines (38 loc) · 1.21 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
import { readdirSync, watch } from "node:fs";
import { join } from "path";
import { spawn } from "child_process";
import { fileURLToPath } from "url";
import { dirname } from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const watchDir = "./";
const debounceDelay = 100;
const watchedFiles = new Set();
function runLinter() {
const lintProcess = spawn("yarn", ["oxlint", "."], {
stdio: "inherit",
env: { ...process.env, FORCE_COLOR: "1" }, // ensures color output
});
lintProcess.on("error", (err) => {
console.error(`Failed to start oxlint: ${err}`);
});
}
function watchRecursive(dir) {
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const fullPath = join(dir, entry.name);
if (entry.isDirectory()) {
if (entry.name === "node_modules") continue;
watchRecursive(fullPath);
} else if (entry.name.endsWith(".ts")) {
if (watchedFiles.has(fullPath)) continue;
watchedFiles.add(fullPath);
let timeout;
watch(fullPath, () => {
clearTimeout(timeout);
timeout = setTimeout(() => runLinter(fullPath), debounceDelay);
});
}
}
}
watchRecursive(join(__dirname, watchDir));
console.log("Watching for JS file changes...");