wip fix process

This commit is contained in:
Rizky 2024-02-01 14:28:58 +07:00
parent 4e818ae458
commit 61241d4466
4 changed files with 173 additions and 64 deletions

View File

@ -120,10 +120,26 @@ export const SyncActions = {
value: Uint8Array; value: Uint8Array;
} }
) => ({}) as boolean | ParsedScope | string, ) => ({}) as boolean | ParsedScope | string,
action: async (arg: { type: "startup-check"; site_id: string }) => action: async (
({}) as { arg: { site_id: string } & (
| { type: "startup-check" }
| { type: "startup-run" }
| { type: "startup-stop" }
)
) =>
({}) as
| undefined
| {
type: "startup-check"; type: "startup-check";
status: "disabled" | "running" | "stopped"; status: "disabled" | "running" | "stopped";
}
| {
type: "startup-run";
status: "running" | "stopped";
}
| {
type: "startup-stop";
status: "running" | "stopped";
}, },
}, },
}; };

View File

@ -1,12 +1,59 @@
import { Subprocess, spawn } from "bun";
import { SAction } from "../actions"; import { SAction } from "../actions";
import { SyncConnection } from "../type"; import { SyncConnection } from "../type";
import { code } from "../editor/code/util-code";
import { waitUntil } from "web-utils";
const code_startup = {
process: {} as Record<string, Subprocess>,
};
export const code_action: SAction["code"]["action"] = async function ( export const code_action: SAction["code"]["action"] = async function (
this: SyncConnection, this: SyncConnection,
arg
) { ) {
let result = null as unknown as Awaited< const { type } = arg;
ReturnType<SAction["code"]["action"]>
>;
return result; 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;
}
}
};

View File

@ -51,7 +51,12 @@ export const EdPopCode = () => {
p.sync.code p.sync.code
.action({ type: "startup-check", site_id: p.site.id }) .action({ type: "startup-check", site_id: p.site.id })
.then((res) => { .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"); localStorage.removeItem("vsc_opened");
if (!open) { if (!open) {
p.ui.popup.code.startup_status = "init";
p.ui.popup.code.open = false; p.ui.popup.code.open = false;
p.render(); p.render();
} }
@ -139,7 +145,7 @@ const CodeBody = () => {
popoverClassName="bg-white shadow-md" popoverClassName="bg-white shadow-md"
className={cx( className={cx(
"flex items-center px-2 w-[200px] hover:bg-blue-50 space-x-1", "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} open={local.namePicker}
onOpenChange={(open) => { onOpenChange={(open) => {
@ -185,16 +191,17 @@ const CodeBody = () => {
}} }}
></div> ></div>
</Tooltip> */} </Tooltip> */}
{p.ui.popup.code.startup_status !== "disabled" && (
<Tooltip <Tooltip
content={`Startup Script: ${p.ui.popup.code.startup_status}`} content={`Startup Script: ${p.ui.popup.code.startup_status}`}
className={cx("flex items-stretch relative border-l")} className={cx("flex items-stretch relative border-r ")}
delay={0} delay={0}
placement="bottom" placement="bottom"
> >
{["loading", "init"].includes(p.ui.popup.code.startup_status) ? ( {["loading", "init"].includes(p.ui.popup.code.startup_status) ? (
<div <div
className={cx( className={cx(
"border-r flex text-center items-center hover:bg-blue-50 cursor-pointer px-2 transition-all" "flex text-center items-center hover:bg-blue-50 cursor-pointer px-2 transition-all"
)} )}
> >
<svg <svg
@ -218,8 +225,8 @@ const CodeBody = () => {
) : ( ) : (
<div <div
className={cx( className={cx(
"border-r flex text-center items-center hover:bg-blue-50 cursor-pointer px-2 transition-all", "flex text-center items-center hover:bg-blue-50 cursor-pointer px-2 transition-all",
p.ui.popup.code.startup_status p.ui.popup.code.startup_status === "running"
? "border-b-2 border-b-green-700 bg-green-50" ? "border-b-2 border-b-green-700 bg-green-50"
: "border-b-2 border-b-transparent" : "border-b-2 border-b-transparent"
)} )}
@ -229,12 +236,52 @@ const CodeBody = () => {
: iconScrollOff, : iconScrollOff,
}} }}
onClick={() => { onClick={() => {
if (p.ui.popup.code.startup_status === "stopped") {
p.ui.popup.code.startup_status = "loading"; p.ui.popup.code.startup_status = "loading";
p.render(); 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();
}
}
});
});
}
}} }}
></div> ></div>
)} )}
</Tooltip> </Tooltip>
)}
<Tooltip <Tooltip
content={`Upload zip, will overwrite files.`} content={`Upload zip, will overwrite files.`}

View File

@ -58,7 +58,6 @@ export const genMeta = (p: GenMetaP, arg: GenMetaArg) => {
} }
genMeta(p, carg); genMeta(p, carg);
} else { } else {
console.warn("invalid item", v);
} }
} }
} }