import { dirAsync, removeAsync } from "fs-jetpack"; import { dir } from "./dir"; import { g } from "./global"; import { spawn } from "bun"; const decoder = new TextDecoder(); export const parcelBuild = async () => { await dirAsync("app/static"); const args = [ "node", dir.path("node_modules/.bin/parcel"), g.mode === "dev" ? "watch" : "build", "./src/index.tsx", g.mode === "dev" ? "--no-hmr" : "--no-optimize", "--dist-dir", dir.path(`app/static`), ]; g.log.info(`Building web with parcel`); if (g.mode !== "dev") { await removeAsync(dir.path("app/static")); await removeAsync(dir.path("app/web/.parcel-cache")); const parcel = spawn({ cmd: args, cwd: dir.path("app/web"), stdio: ["ignore", "inherit", "inherit"], }); await parcel.exited; } else { await new Promise((resolve) => { const parcel = spawn({ cmd: args, cwd: dir.path("app/web"), stdio: ["ignore", "pipe", "pipe"], }); g.parcel = parcel; let output = true; let decoded = false; (async () => { if (parcel.stdout) { for await (const chunk of parcel.stdout) { if (!decoded && decoder.decode(chunk).includes("✨")) { resolve(); decoded = true; } if (output) process.stdout.write(chunk); } } })(); (async () => { if (parcel.stderr) { for await (const chunk of parcel.stderr) { if (output) process.stderr.write(chunk); } } })(); const cleanup = async () => { output = false; }; process.on("SIGINT", async () => { await cleanup(); process.exit(); }); process.on("SIGTERM", async () => { await cleanup(); process.exit(); }); process.on("beforeExit", async () => { await cleanup(); }); }); } };