This commit is contained in:
rizrmd 2024-05-30 22:39:52 +07:00
parent ead75b7f42
commit 4e57b6bdd2
2 changed files with 41 additions and 23 deletions

View File

@ -22,9 +22,9 @@ export const initFrontEnd = async (
if (existing) { if (existing) {
if (force) { if (force) {
try { try {
await existing.dispose(); await existing.ctx.dispose();
delete code.internal.frontend[id_site]; delete code.internal.frontend[id_site];
} catch (e) { } } catch (e) {}
} else { } else {
return; return;
} }
@ -33,7 +33,7 @@ export const initFrontEnd = async (
try { try {
await isInstalling(id_site); await isInstalling(id_site);
const out_dir = dir.data(`code/${id_site}/site/build`); const out_dir = dir.data(`code/${id_site}/site/build`);
const existing = await context({ const build_ctx = await context({
absWorkingDir: dir.data(root), absWorkingDir: dir.data(root),
entryPoints: ["index.tsx"], entryPoints: ["index.tsx"],
outdir: out_dir, outdir: out_dir,
@ -61,14 +61,29 @@ export const initFrontEnd = async (
{ {
name: "prasi", name: "prasi",
async setup(setup) { async setup(setup) {
try { try {
await codeError(id_site, "Building..."); await codeError(id_site, "Building...");
setup.onStart(async () => { setup.onStart(async () => {
if (!(await isInstalling(id_site))) if (!(await isInstalling(id_site)))
await codeError(id_site, "Building..."); await codeError(id_site, "Building...");
const cur = code.internal.frontend[id_site];
if (cur) {
if (!cur.timeout) {
cur.timeout = setTimeout(async () => {
if (cur.ctx) {
cur.timeout = null;
initFrontEnd(root, id_site, true);
}
}, 5000);
}
}
}); });
setup.onEnd(async (res) => { setup.onEnd(async (res) => {
const cur = code.internal.frontend[id_site];
if (cur) {
clearTimeout(cur.timeout);
}
if (res.errors.length > 0) { if (res.errors.length > 0) {
await codeError( await codeError(
id_site, id_site,
@ -104,8 +119,8 @@ export const initFrontEnd = async (
}, },
], ],
}); });
code.internal.frontend[id_site] = existing; code.internal.frontend[id_site] = { ctx: build_ctx, timeout: null };
await existing.watch(); await build_ctx.watch();
} catch (e: any) { } catch (e: any) {
console.error("Error building front end", id_site); console.error("Error building front end", id_site);
delete code.internal.frontend[id_site]; delete code.internal.frontend[id_site];
@ -115,8 +130,7 @@ export const initFrontEnd = async (
const codeError = async (id_site: string, error: string, append?: boolean) => { const codeError = async (id_site: string, error: string, append?: boolean) => {
const path = code.path(id_site, "site", "src", "index.log"); const path = code.path(id_site, "site", "src", "index.log");
if (error) if (error) console.log(error);
console.log(error)
if (append) { if (append) {
await appendFile(path, error); await appendFile(path, error);
return; return;
@ -131,7 +145,7 @@ const isInstalling = async (id_site: string) => {
const text = await file.text(); const text = await file.text();
if (typeof text === "string" && text.startsWith("Installing dependencies")) if (typeof text === "string" && text.startsWith("Installing dependencies"))
return true; return true;
} catch (e) { } } catch (e) {}
return false; return false;
}; };
@ -179,14 +193,13 @@ const installDeps = async (
!im.startsWith("server") !im.startsWith("server")
) { ) {
const parts = im.split("/"); const parts = im.split("/");
if (im.startsWith('@')) { if (im.startsWith("@")) {
im = `${parts[0]}/${parts[1]}` im = `${parts[0]}/${parts[1]}`;
} else { } else {
im = parts[0]; im = parts[0];
} }
imports.add(im); imports.add(im);
} }
} }
} }
} }
@ -202,11 +215,10 @@ const installDeps = async (
!im.path.startsWith("lib") && !im.path.startsWith("lib") &&
!im.path.startsWith("server") !im.path.startsWith("server")
) { ) {
const parts = im.path.split("/"); const parts = im.path.split("/");
let src = im.path; let src = im.path;
if (src.startsWith('@')) { if (src.startsWith("@")) {
src = `${parts[0]}/${parts[1]}` src = `${parts[0]}/${parts[1]}`;
} else { } else {
src = parts[0]; src = parts[0];
} }
@ -219,15 +231,18 @@ const installDeps = async (
} }
if (!isEqual(imports, pkgjson)) { if (!isEqual(imports, pkgjson)) {
const pkgjson = Bun.file(code.path(id_site, "site", "src", "package.json")); const pkgjson = Bun.file(code.path(id_site, "site", "src", "package.json"));
if (!(await pkgjson.exists())) { if (!(await pkgjson.exists())) {
await Bun.write(pkgjson, JSON.stringify({ await Bun.write(
pkgjson,
JSON.stringify({
name: id_site, name: id_site,
scripts: { scripts: {
"startup": "ulimit -c 0; tailwindcss --watch -i ./app/css/global.css -o ./app/css/build.css --minify" startup:
} "ulimit -c 0; tailwindcss --watch -i ./app/css/global.css -o ./app/css/build.css --minify",
})); },
})
);
} }
await codeError( await codeError(

View File

@ -16,7 +16,10 @@ export const codeInternal = {
get frontend() { get frontend() {
if (!g.prasi_code) g.prasi_code = {}; if (!g.prasi_code) g.prasi_code = {};
if (!g.prasi_code.frontend) g.prasi_code.frontend = {}; if (!g.prasi_code.frontend) g.prasi_code.frontend = {};
return g.prasi_code.frontend as Record<SITE_ID, BuildContext>; return g.prasi_code.frontend as Record<
SITE_ID,
{ ctx: BuildContext; timeout: any }
>;
}, },
get typings() { get typings() {
if (!g.prasi_code) g.prasi_code = {}; if (!g.prasi_code) g.prasi_code = {};