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

37
src/daemon/paths.ts Normal file
View File

@@ -0,0 +1,37 @@
import path from "node:path";
import { resolveGatewayProfileSuffix } from "./constants.js";
const windowsAbsolutePath = /^[a-zA-Z]:[\\/]/;
const windowsUncPath = /^\\\\/;
export 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;
}
export function resolveUserPathWithHome(input: string, home?: string): string {
const trimmed = input.trim();
if (!trimmed) return trimmed;
if (trimmed.startsWith("~")) {
if (!home) throw new Error("Missing HOME");
const expanded = trimmed.replace(/^~(?=$|[\\/])/, home);
return path.resolve(expanded);
}
if (windowsAbsolutePath.test(trimmed) || windowsUncPath.test(trimmed)) {
return trimmed;
}
return path.resolve(trimmed);
}
export function resolveGatewayStateDir(env: Record<string, string | undefined>): string {
const override = env.CLAWDBOT_STATE_DIR?.trim();
if (override) {
const home = override.startsWith("~") ? resolveHomeDir(env) : undefined;
return resolveUserPathWithHome(override, home);
}
const home = resolveHomeDir(env);
const suffix = resolveGatewayProfileSuffix(env.CLAWDBOT_PROFILE);
return path.join(home, `.clawdbot${suffix}`);
}