-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.mjs
75 lines (68 loc) · 2.35 KB
/
watch.mjs
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
import {readFileSync, unlinkSync} from "node:fs";
import {dirname, resolve} from "node:path";
import {fileURLToPath} from "node:url";
import EventEmitter from "node:events";
import esbuild from "esbuild";
import CheapWatch from "cheap-watch";
import {EventType, fileChangeEventToMsg, fileRemovalEventToMsg, setupWebSocketServer} from "./remote.mjs";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const config = JSON.parse(readFileSync("./config.json"));
const eventEmitter = new EventEmitter();
const wss = setupWebSocketServer(eventEmitter);
console.log(`Server is running on ${config.port}`);
eventEmitter.on(EventType.ConnectionMade, () => {
for (const [path, stats] of watchBuildFolder.paths.entries()) {
if (stats.isFile()) {
eventEmitter.emit(EventType.MessageSend, fileChangeEventToMsg({path: path}));
}
}
});
function buildScript(path) {
if (!path.endsWith(".ts") && !path.endsWith(".js")) {
return;
}
esbuild
.build({
entryPoints: [path],
outdir: resolve(config.buildFolder, dirname(path)),
platform: "browser",
format: "esm",
sourcemap: "inline",
})
.catch((reason) => console.error(reason));
}
const watchScriptsFolder = new CheapWatch({
dir: config.scriptsFolder,
});
await watchScriptsFolder.init();
watchScriptsFolder.on("+", (fileEvent) => {
if (fileEvent.stats.isFile()) {
buildScript(fileEvent.path);
}
});
watchScriptsFolder.on("-", (fileEvent) => {
if (fileEvent.stats.isFile() && (fileEvent.path.endsWith(".ts") || fileEvent.path.endsWith(".js"))) {
unlinkSync(resolve(__dirname, config.buildFolder, fileEvent.path.replace(".ts", ".js")));
}
});
const watchBuildFolder = new CheapWatch({
dir: config.buildFolder,
});
await watchBuildFolder.init();
watchBuildFolder.on("+", (fileEvent) => {
if (fileEvent.stats.isFile()) {
eventEmitter.emit(EventType.MessageSend, fileChangeEventToMsg({path: fileEvent.path}));
}
});
watchBuildFolder.on("-", (fileEvent) => {
if (fileEvent.stats.isFile()) {
eventEmitter.emit(EventType.MessageSend, fileRemovalEventToMsg({path: fileEvent.path}));
}
});
process.on("SIGINT", function () {
watchScriptsFolder.close();
watchBuildFolder.close();
wss.close();
process.exit(0);
});