This commit is contained in:
Rizky 2024-08-19 11:25:47 +07:00
parent 1acd89613a
commit d193befb27
2 changed files with 45 additions and 27 deletions

View File

@ -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<string, FSWatcher>;
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);

View File

@ -6,8 +6,10 @@ import { FSWatcher, statSync, watch } from "fs";
import { readdir } from "node:fs/promises"; import { readdir } from "node:fs/promises";
import { code } from "../code"; import { code } from "../code";
import { join } from "path"; import { join } from "path";
import { Subprocess } from "bun";
export class Watcher { export class Watcher {
watchers = {} as Record<string, FSWatcher>; proc: undefined | Subprocess;
constructor(path: string, id_site: string) { constructor(path: string, id_site: string) {
this.init(path, id_site); this.init(path, id_site);
@ -33,16 +35,13 @@ export class Watcher {
}); });
}; };
const createWatcher = (p: string, recursive: boolean) => { this.proc = Bun.spawn(
return watch(p, { recursive }, (e, filename) => { ["bun", join(import.meta.dir, "init/watcher.ts"), path],
const fe = code.internal.frontend[id_site]; {
stdout: "inherit",
if ( stderr: "inherit",
filename?.endsWith(".tsx") || ipc(message, childProc) {
filename?.endsWith(".ts") || const fe = code.internal.frontend[id_site];
filename?.endsWith(".css") ||
filename?.endsWith(".html")
) {
if (typeof fe !== "undefined" && !fe.rebuilding) { if (typeof fe !== "undefined" && !fe.rebuilding) {
fe.rebuilding = true; fe.rebuilding = true;
clearTimeout(fe.timeout); clearTimeout(fe.timeout);
@ -58,28 +57,15 @@ export class Watcher {
} }
}, 500); }, 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) { } catch (e) {
console.error(e); console.error(e);
} }
} }
async close() { async close() {
for (const v of Object.values(this.watchers)) { this.proc?.kill();
v.close();
}
} }
} }