This commit is contained in:
Rizky 2024-08-16 11:31:28 +07:00
parent 44f934b4b9
commit e165919ae9
1 changed files with 52 additions and 60 deletions

View File

@ -12,76 +12,68 @@ export class Watcher {
constructor(path: string, id_site: string) { constructor(path: string, id_site: string) {
this.init(path, id_site); this.init(path, id_site);
// watcher.subscribe(path, (err, events) => {
// console.log(events);
// });
// watch(
// path,
// {
// recursive: true,
// persistent: true,
// },
// async (event, filename) => {
// }
// );
} }
async init(path: string, id_site: string) { async init(path: string, id_site: string) {
const broadcastLoading = async () => { try {
const client_ids = user.active const broadcastLoading = async () => {
.findAll({ site_id: id_site }) const client_ids = user.active
.map((e) => e.client_id); .findAll({ site_id: id_site })
.map((e) => e.client_id);
const now = Date.now(); const now = Date.now();
client_ids.forEach((client_id) => { client_ids.forEach((client_id) => {
const ws = conns.get(client_id)?.ws; const ws = conns.get(client_id)?.ws;
if (ws) if (ws)
sendWS(ws, { sendWS(ws, {
type: SyncType.Event, type: SyncType.Event,
event: "code_changes", event: "code_changes",
data: { ts: now, mode: "frontend", status: "building" }, data: { ts: now, mode: "frontend", status: "building" },
}); });
}); });
}; };
this.subcription = await watcher.subscribe(path, (err, events) => { this.subcription = await watcher.subscribe(path, (err, events) => {
for (const e of events) { for (const e of events) {
const filename = e.path.substring(path.length + 1); const filename = e.path.substring(path.length + 1);
if (e.type === "create" || e.type === "update") { if (e.type === "create" || e.type === "update") {
const fe = code.internal.frontend[id_site]; const fe = code.internal.frontend[id_site];
if ( if (
filename?.startsWith("node_modules") || filename?.startsWith("node_modules") ||
filename?.startsWith("typings") || filename?.startsWith("typings") ||
filename?.endsWith(".log") filename?.endsWith(".log")
) )
return; return;
if ( if (
filename?.endsWith(".tsx") || filename?.endsWith(".tsx") ||
filename?.endsWith(".ts") || filename?.endsWith(".ts") ||
filename?.endsWith(".css") || filename?.endsWith(".css") ||
filename?.endsWith(".html") 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);
fe.timeout = setTimeout(async () => { fe.timeout = setTimeout(async () => {
try { try {
broadcastLoading(); broadcastLoading();
await fe.ctx.rebuild(); await fe.ctx.rebuild();
fe.rebuilding = false; fe.rebuilding = false;
} catch (e: any) { } catch (e: any) {
console.error(`Frontend failed rebuild (site: ${id_site})`); console.error(`Frontend failed rebuild (site: ${id_site})`);
console.error(e.message); console.error(e.message);
fe.rebuilding = false; fe.rebuilding = false;
} }
}, 500); }, 500);
}
} }
} }
} }
} });
}); } catch (e) {
console.error(e);
}
} }
async close() { async close() {