From ae1a27fb63bf24db5687d8b3eb8c41df57fc930d Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Mon, 27 May 2024 13:43:27 +0000 Subject: [PATCH] fix: wait for fluentci studio server to start fix: wait for fluentci studio server to start --- src/cmd/studio.ts | 102 +++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/src/cmd/studio.ts b/src/cmd/studio.ts index 154a852..1c477dd 100644 --- a/src/cmd/studio.ts +++ b/src/cmd/studio.ts @@ -70,68 +70,68 @@ async function studio({ port }: { port?: number }) { }, }).spawn(); - child.status.then(async ({ code }) => { + child.status.then(({ code }) => { if (code !== 0) { console.error("Failed to start FluentCI Studio"); Deno.exit(1); } + }); - await sleep(1000); // wait for the studio to start + await sleep(1000); // wait for the studio to start - Deno.serve( - { - port: port || 6076, - onListen: () => { - const PORT = port || 6076; - console.log( - `${green("FluentCI Studio")} is up and running on ${cyan( - `http://localhost:${PORT}` - )}` - ); - open(`http://localhost:${PORT}/project/${projectId}`).catch((err) => { - console.error(err); - }); - }, + Deno.serve( + { + port: port || 6076, + onListen: () => { + const PORT = port || 6076; + console.log( + `${green("FluentCI Studio")} is up and running on ${cyan( + `http://localhost:${PORT}` + )}` + ); + open(`http://localhost:${PORT}/project/${projectId}`).catch((err) => { + console.error(err); + }); }, - async (req) => { - const upgrade = req.headers.get("upgrade") || ""; - if (upgrade.toLowerCase() === "websocket") { - const ws = Deno.upgradeWebSocket(req); - const id = createId(); - sockets[id] = ws.socket; - - sockets[id].onmessage = (e) => { - if (e.data !== "ping") { - console.log("> socket message:", e.data); - } - sockets[id]?.send(new Date().toString()); - }; - sockets[id].onerror = (e) => - console.log("socket errored:", (e as unknown as Error).message); - sockets[id].onclose = () => delete sockets[id]; + }, + async (req) => { + const upgrade = req.headers.get("upgrade") || ""; + if (upgrade.toLowerCase() === "websocket") { + const ws = Deno.upgradeWebSocket(req); + const id = createId(); + sockets[id] = ws.socket; - return ws.response; - } + sockets[id].onmessage = (e) => { + if (e.data !== "ping") { + console.log("> socket message:", e.data); + } + sockets[id]?.send(new Date().toString()); + }; + sockets[id].onerror = (e) => + console.log("socket errored:", (e as unknown as Error).message); + sockets[id].onclose = () => delete sockets[id]; - const url = new URL(req.url); - if ( - url.pathname.endsWith("/graphql") || - url.pathname.endsWith("/graphiql") - ) { - return yoga(req); - } - url.protocol = "http"; - url.hostname = "127.0.0.1"; - url.port = FLUENTCI_STUDIO_PORT; + return ws.response; + } - return await fetch(url.href, { - headers: req.headers, - method: req.method, - body: req.body, - }); + const url = new URL(req.url); + if ( + url.pathname.endsWith("/graphql") || + url.pathname.endsWith("/graphiql") + ) { + return yoga(req); } - ); - }); + url.protocol = "http"; + url.hostname = "127.0.0.1"; + url.port = FLUENTCI_STUDIO_PORT; + + return await fetch(url.href, { + headers: req.headers, + method: req.method, + body: req.body, + }); + } + ); } export default studio;