forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.completion.ts
More file actions
120 lines (108 loc) · 3.92 KB
/
Copy pathsetup.completion.ts
File metadata and controls
120 lines (108 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os from "node:os";
import path from "node:path";
import { resolveCliName } from "../cli/cli-name.js";
import {
formatCompletionReloadCommand,
installCompletion,
resolveCompletionProfilePath,
} from "../cli/completion-runtime.js";
import type { ShellCompletionStatus } from "../commands/doctor-completion.js";
import {
checkShellCompletionStatus,
ensureCompletionCacheExists,
} from "../commands/doctor-completion.js";
import { pathExists } from "../utils.js";
import { t } from "./i18n/index.js";
import type { WizardPrompter } from "./prompts.js";
import type { WizardFlow } from "./setup.types.js";
type CompletionDeps = {
resolveCliName: () => string;
checkShellCompletionStatus: (binName: string) => Promise<ShellCompletionStatus>;
ensureCompletionCacheExists: (binName: string) => Promise<boolean>;
installCompletion: (shell: string, yes: boolean, binName?: string) => Promise<void>;
};
async function resolveProfileHint(shell: ShellCompletionStatus["shell"]): Promise<string> {
const home = process.env.HOME || os.homedir();
if (shell === "zsh") {
return "~/.zshrc";
}
if (shell === "bash") {
const bashrc = path.join(home, ".bashrc");
return (await pathExists(bashrc)) ? "~/.bashrc" : "~/.bash_profile";
}
if (shell === "fish") {
return "~/.config/fish/config.fish";
}
return resolveCompletionProfilePath("powershell");
}
function formatReloadHint(shell: ShellCompletionStatus["shell"], profileHint: string): string {
if (shell === "powershell") {
return t("wizard.completion.reloadPowerShell", {
command: formatCompletionReloadCommand("powershell", profileHint),
});
}
return t("wizard.completion.reloadShell", { profile: profileHint });
}
export async function setupWizardShellCompletion(params: {
flow: WizardFlow;
prompter: Pick<WizardPrompter, "confirm" | "note">;
deps?: Partial<CompletionDeps>;
}): Promise<void> {
const deps: CompletionDeps = {
resolveCliName,
checkShellCompletionStatus,
ensureCompletionCacheExists,
installCompletion,
...params.deps,
};
const cliName = deps.resolveCliName();
const completionStatus = await deps.checkShellCompletionStatus(cliName);
if (completionStatus.usesSlowPattern) {
// Case 1: Profile uses slow dynamic pattern - silently upgrade to cached version
const cacheGenerated = await deps.ensureCompletionCacheExists(cliName);
if (cacheGenerated) {
await deps.installCompletion(completionStatus.shell, true, cliName);
}
return;
}
if (completionStatus.profileInstalled && !completionStatus.cacheExists) {
// Case 2: Profile has completion but no cache - auto-fix silently
await deps.ensureCompletionCacheExists(cliName);
return;
}
if (!completionStatus.profileInstalled) {
// Case 3: No completion at all
const shouldInstall =
params.flow === "quickstart"
? true
: await params.prompter.confirm({
message: t("wizard.completion.enable", {
shell: completionStatus.shell,
cli: cliName,
}),
initialValue: true,
});
if (!shouldInstall) {
return;
}
// Generate cache first (required for fast shell startup)
const cacheGenerated = await deps.ensureCompletionCacheExists(cliName);
if (!cacheGenerated) {
await params.prompter.note(
t("wizard.completion.cacheFailed", { command: `${cliName} completion --install` }),
t("wizard.completion.title"),
);
return;
}
// Install to shell profile
await deps.installCompletion(completionStatus.shell, true, cliName);
const profileHint = await resolveProfileHint(completionStatus.shell);
await params.prompter.note(
t("wizard.completion.installed", {
reloadHint: formatReloadHint(completionStatus.shell, profileHint),
}),
t("wizard.completion.title"),
);
}
// Case 4: Both profile and cache exist (using cached version) - all good, nothing to do
}