From d193befb27a592601e636ff550b12de295961cc5 Mon Sep 17 00:00:00 2001 From: Rizky Date: Mon, 19 Aug 2024 11:25:47 +0700 Subject: [PATCH] fix --- app/srv/ws/sync/code/parts/init/watcher.ts | 32 +++++++++++++++++ app/srv/ws/sync/code/parts/watcher.ts | 40 +++++++--------------- 2 files changed, 45 insertions(+), 27 deletions(-) create mode 100644 app/srv/ws/sync/code/parts/init/watcher.ts diff --git a/app/srv/ws/sync/code/parts/init/watcher.ts b/app/srv/ws/sync/code/parts/init/watcher.ts new file mode 100644 index 00000000..7952782a --- /dev/null +++ b/app/srv/ws/sync/code/parts/init/watcher.ts @@ -0,0 +1,32 @@ +import { FSWatcher, statSync, watch } from "fs"; +import { readdir } from "node:fs/promises"; +import { join } from "path"; + +const path = process.argv.splice(2).join(" "); + +const watchers = {} as Record; + +const createWatcher = (p: string, recursive: boolean) => { + return watch(p, { recursive }, (e, filename) => { + if ( + filename?.endsWith(".tsx") || + filename?.endsWith(".ts") || + filename?.endsWith(".css") || + filename?.endsWith(".html") + ) { + process.send?.([e, filename]); + } + }); +}; + +watchers["."] = createWatcher(path, false); +const files = await readdir(path); +for (const file of files) { + if (file.startsWith(".") || file === "node_modules") continue; + const fullpath = join(path, file); + const stats = statSync(fullpath); + if (stats.isDirectory()) { + watchers[file] = createWatcher(fullpath, true); + } +} +setInterval(() => {}, 1e9); diff --git a/app/srv/ws/sync/code/parts/watcher.ts b/app/srv/ws/sync/code/parts/watcher.ts index 00d01158..3810e93f 100644 --- a/app/srv/ws/sync/code/parts/watcher.ts +++ b/app/srv/ws/sync/code/parts/watcher.ts @@ -6,8 +6,10 @@ import { FSWatcher, statSync, watch } from "fs"; import { readdir } from "node:fs/promises"; import { code } from "../code"; import { join } from "path"; +import { Subprocess } from "bun"; + export class Watcher { - watchers = {} as Record; + proc: undefined | Subprocess; constructor(path: string, id_site: string) { this.init(path, id_site); @@ -33,16 +35,13 @@ export class Watcher { }); }; - const createWatcher = (p: string, recursive: boolean) => { - return watch(p, { recursive }, (e, filename) => { - const fe = code.internal.frontend[id_site]; - - if ( - filename?.endsWith(".tsx") || - filename?.endsWith(".ts") || - filename?.endsWith(".css") || - filename?.endsWith(".html") - ) { + this.proc = Bun.spawn( + ["bun", join(import.meta.dir, "init/watcher.ts"), path], + { + stdout: "inherit", + stderr: "inherit", + ipc(message, childProc) { + const fe = code.internal.frontend[id_site]; if (typeof fe !== "undefined" && !fe.rebuilding) { fe.rebuilding = true; clearTimeout(fe.timeout); @@ -58,28 +57,15 @@ export class Watcher { } }, 500); } - } - }); - }; - - this.watchers["."] = createWatcher(path, false); - const files = await readdir(path); - for (const file of files) { - if (file.startsWith(".") || file === "node_modules") continue; - const fullpath = join(path, file); - const stats = statSync(fullpath); - if (stats.isDirectory()) { - this.watchers[file] = createWatcher(fullpath, true); + }, } - } + ); } catch (e) { console.error(e); } } async close() { - for (const v of Object.values(this.watchers)) { - v.close(); - } + this.proc?.kill(); } }