























@@ -16,9 +16,20 @@ export function isCompletionShell(value: string): value is CompletionShell {
1616return COMPLETION_SHELLS.includes(value as CompletionShell);
1717}
181819+function resolveShellBasename(
20+shellPath: string,
21+platform: NodeJS.Platform = process.platform,
22+): string {
23+const platformBasename =
24+platform === "win32" ? path.win32.basename(shellPath) : path.basename(shellPath);
25+const winBasename = path.win32.basename(shellPath);
26+const basename = winBasename.length < platformBasename.length ? winBasename : platformBasename;
27+return normalizeLowercaseStringOrEmpty(basename.replace(/\.(?:exe|cmd|bat)$/i, ""));
28+}
29+1930export function resolveShellFromEnv(env: NodeJS.ProcessEnv = process.env): CompletionShell {
2031const shellPath = normalizeOptionalString(env.SHELL) ?? "";
21-const shellName = shellPath ? normalizeLowercaseStringOrEmpty(path.basename(shellPath)) : "";
32+const shellName = shellPath ? resolveShellBasename(shellPath) : "";
2233if (shellName === "zsh") {
2334return "zsh";
2435}
@@ -162,10 +173,13 @@ export function resolveCompletionProfilePath(
162173return path.join(home, ".config", "fish", "config.fish");
163174}
164175if (platform === "win32") {
176+const shellPath = normalizeOptionalString(env.SHELL) ?? "";
177+const shellName = shellPath ? resolveShellBasename(shellPath, platform) : "";
178+const profileDirectory = shellName === "powershell" ? "WindowsPowerShell" : "PowerShell";
165179return path.win32.join(
166180env.USERPROFILE || home,
167181"Documents",
168-"PowerShell",
182+profileDirectory,
169183"Microsoft.PowerShell_profile.ps1",
170184);
171185}
@@ -217,9 +231,6 @@ export async function usesSlowDynamicCompletion(
217231}
218232219233export async function installCompletion(shell: string, yes: boolean, binName = "openclaw") {
220-let profilePath = "";
221-let sourceLine = "";
222-223234const isShellSupported = isCompletionShell(shell);
224235if (!isShellSupported) {
225236console.error(`Automated installation not supported for ${shell} yet.`);
@@ -235,27 +246,31 @@ export async function installCompletion(shell: string, yes: boolean, binName = "
235246return;
236247}
237248238-if (shell === "zsh") {
239-profilePath = resolveCompletionProfilePath("zsh");
240-sourceLine = formatCompletionSourceLine("zsh", binName, cachePath);
241-} else if (shell === "bash") {
242-profilePath = resolveCompletionProfilePath("bash");
243-try {
244-await fs.access(profilePath);
245-} catch {
246-const home = process.env.HOME || os.homedir();
247-profilePath = path.join(home, ".bash_profile");
248-}
249-sourceLine = formatCompletionSourceLine("bash", binName, cachePath);
250-} else if (shell === "fish") {
251-profilePath = resolveCompletionProfilePath("fish");
252-sourceLine = formatCompletionSourceLine("fish", binName, cachePath);
253-} else if (shell === "powershell") {
254-profilePath = resolveCompletionProfilePath("powershell");
255-sourceLine = formatCompletionSourceLine("powershell", binName, cachePath);
256-} else {
257-console.error(`Automated installation not supported for ${shell} yet.`);
258-return;
249+let profilePath: string;
250+let sourceLine: string;
251+switch (shell) {
252+case "zsh":
253+profilePath = resolveCompletionProfilePath("zsh");
254+sourceLine = formatCompletionSourceLine("zsh", binName, cachePath);
255+break;
256+case "bash":
257+profilePath = resolveCompletionProfilePath("bash");
258+try {
259+await fs.access(profilePath);
260+} catch {
261+const home = process.env.HOME || os.homedir();
262+profilePath = path.join(home, ".bash_profile");
263+}
264+sourceLine = formatCompletionSourceLine("bash", binName, cachePath);
265+break;
266+case "fish":
267+profilePath = resolveCompletionProfilePath("fish");
268+sourceLine = formatCompletionSourceLine("fish", binName, cachePath);
269+break;
270+case "powershell":
271+profilePath = resolveCompletionProfilePath("powershell");
272+sourceLine = formatCompletionSourceLine("powershell", binName, cachePath);
273+break;
259274}
260275261276try {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。