|
| 1 | +import { spawn, type ChildProcess } from "node:child_process" |
| 2 | +import { closeSync, existsSync, mkdirSync, openSync } from "node:fs" |
| 3 | +import { homedir } from "node:os" |
| 4 | +import { dirname, join, resolve } from "node:path" |
| 5 | +import { Effect } from "effect" |
| 6 | + |
| 7 | +import { ApiInternalError, ApiNotFoundError } from "../api/errors.js" |
| 8 | + |
| 9 | +export type SkillerLaunch = { |
| 10 | + readonly alreadyRunning: boolean |
| 11 | + readonly logPath: string |
| 12 | + readonly pid: number | null |
| 13 | + readonly startedAtIso: string |
| 14 | +} |
| 15 | + |
| 16 | +type SkillerProcess = { |
| 17 | + readonly logPath: string |
| 18 | + readonly process: ChildProcess |
| 19 | + readonly startedAtIso: string |
| 20 | +} |
| 21 | + |
| 22 | +const submoduleRelativePath = join("third_party", "skiller-desktop-skills-manager") |
| 23 | +const launchLogPath = join(homedir(), ".docker-git", "logs", "skiller.log") |
| 24 | + |
| 25 | +let currentProcess: SkillerProcess | null = null |
| 26 | + |
| 27 | +const isRunning = (process: ChildProcess): boolean => |
| 28 | + process.exitCode === null && process.signalCode === null && !process.killed |
| 29 | + |
| 30 | +const findWorkspaceRoot = (startDir: string): string | null => { |
| 31 | + let current = resolve(startDir) |
| 32 | + for (;;) { |
| 33 | + if (existsSync(join(current, ".gitmodules")) && existsSync(join(current, submoduleRelativePath))) { |
| 34 | + return current |
| 35 | + } |
| 36 | + const parent = dirname(current) |
| 37 | + if (parent === current) { |
| 38 | + return null |
| 39 | + } |
| 40 | + current = parent |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +const resolveSkillerDir = (): Effect.Effect<string, ApiNotFoundError> => |
| 45 | + Effect.gen(function*(_) { |
| 46 | + const root = findWorkspaceRoot(process.cwd()) |
| 47 | + if (root === null) { |
| 48 | + return yield* _(Effect.fail(new ApiNotFoundError({ |
| 49 | + message: "docker-git workspace root with Skiller submodule was not found." |
| 50 | + }))) |
| 51 | + } |
| 52 | + const skillerDir = join(root, submoduleRelativePath) |
| 53 | + if (!existsSync(join(skillerDir, "package.json"))) { |
| 54 | + return yield* _(Effect.fail(new ApiNotFoundError({ |
| 55 | + message: `Skiller submodule is not initialized at ${skillerDir}. Run bun run skiller:init first.` |
| 56 | + }))) |
| 57 | + } |
| 58 | + return skillerDir |
| 59 | + }) |
| 60 | + |
| 61 | +const launchScript = [ |
| 62 | + "set -euo pipefail", |
| 63 | + "if [ ! -d node_modules ]; then bun install --frozen-lockfile; fi", |
| 64 | + "bun run build", |
| 65 | + "ln -sf index.mjs out/preload/index.js", |
| 66 | + "if [ -z \"${DISPLAY:-}\" ] && command -v xvfb-run >/dev/null 2>&1; then", |
| 67 | + " exec xvfb-run -a ./node_modules/electron/dist/electron --no-sandbox out/main/index.js", |
| 68 | + "fi", |
| 69 | + "exec ./node_modules/electron/dist/electron --no-sandbox out/main/index.js" |
| 70 | +].join("\n") |
| 71 | + |
| 72 | +const launchSkillerProcess = (skillerDir: string): SkillerLaunch => { |
| 73 | + mkdirSync(dirname(launchLogPath), { recursive: true }) |
| 74 | + const logFd = openSync(launchLogPath, "a") |
| 75 | + try { |
| 76 | + const child = spawn("bash", ["-lc", launchScript], { |
| 77 | + cwd: skillerDir, |
| 78 | + detached: true, |
| 79 | + env: { |
| 80 | + ...process.env, |
| 81 | + ELECTRON_ENABLE_LOGGING: "1" |
| 82 | + }, |
| 83 | + stdio: ["ignore", logFd, logFd] |
| 84 | + }) |
| 85 | + const startedAtIso = new Date().toISOString() |
| 86 | + currentProcess = { logPath: launchLogPath, process: child, startedAtIso } |
| 87 | + child.once("exit", () => { |
| 88 | + if (currentProcess?.process.pid === child.pid) { |
| 89 | + currentProcess = null |
| 90 | + } |
| 91 | + }) |
| 92 | + child.unref() |
| 93 | + return { |
| 94 | + alreadyRunning: false, |
| 95 | + logPath: launchLogPath, |
| 96 | + pid: child.pid ?? null, |
| 97 | + startedAtIso |
| 98 | + } |
| 99 | + } finally { |
| 100 | + closeSync(logFd) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +export const openSkiller = (): Effect.Effect<SkillerLaunch, ApiInternalError | ApiNotFoundError> => |
| 105 | + Effect.gen(function*(_) { |
| 106 | + if (currentProcess !== null && isRunning(currentProcess.process)) { |
| 107 | + return { |
| 108 | + alreadyRunning: true, |
| 109 | + logPath: currentProcess.logPath, |
| 110 | + pid: currentProcess.process.pid ?? null, |
| 111 | + startedAtIso: currentProcess.startedAtIso |
| 112 | + } |
| 113 | + } |
| 114 | + const skillerDir = yield* _(resolveSkillerDir()) |
| 115 | + return yield* _(Effect.try({ |
| 116 | + catch: (cause) => new ApiInternalError({ |
| 117 | + message: "Failed to launch Skiller.", |
| 118 | + cause |
| 119 | + }), |
| 120 | + try: () => launchSkillerProcess(skillerDir) |
| 121 | + })) |
| 122 | + }) |
0 commit comments