From 61241d44669351733896b450d776f28ff48d80ef Mon Sep 17 00:00:00 2001 From: Rizky Date: Thu, 1 Feb 2024 14:28:58 +0700 Subject: [PATCH] wip fix process --- app/srv/ws/sync/actions.ts | 26 ++- app/srv/ws/sync/actions/code_action.ts | 61 ++++++- app/web/src/nova/ed/panel/popup/code/code.tsx | 149 ++++++++++++------ app/web/src/nova/vi/meta/meta.ts | 1 - 4 files changed, 173 insertions(+), 64 deletions(-) diff --git a/app/srv/ws/sync/actions.ts b/app/srv/ws/sync/actions.ts index 473cbb6a..1043e2aa 100644 --- a/app/srv/ws/sync/actions.ts +++ b/app/srv/ws/sync/actions.ts @@ -120,10 +120,26 @@ export const SyncActions = { value: Uint8Array; } ) => ({}) as boolean | ParsedScope | string, - action: async (arg: { type: "startup-check"; site_id: string }) => - ({}) as { - type: "startup-check"; - status: "disabled" | "running" | "stopped"; - }, + action: async ( + arg: { site_id: string } & ( + | { type: "startup-check" } + | { type: "startup-run" } + | { type: "startup-stop" } + ) + ) => + ({}) as + | undefined + | { + type: "startup-check"; + status: "disabled" | "running" | "stopped"; + } + | { + type: "startup-run"; + status: "running" | "stopped"; + } + | { + type: "startup-stop"; + status: "running" | "stopped"; + }, }, }; diff --git a/app/srv/ws/sync/actions/code_action.ts b/app/srv/ws/sync/actions/code_action.ts index 2cb00b3a..88cdb4ef 100644 --- a/app/srv/ws/sync/actions/code_action.ts +++ b/app/srv/ws/sync/actions/code_action.ts @@ -1,12 +1,59 @@ - -import { SAction } from "../actions"; +import { Subprocess, spawn } from "bun"; +import { SAction } from "../actions"; import { SyncConnection } from "../type"; +import { code } from "../editor/code/util-code"; +import { waitUntil } from "web-utils"; + +const code_startup = { + process: {} as Record, +}; + export const code_action: SAction["code"]["action"] = async function ( this: SyncConnection, + arg ) { - let result = null as unknown as Awaited< - ReturnType - >; + const { type } = arg; - return result; -} \ No newline at end of file + switch (type) { + case "startup-check": { + const cs = code_startup.process[arg.site_id]; + + if (!cs) { + const pkg_file = Bun.file( + code.path(arg.site_id, "site", "src", "package.json") + ); + + const pkg_json = await pkg_file.json(); + if (!pkg_json.scripts || !pkg_json.scripts.startup) { + return { type, status: "disabled" }; + } + + return { type, status: "stopped" }; + } + + return { type, status: cs.killed ? "stopped" : "running" }; + } + case "startup-run": { + if ( + !code_startup.process[arg.site_id] || + (code_startup.process[arg.site_id] && + !code_startup.process[arg.site_id].killed) + ) { + code_startup.process[arg.site_id] = spawn({ + cmd: ["npm", "run", "startup"], + cwd: code.path(arg.site_id, "site", "src"), + }); + await waitUntil(1000); + } + break; + } + case "startup-stop": { + const cs = code_startup.process[arg.site_id]; + if (cs && !cs.killed) { + cs.kill(); + await cs.exited; + } + break; + } + } +}; diff --git a/app/web/src/nova/ed/panel/popup/code/code.tsx b/app/web/src/nova/ed/panel/popup/code/code.tsx index 9d34827a..0d277d4d 100644 --- a/app/web/src/nova/ed/panel/popup/code/code.tsx +++ b/app/web/src/nova/ed/panel/popup/code/code.tsx @@ -51,7 +51,12 @@ export const EdPopCode = () => { p.sync.code .action({ type: "startup-check", site_id: p.site.id }) .then((res) => { - console.log(res); + if (res) { + if (res.type === "startup-check") { + p.ui.popup.code.startup_status = res.status; + p.render(); + } + } }); } @@ -63,6 +68,7 @@ export const EdPopCode = () => { localStorage.removeItem("vsc_opened"); if (!open) { + p.ui.popup.code.startup_status = "init"; p.ui.popup.code.open = false; p.render(); } @@ -139,7 +145,7 @@ const CodeBody = () => { popoverClassName="bg-white shadow-md" className={cx( "flex items-center px-2 w-[200px] hover:bg-blue-50 space-x-1", - "cursor-pointer justify-between" + "cursor-pointer justify-between border-r " )} open={local.namePicker} onOpenChange={(open) => { @@ -185,56 +191,97 @@ const CodeBody = () => { }} > */} - - {["loading", "init"].includes(p.ui.popup.code.startup_status) ? ( -
- + {["loading", "init"].includes(p.ui.popup.code.startup_status) ? ( +
- - - - - -
- ) : ( -
{ - p.ui.popup.code.startup_status = "loading"; - p.render(); - }} - >
- )} - + + + + + + +
+ ) : ( +
{ + if (p.ui.popup.code.startup_status === "stopped") { + p.ui.popup.code.startup_status = "loading"; + p.render(); + p.sync.code + .action({ type: "startup-run", site_id: p.site.id }) + .then(() => { + p.sync.code + .action({ + type: "startup-check", + site_id: p.site.id, + }) + .then((res) => { + if (res) { + if (res.type === "startup-check") { + p.ui.popup.code.startup_status = res.status; + p.render(); + } + } + }); + }); + } else { + p.ui.popup.code.startup_status = "loading"; + p.render(); + p.sync.code + .action({ type: "startup-stop", site_id: p.site.id }) + .then(() => { + p.sync.code + .action({ + type: "startup-check", + site_id: p.site.id, + }) + .then((res) => { + if (res) { + if (res.type === "startup-check") { + p.ui.popup.code.startup_status = res.status; + p.render(); + } + } + }); + }); + } + }} + >
+ )} +
+ )} { } genMeta(p, carg); } else { - console.warn("invalid item", v); } } }