refactor: consolidate daemon runtime and start hints

This commit is contained in:
Peter Steinberger
2026-03-07 21:00:55 +00:00
parent a91731a831
commit b955ba1688
5 changed files with 161 additions and 75 deletions

View File

@@ -0,0 +1,71 @@
import { describe, expect, it } from "vitest";
import { buildPlatformRuntimeLogHints, buildPlatformServiceStartHints } from "./runtime-hints.js";
describe("buildPlatformRuntimeLogHints", () => {
it("renders launchd log hints on darwin", () => {
expect(
buildPlatformRuntimeLogHints({
platform: "darwin",
env: {
OPENCLAW_STATE_DIR: "/tmp/openclaw-state",
OPENCLAW_LOG_PREFIX: "gateway",
},
systemdServiceName: "openclaw-gateway",
windowsTaskName: "OpenClaw Gateway",
}),
).toEqual([
"Launchd stdout (if installed): /tmp/openclaw-state/logs/gateway.log",
"Launchd stderr (if installed): /tmp/openclaw-state/logs/gateway.err.log",
]);
});
it("renders systemd and windows hints by platform", () => {
expect(
buildPlatformRuntimeLogHints({
platform: "linux",
systemdServiceName: "openclaw-gateway",
windowsTaskName: "OpenClaw Gateway",
}),
).toEqual(["Logs: journalctl --user -u openclaw-gateway.service -n 200 --no-pager"]);
expect(
buildPlatformRuntimeLogHints({
platform: "win32",
systemdServiceName: "openclaw-gateway",
windowsTaskName: "OpenClaw Gateway",
}),
).toEqual(['Logs: schtasks /Query /TN "OpenClaw Gateway" /V /FO LIST']);
});
});
describe("buildPlatformServiceStartHints", () => {
it("builds platform-specific service start hints", () => {
expect(
buildPlatformServiceStartHints({
platform: "darwin",
installCommand: "openclaw gateway install",
startCommand: "openclaw gateway",
launchAgentPlistPath: "~/Library/LaunchAgents/com.openclaw.gateway.plist",
systemdServiceName: "openclaw-gateway",
windowsTaskName: "OpenClaw Gateway",
}),
).toEqual([
"openclaw gateway install",
"openclaw gateway",
"launchctl bootstrap gui/$UID ~/Library/LaunchAgents/com.openclaw.gateway.plist",
]);
expect(
buildPlatformServiceStartHints({
platform: "linux",
installCommand: "openclaw gateway install",
startCommand: "openclaw gateway",
launchAgentPlistPath: "~/Library/LaunchAgents/com.openclaw.gateway.plist",
systemdServiceName: "openclaw-gateway",
windowsTaskName: "OpenClaw Gateway",
}),
).toEqual([
"openclaw gateway install",
"openclaw gateway",
"systemctl --user start openclaw-gateway.service",
]);
});
});

View File

@@ -0,0 +1,47 @@
import { resolveGatewayLogPaths } from "./launchd.js";
export function buildPlatformRuntimeLogHints(params: {
platform?: NodeJS.Platform;
env?: NodeJS.ProcessEnv;
systemdServiceName: string;
windowsTaskName: string;
}): string[] {
const platform = params.platform ?? process.platform;
const env = params.env ?? process.env;
if (platform === "darwin") {
const logs = resolveGatewayLogPaths(env);
return [
`Launchd stdout (if installed): ${logs.stdoutPath}`,
`Launchd stderr (if installed): ${logs.stderrPath}`,
];
}
if (platform === "linux") {
return [`Logs: journalctl --user -u ${params.systemdServiceName}.service -n 200 --no-pager`];
}
if (platform === "win32") {
return [`Logs: schtasks /Query /TN "${params.windowsTaskName}" /V /FO LIST`];
}
return [];
}
export function buildPlatformServiceStartHints(params: {
platform?: NodeJS.Platform;
installCommand: string;
startCommand: string;
launchAgentPlistPath: string;
systemdServiceName: string;
windowsTaskName: string;
}): string[] {
const platform = params.platform ?? process.platform;
const base = [params.installCommand, params.startCommand];
switch (platform) {
case "darwin":
return [...base, `launchctl bootstrap gui/$UID ${params.launchAgentPlistPath}`];
case "linux":
return [...base, `systemctl --user start ${params.systemdServiceName}.service`];
case "win32":
return [...base, `schtasks /Run /TN "${params.windowsTaskName}"`];
default:
return base;
}
}