diff --git a/bun.lockb b/bun.lockb index 2a15311..d1cab85 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/internal/content/content-deploy.ts b/internal/content/content-deploy.ts index 056de21..1bc2def 100644 --- a/internal/content/content-deploy.ts +++ b/internal/content/content-deploy.ts @@ -1,11 +1,19 @@ import { g } from "utils/global"; import { ensureDeployExists } from "./deploy/ensure"; import type { PrasiContent } from "./types"; +import { config } from "utils/config"; +import { loadCurrentDeploy } from "./deploy/load"; export const prasi_content_deploy: PrasiContent = { async prepare(site_id) { await ensureDeployExists(site_id); }, + async init() { + const ts = config.current?.deploy.current; + if (ts) { + await loadCurrentDeploy(ts); + } + }, async staticFile(ctx) {}, async route(ctx) {}, }; diff --git a/internal/content/content-ipc.ts b/internal/content/content-ipc.ts index 32eb190..f44f21c 100644 --- a/internal/content/content-ipc.ts +++ b/internal/content/content-ipc.ts @@ -1,9 +1,33 @@ import { g } from "utils/global"; import type { PrasiContent } from "./types"; +import { ipcSend } from "./ipc/send"; +import { staticFile } from "utils/static"; export const prasi_content_ipc: PrasiContent = { - prepare(site_id) { - console.log("mantap jiwa"); + prepare(site_id) {}, + init() { + return new Promise((done) => { + if (g.mode === "site" && g.ipc) { + ipcSend({ type: "init" }); + if (g.server) { + console.log("restarting..."); + process.exit(); + } else { + process.on( + "message", + async (msg: { type: "start"; path: { asset: string } }) => { + if (g.mode === "site" && g.ipc) { + if (msg.type === "start") { + g.ipc.asset = await staticFile(msg.path.asset); + ipcSend({ type: "ready", port: g.server.port }); + done(); + } + } + } + ); + } + } + }); }, async staticFile(ctx) { const asset = g.mode === "site" && g.ipc?.asset!; diff --git a/internal/content/deploy/download.ts b/internal/content/deploy/download.ts index 2977f50..3fd33c1 100644 --- a/internal/content/deploy/download.ts +++ b/internal/content/deploy/download.ts @@ -4,9 +4,14 @@ import { fs } from "utils/fs"; import { siteLog } from "utils/log"; export const downloadDeployedSite = async (site_id: string) => { + if (!site_id) { + siteLog(`No site_id defined in site/site.json`); + return 0; + } + let base_url = "https://prasi.avolut.com"; const ts = Date.now(); - siteLog("Downloading site deploy: "); + siteLog(`Downloading site [${site_id}] deploy: `); await downloadFile( `${base_url}/prod-zip/${site_id}?ts=${ts}&msgpack=1`, fs.path(`site:deploy/history/${ts}.gz`), diff --git a/internal/content/deploy/ensure.ts b/internal/content/deploy/ensure.ts index 196921d..f17d3fa 100644 --- a/internal/content/deploy/ensure.ts +++ b/internal/content/deploy/ensure.ts @@ -3,6 +3,7 @@ import { fs } from "utils/fs"; import { downloadDeployedSite } from "./download"; export const ensureDeployExists = async (site_id: string) => { + if (!site_id) return 0; let download_deploy = false; const ts = config.current?.deploy.current; if (!ts) { diff --git a/internal/content/types.ts b/internal/content/types.ts index fd70112..a8406de 100644 --- a/internal/content/types.ts +++ b/internal/content/types.ts @@ -2,6 +2,7 @@ import type { ServerCtx } from "utils/server-ctx"; export type PrasiContent = { prepare: (site_id: string) => void | Promise; + init: () => Promise; staticFile: (ctx: ServerCtx) => Promise; route: (ctx: ServerCtx) => Promise; }; diff --git a/internal/server/ensure.ts b/internal/server/ensure.ts index a078fc2..c373a04 100644 --- a/internal/server/ensure.ts +++ b/internal/server/ensure.ts @@ -14,7 +14,7 @@ export const ensureServerReady = async (is_dev: boolean) => { if (is_dev) { const rebuild = async () => { try { - await $`bun build --watch --target bun --entry ${fs.path( + await $`bun build --watch --no-clear-screen --target bun --entry ${fs.path( "internal:server/server.ts" )} --outdir ${fs.path("site:app")} --sourcemap=linked`.quiet(); } catch (e) { diff --git a/internal/server/server.ts b/internal/server/server.ts index 771e6d4..048724b 100644 --- a/internal/server/server.ts +++ b/internal/server/server.ts @@ -1,45 +1,18 @@ import { config } from "utils/config"; import { g, startup } from "utils/global"; -import { prasi_content_ipc } from "../content/content-ipc"; -import { prasi_content_deploy } from "../content/content-deploy"; -import { loadCurrentDeploy } from "../content/deploy/load"; -import { ipcSend } from "../content/ipc/send"; -import { staticFile } from "utils/static"; import type { ServerCtx } from "utils/server-ctx"; import { prasiContent } from "../content/content"; +import { prasi_content_deploy } from "../content/content-deploy"; +import { prasi_content_ipc } from "../content/content-ipc"; startup("site", async () => { await config.init("site:site.json"); if (g.mode === "site") { g.content = g.ipc ? prasi_content_ipc : prasi_content_deploy; - if (g.ipc) { - ipcSend({ type: "init" }); - if (g.server) { - console.log("restarting..."); - process.exit(); - } else { - process.on( - "message", - async (msg: { type: "start"; path: { asset: string } }) => { - if (g.mode === "site" && g.ipc) { - if (msg.type === "start") { - g.ipc.asset = await staticFile(msg.path.asset); - startGlobalServer(); - ipcSend({ type: "ready", port: g.server.port }); - } - } - } - ); - } - } else { - const ts = config.current?.deploy.current; - if (ts) { - await loadCurrentDeploy(ts); - } - - startGlobalServer(); - } + const content = g.content; + await content.init(); + startGlobalServer(); } }); diff --git a/internal/server/start.ts b/internal/server/start.ts index e4c7b34..a031efb 100644 --- a/internal/server/start.ts +++ b/internal/server/start.ts @@ -1,19 +1,24 @@ import { fs } from "utils/fs"; import { g } from "utils/global"; +import { siteLog } from "utils/log"; import { spawn } from "utils/spawn"; export const startServer = (arg: { mode: "dev" | "prod" }) => { if (g.mode === "supervisor") { - g.supervisor = { - process: spawn({ - cmd: - arg.mode === "dev" - ? "bun run --watch server.js" - : "bun run server.js", - cwd: fs.path("site:app"), - mode: "passthrough", - }), - }; + if (fs.exists("site:app/server.js")) { + g.supervisor = { + process: spawn({ + cmd: + arg.mode === "dev" + ? "bun run --watch --no-clear-screen server.js" + : "bun run server.js", + cwd: fs.path("site:app"), + mode: "passthrough", + }), + }; + } else { + siteLog("No server.js found in site/app directory"); + } } else { import("./server"); } diff --git a/internal/supervisor.ts b/internal/supervisor.ts index 285e30a..1058fd3 100644 --- a/internal/supervisor.ts +++ b/internal/supervisor.ts @@ -19,18 +19,17 @@ startup("supervisor", async () => { await prasi_content_deploy.prepare(site_id); if (!site_id) { - siteLog("No Site Loaded"); + siteLog(`Warning: site_id is empty. Please set it in site.json`); } else { siteLog(`Site ID: ${site_id}`); } await ensureDBReady(); + await ensureServerReady(is_dev); } else { g.mode = "site"; if (g.mode === "site") g.ipc = {}; } - await ensureServerReady(is_dev); - startServer({ mode: is_dev ? "dev" : "prod", }); diff --git a/package.json b/package.json index 8177c79..77da045 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,8 @@ "module": "index.ts", "type": "module", "scripts": { - "dev": "bun run --watch internal/supervisor.ts --dev", - "ipc": "bun run --hot internal/supervisor.ts --dev --ipc" + "dev": "bun run --watch --no-clear-screen internal/supervisor.ts --dev", + "ipc": "bun run --hot --no-clear-screen internal/supervisor.ts --dev --ipc" }, "devDependencies": { "@types/bun": "latest" @@ -23,6 +23,7 @@ "lodash.set": "^4.3.2", "mime": "^4.0.6", "msgpackr": "^1.11.2", - "rou3": "^0.5.1" + "rou3": "^0.5.1", + "uuid": "^11.0.3" } }