|
| 1 | +import type { AppInfo, PageInfo, ThunkActionCreator } from "../reducer"; |
| 2 | +import { importByPlatform } from "./platforms"; |
| 3 | +import { spawn } from "child_process"; |
| 4 | +import { dialog } from "electron"; |
| 5 | +import getPort from "get-port"; |
| 6 | +import path from "node:path"; |
| 7 | +import { v4 } from "uuid"; |
| 8 | + |
| 9 | +export const init: ThunkActionCreator = () => async (dispatch, getState) => { |
| 10 | + // first load |
| 11 | + const { adapter } = await importByPlatform(); |
| 12 | + const apps = await adapter.readAll(); |
| 13 | + |
| 14 | + if (!apps.ok) throw new Error("Failed to read apps"); |
| 15 | + dispatch({ |
| 16 | + type: "app/loaded", |
| 17 | + payload: apps.unwrap(), |
| 18 | + }); |
| 19 | + |
| 20 | + // timer |
| 21 | + setInterval(async () => { |
| 22 | + const { session } = getState(); |
| 23 | + const sessions = Object.values(session); |
| 24 | + const ports = sessions.flatMap((s) => [s.nodePort, s.windowPort]); |
| 25 | + |
| 26 | + const payloads = await Promise.allSettled<PageInfo>( |
| 27 | + ports.map((port) => |
| 28 | + fetch(`http://127.0.0.1:${port}/json`).then((res) => res.json()), |
| 29 | + ), |
| 30 | + ); |
| 31 | + const pages = payloads.flatMap((p) => |
| 32 | + p.status === "fulfilled" ? p.value : [], |
| 33 | + ); |
| 34 | + console.log(ports, pages); |
| 35 | + }, 3000); |
| 36 | +}; |
| 37 | + |
| 38 | +export const debug: ThunkActionCreator<AppInfo> = (app) => async (dispatch) => { |
| 39 | + const nodePort = await getPort(); |
| 40 | + const windowPort = await getPort(); |
| 41 | + |
| 42 | + const sp = spawn( |
| 43 | + app.exePath, |
| 44 | + [`--inspect=${nodePort}`, `--remote-debugging-port=${windowPort}`], |
| 45 | + { |
| 46 | + cwd: process.platform === "win32" ? path.dirname(app.exePath) : "/", |
| 47 | + }, |
| 48 | + ); |
| 49 | + |
| 50 | + const sessionId = v4(); |
| 51 | + dispatch({ |
| 52 | + type: "session/added", |
| 53 | + payload: { sessionId, appId: app.id, nodePort, windowPort }, |
| 54 | + }); |
| 55 | + |
| 56 | + sp.on("error", (err) => { |
| 57 | + dialog.showErrorBox(`Error: ${app.name}`, err.message); |
| 58 | + }); |
| 59 | + sp.on("close", () => { |
| 60 | + // console.log(`child process exited with code ${code}`) |
| 61 | + dispatch({ type: "session/removed", payload: sessionId }); |
| 62 | + }); |
| 63 | + |
| 64 | + const handleStdout = |
| 65 | + (isError = false) => |
| 66 | + (chunk: Buffer) => { |
| 67 | + // TODO: stderr colors |
| 68 | + console.log(isError); |
| 69 | + dispatch({ |
| 70 | + type: "session/logAppended", |
| 71 | + payload: { |
| 72 | + sessionId, |
| 73 | + content: chunk.toString(), |
| 74 | + }, |
| 75 | + }); |
| 76 | + }; |
| 77 | + |
| 78 | + if (sp.stdout) { |
| 79 | + sp.stdout.on("data", handleStdout()); |
| 80 | + } |
| 81 | + if (sp.stderr) { |
| 82 | + sp.stderr.on("data", handleStdout(true)); |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +export const debugPath: ThunkActionCreator<string> = |
| 87 | + (path) => async (dispatch) => { |
| 88 | + // TODO: |
| 89 | + }; |
0 commit comments