diff --git a/internal/server/server-mode-ipc.ts b/internal/server/server-mode-ipc.ts deleted file mode 100644 index 253f707..0000000 --- a/internal/server/server-mode-ipc.ts +++ /dev/null @@ -1,6 +0,0 @@ -export const startServerWithIPC = () => { - return Bun.serve({ - fetch(request, server) {}, - websocket: { message(ws, message) {} }, - }); -}; diff --git a/internal/server/server-mode-deploy.ts b/internal/server/server.ts similarity index 61% rename from internal/server/server-mode-deploy.ts rename to internal/server/server.ts index dfd550b..9a88b17 100644 --- a/internal/server/server-mode-deploy.ts +++ b/internal/server/server.ts @@ -1,9 +1,10 @@ import { config } from "utils/config"; -import { startup } from "utils/global"; +import { g, startup } from "utils/global"; startup("site", async () => { await config.init("site:site.json"); const ts = config.current?.deploy.current; if (ts) { + // g.server = process.argv.includes("--ipc") ? "ipc" : "gz"; } }); diff --git a/internal/server/start.ts b/internal/server/start.ts index 2d9b322..3b482f0 100644 --- a/internal/server/start.ts +++ b/internal/server/start.ts @@ -1,18 +1,20 @@ import { fs } from "utils/fs"; import { g } from "utils/global"; import { spawn } from "utils/spawn"; -import { prasi_content_ipc } from "../content/content-ipc"; -import { startServerWithIPC } from "./server-mode-ipc"; -export const startServer = (is_dev: boolean, site_id: string) => { - if (g.server.mode === "deploy") { - g.server.process = spawn({ - cmd: is_dev ? "bun run --watch server.js" : "bun run server.js", - cwd: fs.path("site:app"), - mode: "passthrough", - }); +export const startServer = (arg: { site_id: string; 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", + }), + }; } else { - g.server.bun_server = startServerWithIPC(); - prasi_content_ipc.prepare(site_id); + import("./server"); } }; diff --git a/internal/supervisor.ts b/internal/supervisor.ts index af57d0c..970fc47 100644 --- a/internal/supervisor.ts +++ b/internal/supervisor.ts @@ -9,6 +9,7 @@ import { ensureServerReady } from "./server/ensure"; import { startServer } from "./server/start"; const is_dev = process.argv.includes("--dev"); +const is_ipc = process.argv.includes("--ipc"); startup("supervisor", async () => { console.log(`${c.green}Prasi Server:${c.esc} ${fs.path("site:")}`); await config.init("site:site.json"); @@ -19,13 +20,21 @@ startup("supervisor", async () => { } else { siteLog(`Site ID: ${site_id}`); - if (g.server.mode === "deploy") { + if (!is_ipc) { await prasi_content_deploy.prepare(site_id); } await ensureServerReady(is_dev); await ensureDBReady(); - startServer(is_dev, site_id); + if (is_ipc) { + g.mode = "site"; + if (g.mode === "site") g.ipc = true; + } + + startServer({ + site_id, + mode: is_dev ? "dev" : "prod", + }); } }); diff --git a/internal/utils/global.ts b/internal/utils/global.ts index 7bb6c78..1a2673e 100644 --- a/internal/utils/global.ts +++ b/internal/utils/global.ts @@ -1,8 +1,8 @@ -import { join, resolve } from "path"; -import { fs } from "./fs"; -import type { SiteConfig } from "./config"; -import type { spawn } from "./spawn"; import type { Server } from "bun"; +import { join, resolve } from "path"; +import type { SiteConfig } from "./config"; +import { fs } from "./fs"; +import type { PrasiSpawn, spawn } from "./spawn"; if (!(globalThis as any).prasi) { (globalThis as any).prasi = {}; @@ -10,45 +10,50 @@ if (!(globalThis as any).prasi) { export const g = (globalThis as any).prasi as unknown as { dir: { root: string }; - mode: "supervisor" | "site"; - server: - | { mode: "deploy"; process: ReturnType } - | { mode: "ipc"; bun_server: Server }; - site?: { - db?: SiteConfig["db"]; - layouts: { - id: string; - name: string; - url: string; - content_tree: any; - is_default_layout: boolean; - }[]; - pages: { - id: string; - name: string; - url: string; - content_tree: any; - }[]; - comps: { - id: string; - content_tree: any; - }[]; - info: { - id: string; - name: string; - config?: { - api_url: string; +} & ( + | { + mode: "site"; + server: Server; + ipc: boolean; + site: { + db?: SiteConfig["db"]; + layouts: { + id: string; + name: string; + url: string; + content_tree: any; + is_default_layout: boolean; + }[]; + pages: { + id: string; + name: string; + url: string; + content_tree: any; + }[]; + comps: { + id: string; + content_tree: any; + }[]; + info: { + id: string; + name: string; + config?: { + api_url: string; + }; + responsive: string; + domain: string; + }; }; - responsive: string; - domain: string; - }; - }; -}; + } + | { + mode: "supervisor"; + supervisor: { process: PrasiSpawn }; + } +); export const startup = (mode: "supervisor" | "site", fn: () => void) => { g.dir = { root: "" }; g.mode = mode; - g.server.mode = process.argv.includes("--ipc") ? "ipc" : "deploy"; if (mode === "supervisor") { const argv = process.argv.filter((e) => !e.startsWith("--")); diff --git a/internal/utils/spawn.ts b/internal/utils/spawn.ts index 605909e..7dad004 100644 --- a/internal/utils/spawn.ts +++ b/internal/utils/spawn.ts @@ -1,6 +1,7 @@ import { spawn as bunSpawn, type Subprocess } from "bun"; import { Readable } from "node:stream"; +export type PrasiSpawn = ReturnType; export const spawn = ( arg: { cmd: string; diff --git a/package.json b/package.json index 4e8df9a..65c5e58 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,8 @@ "module": "index.ts", "type": "module", "scripts": { - "dev": "bun run --watch internal/supervisor.ts --dev" + "dev": "bun run --watch internal/supervisor.ts --dev", + "ipc": "bun run --watch internal/supervisor.ts --dev --ipc" }, "devDependencies": { "@types/bun": "latest"