refactor: centralize daemon path resolution

This commit is contained in:
Peter Steinberger
2026-01-15 23:09:08 +00:00
parent 4a99b9b651
commit db9be87d94
9 changed files with 130 additions and 56 deletions

View File

@@ -16,6 +16,7 @@ import {
} from "./launchd-plist.js";
import { parseKeyValueOutput } from "./runtime-parse.js";
import type { GatewayServiceRuntime } from "./service-runtime.js";
import { resolveGatewayStateDir, resolveHomeDir } from "./paths.js";
const execFileAsync = promisify(execFile);
@@ -29,11 +30,6 @@ function resolveLaunchAgentLabel(args?: { env?: Record<string, string | undefine
if (envLabel) return envLabel;
return resolveGatewayLaunchAgentLabel(args?.env?.CLAWDBOT_PROFILE);
}
function resolveHomeDir(env: Record<string, string | undefined>): string {
const home = env.HOME?.trim() || env.USERPROFILE?.trim();
if (!home) throw new Error("Missing HOME");
return home;
}
function resolveLaunchAgentPlistPathForLabel(
env: Record<string, string | undefined>,
@@ -53,12 +49,7 @@ export function resolveGatewayLogPaths(env: Record<string, string | undefined>):
stdoutPath: string;
stderrPath: string;
} {
const home = resolveHomeDir(env);
const stateOverride = env.CLAWDBOT_STATE_DIR?.trim();
const profile = env.CLAWDBOT_PROFILE?.trim();
const suffix = profile && profile.toLowerCase() !== "default" ? `-${profile}` : "";
const defaultStateDir = path.join(home, `.clawdbot${suffix}`);
const stateDir = stateOverride ? resolveUserPathWithHome(stateOverride, home) : defaultStateDir;
const stateDir = resolveGatewayStateDir(env);
const logDir = path.join(stateDir, "logs");
return {
logDir,
@@ -67,16 +58,6 @@ export function resolveGatewayLogPaths(env: Record<string, string | undefined>):
};
}
function resolveUserPathWithHome(input: string, home: string): string {
const trimmed = input.trim();
if (!trimmed) return trimmed;
if (trimmed.startsWith("~")) {
const expanded = trimmed.replace(/^~(?=$|[\\/])/, home);
return path.resolve(expanded);
}
return path.resolve(trimmed);
}
export async function readLaunchAgentProgramArguments(
env: Record<string, string | undefined>,
): Promise<{