bunext/dist/functions/server/watcher.js
2026-03-18 17:37:24 +01:00

32 lines
936 B
JavaScript

import { watch, existsSync } from "fs";
import path from "path";
import grabDirNames from "../../utils/grab-dir-names";
import rebuildBundler from "./rebuild-bundler";
const { SRC_DIR } = grabDirNames();
export default function watcher() {
watch(SRC_DIR, {
recursive: true,
persistent: true,
}, async (event, filename) => {
if (!filename)
return;
if (event !== "rename")
return;
if (global.RECOMPILING)
return;
const fullPath = path.join(SRC_DIR, filename);
const action = existsSync(fullPath) ? "created" : "deleted";
try {
global.RECOMPILING = true;
console.log(`Page ${action}: ${filename}. Rebuilding ...`);
await rebuildBundler();
}
catch (error) {
console.error(error);
}
finally {
global.RECOMPILING = false;
}
});
}