import { installLaunchAgent, isLaunchAgentLoaded, readLaunchAgentProgramArguments, readLaunchAgentRuntime, restartLaunchAgent, stopLaunchAgent, uninstallLaunchAgent, } from "./launchd.js"; import { installScheduledTask, isScheduledTaskInstalled, readScheduledTaskCommand, readScheduledTaskRuntime, restartScheduledTask, stopScheduledTask, uninstallScheduledTask, } from "./schtasks.js"; import type { GatewayServiceRuntime } from "./service-runtime.js"; import { installSystemdService, isSystemdServiceEnabled, readSystemdServiceExecStart, readSystemdServiceRuntime, restartSystemdService, stopSystemdService, uninstallSystemdService, } from "./systemd.js"; export type GatewayServiceInstallArgs = { env: Record; stdout: NodeJS.WritableStream; programArguments: string[]; workingDirectory?: string; environment?: Record; description?: string; }; export type GatewayService = { label: string; loadedText: string; notLoadedText: string; install: (args: GatewayServiceInstallArgs) => Promise; uninstall: (args: { env: Record; stdout: NodeJS.WritableStream; }) => Promise; stop: (args: { env?: Record; stdout: NodeJS.WritableStream; }) => Promise; restart: (args: { env?: Record; stdout: NodeJS.WritableStream; }) => Promise; isLoaded: (args: { env?: Record }) => Promise; readCommand: (env: Record) => Promise<{ programArguments: string[]; workingDirectory?: string; environment?: Record; sourcePath?: string; } | null>; readRuntime: (env: Record) => Promise; }; export function resolveGatewayService(): GatewayService { if (process.platform === "darwin") { return { label: "LaunchAgent", loadedText: "loaded", notLoadedText: "not loaded", install: async (args) => { await installLaunchAgent(args); }, uninstall: async (args) => { await uninstallLaunchAgent(args); }, stop: async (args) => { await stopLaunchAgent({ stdout: args.stdout, env: args.env, }); }, restart: async (args) => { await restartLaunchAgent({ stdout: args.stdout, env: args.env, }); }, isLoaded: async (args) => isLaunchAgentLoaded(args), readCommand: readLaunchAgentProgramArguments, readRuntime: readLaunchAgentRuntime, }; } if (process.platform === "linux") { return { label: "systemd", loadedText: "enabled", notLoadedText: "disabled", install: async (args) => { await installSystemdService(args); }, uninstall: async (args) => { await uninstallSystemdService(args); }, stop: async (args) => { await stopSystemdService({ stdout: args.stdout, env: args.env, }); }, restart: async (args) => { await restartSystemdService({ stdout: args.stdout, env: args.env, }); }, isLoaded: async (args) => isSystemdServiceEnabled(args), readCommand: readSystemdServiceExecStart, readRuntime: async (env) => await readSystemdServiceRuntime(env), }; } if (process.platform === "win32") { return { label: "Scheduled Task", loadedText: "registered", notLoadedText: "missing", install: async (args) => { await installScheduledTask(args); }, uninstall: async (args) => { await uninstallScheduledTask(args); }, stop: async (args) => { await stopScheduledTask({ stdout: args.stdout, env: args.env, }); }, restart: async (args) => { await restartScheduledTask({ stdout: args.stdout, env: args.env, }); }, isLoaded: async (args) => isScheduledTaskInstalled(args), readCommand: readScheduledTaskCommand, readRuntime: async (env) => await readScheduledTaskRuntime(env), }; } throw new Error(`Gateway service install not supported on ${process.platform}`); }