diff --git a/internal/content/content-deploy.ts b/internal/content/content-deploy.ts new file mode 100644 index 0000000..ea73bf4 --- /dev/null +++ b/internal/content/content-deploy.ts @@ -0,0 +1,24 @@ +import { g } from "utils/global"; +import { ensureDeployExists } from "./deploy/ensure"; +import type { PrasiContent } from "./types"; + +export const prasi_content_deploy: PrasiContent = { + async prepare(site_id) { + await ensureDeployExists(site_id); + }, + async comps(comp_ids) { + return []; + }, + async file(url, options) { + return { body: "", compression: "none" }; + }, + async layouts() { + return []; + }, + async page_urls() { + return {}; + }, + async pages(page_ids) { + return []; + }, +}; diff --git a/internal/content/content-ipc.ts b/internal/content/content-ipc.ts new file mode 100644 index 0000000..3e46d8b --- /dev/null +++ b/internal/content/content-ipc.ts @@ -0,0 +1,22 @@ +import type { PrasiContent } from "./types"; + +export const prasi_content_ipc: PrasiContent = { + prepare(site_id) { + console.log("mantap jiwa"); + }, + async comps(comp_ids) { + return []; + }, + async file(url, options) { + return { body: "", compression: "none" }; + }, + async layouts() { + return []; + }, + async page_urls() { + return {}; + }, + async pages(page_ids) { + return []; + }, +}; diff --git a/internal/deploy/download.ts b/internal/content/deploy/download.ts similarity index 100% rename from internal/deploy/download.ts rename to internal/content/deploy/download.ts diff --git a/internal/deploy/ensure.ts b/internal/content/deploy/ensure.ts similarity index 100% rename from internal/deploy/ensure.ts rename to internal/content/deploy/ensure.ts diff --git a/internal/deploy/load.ts b/internal/content/deploy/load.ts similarity index 100% rename from internal/deploy/load.ts rename to internal/content/deploy/load.ts diff --git a/internal/content/types.ts b/internal/content/types.ts new file mode 100644 index 0000000..17871a5 --- /dev/null +++ b/internal/content/types.ts @@ -0,0 +1,43 @@ +export type PrasiContent = { + prepare: (site_id: string) => void | Promise; + page_urls: () => Promise>; + pages: (page_ids: string[]) => Promise; + comps: (comp_ids: string[]) => Promise; + layouts: () => Promise; + file: ( + url: string, + options?: { + accept: ("gzip" | "br" | "zstd")[]; + } + ) => Promise<{ body: any; compression: "none" | "gzip" | "br" | "zstd" }>; +}; + +export type ILayout = { + id: string; + name: string; + url: string; + content_tree: any; + is_default_layout: boolean; +}; + +export type IPage = { + id: string; + name: string; + url: string; + content_tree: any; +}; + +export type IComp = { + id: string; + content_tree: any; +}; + +export type ISiteInfo = { + id: string; + name: string; + config?: { + api_url: string; + }; + responsive: string; + domain: string; +}; diff --git a/internal/server/server-mode-deploy.ts b/internal/server/server-mode-deploy.ts new file mode 100644 index 0000000..dfd550b --- /dev/null +++ b/internal/server/server-mode-deploy.ts @@ -0,0 +1,9 @@ +import { config } from "utils/config"; +import { startup } from "utils/global"; + +startup("site", async () => { + await config.init("site:site.json"); + const ts = config.current?.deploy.current; + if (ts) { + } +}); diff --git a/internal/server/server-mode-ipc.ts b/internal/server/server-mode-ipc.ts new file mode 100644 index 0000000..253f707 --- /dev/null +++ b/internal/server/server-mode-ipc.ts @@ -0,0 +1,6 @@ +export const startServerWithIPC = () => { + return Bun.serve({ + fetch(request, server) {}, + websocket: { message(ws, message) {} }, + }); +}; diff --git a/internal/server/server.ts b/internal/server/server.ts deleted file mode 100644 index d468652..0000000 --- a/internal/server/server.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { c } from "utils/color"; -import { config } from "utils/config"; -import { g, startup } from "utils/global"; -import { siteLog } from "utils/log"; -import { loadCurrentDeploy } from "../deploy/load"; - -startup("site", async () => { - await config.init("site:site.json"); - const ts = config.current?.deploy.current; - if (ts) { - await loadCurrentDeploy(ts); - siteLog( - `Site Loaded [${c.green}${g.site.pages.length} pages${c.esc}] [${c.blue}${g.site.comps.length} components${c.esc}]` - ); - } -}); diff --git a/internal/server/start.ts b/internal/server/start.ts index 1081ca0..2d9b322 100644 --- a/internal/server/start.ts +++ b/internal/server/start.ts @@ -1,11 +1,18 @@ 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) => { - g.server = spawn({ - cmd: is_dev ? "bun run --watch server.js" : "bun run server.js", - cwd: fs.path("site:app"), - mode: "passthrough", - }); +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", + }); + } else { + g.server.bun_server = startServerWithIPC(); + prasi_content_ipc.prepare(site_id); + } }; diff --git a/internal/supervisor.ts b/internal/supervisor.ts index 31babe3..af57d0c 100644 --- a/internal/supervisor.ts +++ b/internal/supervisor.ts @@ -1,10 +1,10 @@ import { c } from "utils/color"; import { config } from "utils/config"; import { fs } from "utils/fs"; -import { startup } from "utils/global"; +import { g, startup } from "utils/global"; import { siteLog } from "utils/log"; +import { prasi_content_deploy } from "./content/content-deploy"; import { ensureDBReady } from "./db/ensure"; -import { ensureDeployExists } from "./deploy/ensure"; import { ensureServerReady } from "./server/ensure"; import { startServer } from "./server/start"; @@ -18,10 +18,14 @@ startup("supervisor", async () => { siteLog("No Site Loaded"); } else { siteLog(`Site ID: ${site_id}`); - await ensureDeployExists(site_id); + + if (g.server.mode === "deploy") { + await prasi_content_deploy.prepare(site_id); + } + await ensureServerReady(is_dev); await ensureDBReady(); - startServer(is_dev); + startServer(is_dev, site_id); } }); diff --git a/internal/utils/global.ts b/internal/utils/global.ts index 5f84f2a..7bb6c78 100644 --- a/internal/utils/global.ts +++ b/internal/utils/global.ts @@ -2,6 +2,7 @@ import { join, resolve } from "path"; import { fs } from "./fs"; import type { SiteConfig } from "./config"; import type { spawn } from "./spawn"; +import type { Server } from "bun"; if (!(globalThis as any).prasi) { (globalThis as any).prasi = {}; @@ -9,8 +10,11 @@ if (!(globalThis as any).prasi) { export const g = (globalThis as any).prasi as unknown as { dir: { root: string }; - server: ReturnType; - site: { + mode: "supervisor" | "site"; + server: + | { mode: "deploy"; process: ReturnType } + | { mode: "ipc"; bun_server: Server }; + site?: { db?: SiteConfig["db"]; layouts: { id: string; @@ -43,8 +47,11 @@ export const g = (globalThis as any).prasi as unknown as { 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 !== "--dev"); + const argv = process.argv.filter((e) => !e.startsWith("--")); if (argv.length > 2) { g.dir.root = resolve(argv[2]); } else {