import { spawnSync, type SpawnSyncOptionsWithStringEncoding } from "node:child_process"; import { fileURLToPath } from "node:url"; import { dirname, resolve } from "node:path"; export interface AutostartInstallOpts { port?: number; bind?: string; dbPath?: string; } export interface AutostartManager { readonly mode: "default" | "service"; install(opts: AutostartInstallOpts): Promise; uninstall(purge: boolean): Promise; start(): Promise; stop(): Promise; status(): Promise<"Running" | "Stopped" | "NotInstalled">; } export interface RunResult { status: number; stdout: string; stderr: string; } export function run(file: string, args: string[]): RunResult { const opts: SpawnSyncOptionsWithStringEncoding = { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], shell: false, }; const r = spawnSync(file, args, opts); return { status: r.status ?? -1, stdout: typeof r.stdout === "string" ? r.stdout : "", stderr: typeof r.stderr === "string" ? r.stderr : "", }; } export function cliEntry(): { node: string; script: string } { const here = dirname(fileURLToPath(import.meta.url)); return { node: process.execPath, script: resolve(here, "..", "cli.js"), }; } export async function autostartManager(mode: "default" | "service" = "default"): Promise { if (process.platform === "win32") { const mod = await import("./windows.js"); return mod.windowsManager(mode); } if (process.platform === "darwin") { const mod = await import("./darwin.js"); return mod.darwinManager(); } const mod = await import("./linux.js"); return mod.linuxManager(); }