mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-06 22:42:52 +00:00
Fixes #86007. Release note: Windows gateway install/update now ignores a persisted OPENCLAW_WRAPPER when it points back at the generated gateway.cmd task script, preventing recursive gateway startup while keeping valid wrapper installs intact. Credit: thanks @luoyanglang for the fix and proof.
58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
import path from "node:path";
|
|
import { normalizeOptionalString } from "../shared/string-coerce.js";
|
|
import { resolveGatewayProfileSuffix } from "./constants.js";
|
|
|
|
const windowsAbsolutePath = /^[a-zA-Z]:[\\/]/;
|
|
const windowsUncPath = /^\\\\/;
|
|
|
|
export function resolveHomeDir(env: Record<string, string | undefined>): string {
|
|
const home = normalizeOptionalString(env.HOME) || normalizeOptionalString(env.USERPROFILE);
|
|
if (!home) {
|
|
throw new Error("Missing HOME");
|
|
}
|
|
return home;
|
|
}
|
|
|
|
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 = normalizeOptionalString(env.OPENCLAW_STATE_DIR);
|
|
if (override) {
|
|
const home = override.startsWith("~") ? resolveHomeDir(env) : undefined;
|
|
return resolveUserPathWithHome(override, home);
|
|
}
|
|
const home = resolveHomeDir(env);
|
|
const suffix = resolveGatewayProfileSuffix(env.OPENCLAW_PROFILE);
|
|
return path.join(home, `.openclaw${suffix}`);
|
|
}
|
|
|
|
export function resolveGatewayTaskScriptPath(env: Record<string, string | undefined>): string {
|
|
const override = normalizeOptionalString(env.OPENCLAW_TASK_SCRIPT);
|
|
if (override) {
|
|
return override;
|
|
}
|
|
const scriptName = normalizeOptionalString(env.OPENCLAW_TASK_SCRIPT_NAME) || "gateway.cmd";
|
|
if (/[/\\]|\.\./.test(scriptName)) {
|
|
throw new Error(
|
|
`OPENCLAW_TASK_SCRIPT_NAME must be a file name only, not a path: ${scriptName}`,
|
|
);
|
|
}
|
|
return path.join(resolveGatewayStateDir(env), scriptName);
|
|
}
|