This commit is contained in:
Rizky 2024-12-19 17:43:34 +07:00
parent e2a4a1a1f2
commit 683a050de8
7 changed files with 72 additions and 59 deletions

View File

@ -1,6 +0,0 @@
export const startServerWithIPC = () => {
return Bun.serve({
fetch(request, server) {},
websocket: { message(ws, message) {} },
});
};

View File

@ -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";
}
});

View File

@ -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",
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");
}
};

View File

@ -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",
});
}
});

View File

@ -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,11 +10,12 @@ 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<typeof spawn> }
| { mode: "ipc"; bun_server: Server };
site?: {
} & (
| {
mode: "site";
server: Server;
ipc: boolean;
site: {
db?: SiteConfig["db"];
layouts: {
id: string;
@ -43,12 +44,16 @@ export const g = (globalThis as any).prasi as unknown as {
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("--"));

View File

@ -1,6 +1,7 @@
import { spawn as bunSpawn, type Subprocess } from "bun";
import { Readable } from "node:stream";
export type PrasiSpawn = ReturnType<typeof spawn>;
export const spawn = (
arg: {
cmd: string;

View File

@ -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"