This commit is contained in:
Rizky 2024-07-26 09:37:14 +07:00
parent 6f3a5a0c82
commit 9c5f43538f
1 changed files with 97 additions and 82 deletions

View File

@ -52,10 +52,128 @@ export const initFrontEnd = async (
try {
await isInstalling(id_site);
const broadcastLoading = async () => {
const client_ids = user.active
.findAll({ site_id: id_site })
.map((e) => e.client_id);
const now = Date.now();
client_ids.forEach((client_id) => {
const ws = conns.get(client_id)?.ws;
if (ws)
sendWS(ws, {
type: SyncType.Event,
event: "code_changes",
data: { ts: now, mode: "frontend", status: "building" },
});
});
};
code.internal.frontend[id_site] = {
ctx: await initBuildCtx({ id_site, root }),
timeout: null,
rebuilding: false,
watch: watch(
dir.data(root),
{
recursive: true,
},
async (event, filename) => {
const fe = code.internal.frontend[id_site];
const srv = code.internal.server[id_site];
if (
filename?.startsWith("node_modules") ||
filename?.startsWith("typings")
)
return;
if (
filename?.endsWith(".tsx") ||
filename?.endsWith(".ts") ||
filename?.endsWith(".css") ||
filename?.endsWith(".html")
) {
if (typeof fe !== "undefined" && !fe.rebuilding) {
fe.rebuilding = true;
clearTimeout(fe.timeout);
fe.timeout = setTimeout(async () => {
const build_timeout = setTimeout(async () => {
console.log(`Build unfinished ${id_site} ${filename}`);
await fe.ctx.dispose();
fe.ctx = await initBuildCtx({ id_site, root });
}, 3000);
try {
broadcastLoading();
await fe.ctx.rebuild();
clearTimeout(build_timeout);
} catch (e: any) {
console.error(`Frontend failed rebuild (site: ${id_site})`);
console.error(e.messsage);
}
fe.rebuilding = false;
}, 500);
}
if (typeof srv !== "undefined" && !srv.rebuilding && srv.ctx) {
srv.rebuilding = true;
try {
await srv.ctx.rebuild();
await server.init(id_site);
} catch (e) {}
srv.rebuilding = false;
}
}
}
),
};
const fe = code.internal.frontend[id_site];
fe.rebuilding = true;
try {
await fe.ctx.rebuild();
} catch (e) {}
fe.rebuilding = false;
} catch (e: any) {
console.error("Error building front end", id_site);
delete code.internal.frontend[id_site];
}
};
const codeError = async (id_site: string, error: string, append?: boolean) => {
const path = code.path(id_site, "site", "src", "index.log");
if (error) console.log(error);
if (append) {
await appendFile(path, error);
return;
}
await Bun.write(path, error);
};
const isInstalling = async (id_site: string) => {
const path = code.path(id_site, "site", "src", "index.log");
const file = Bun.file(path);
try {
const text = await file.text();
if (typeof text === "string" && text.startsWith("Installing dependencies"))
return true;
} catch (e) {}
return false;
};
const initBuildCtx = async ({
id_site,
root,
}: {
id_site: string;
root: string;
}) => {
const out_dir_temp = dir.data(`code/${id_site}/site/build-temp`);
const out_dir_switch = dir.data(`code/${id_site}/site/build-switch`);
const out_dir = dir.data(`code/${id_site}/site/build`);
const build_ctx = await context({
return await context({
absWorkingDir: dir.data(root),
entryPoints: ["index.tsx"],
outdir: out_dir_temp,
@ -132,107 +250,4 @@ export const initFrontEnd = async (
},
],
});
const broadcastLoading = async () => {
const client_ids = user.active
.findAll({ site_id: id_site })
.map((e) => e.client_id);
const now = Date.now();
client_ids.forEach((client_id) => {
const ws = conns.get(client_id)?.ws;
if (ws)
sendWS(ws, {
type: SyncType.Event,
event: "code_changes",
data: { ts: now, mode: "frontend", status: "building" },
});
});
};
code.internal.frontend[id_site] = {
ctx: build_ctx,
timeout: null,
rebuilding: false,
watch: watch(
dir.data(root),
{
recursive: true,
},
async (event, filename) => {
const fe = code.internal.frontend[id_site];
const srv = code.internal.server[id_site];
if (
filename?.startsWith("node_modules") ||
filename?.startsWith("typings")
)
return;
if (
filename?.endsWith(".tsx") ||
filename?.endsWith(".ts") ||
filename?.endsWith(".css") ||
filename?.endsWith(".html")
) {
console.log(`Changed ${id_site} ${filename}`, fe);
if (typeof fe !== "undefined" && !fe.rebuilding) {
fe.rebuilding = true;
clearTimeout(fe.timeout);
fe.timeout = setTimeout(async () => {
try {
broadcastLoading();
await fe.ctx.rebuild();
} catch (e: any) {
console.error(`Frontend failed rebuild (site: ${id_site})`);
console.error(e.messsage);
}
fe.rebuilding = false;
}, 500);
}
if (typeof srv !== "undefined" && !srv.rebuilding && srv.ctx) {
srv.rebuilding = true;
try {
await srv.ctx.rebuild();
await server.init(id_site);
} catch (e) {}
srv.rebuilding = false;
}
}
}
),
};
const fe = code.internal.frontend[id_site];
fe.rebuilding = true;
try {
await fe.ctx.rebuild();
} catch (e) {}
fe.rebuilding = false;
} catch (e: any) {
console.error("Error building front end", id_site);
delete code.internal.frontend[id_site];
}
};
const codeError = async (id_site: string, error: string, append?: boolean) => {
const path = code.path(id_site, "site", "src", "index.log");
if (error) console.log(error);
if (append) {
await appendFile(path, error);
return;
}
await Bun.write(path, error);
};
const isInstalling = async (id_site: string) => {
const path = code.path(id_site, "site", "src", "index.log");
const file = Bun.file(path);
try {
const text = await file.text();
if (typeof text === "string" && text.startsWith("Installing dependencies"))
return true;
} catch (e) {}
return false;
};