-
-
Notifications
You must be signed in to change notification settings - Fork 76.2k
Expand file tree
/
Copy pathshell-utils.ts
More file actions
199 lines (182 loc) · 5.55 KB
/
shell-utils.ts
File metadata and controls
199 lines (182 loc) · 5.55 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import fs from "node:fs";
import path from "node:path";
export function resolvePowerShellPath(): string {
// Prefer PowerShell 7 when available; PS 5.1 lacks "&&" support.
const programFiles = process.env.ProgramFiles || process.env.PROGRAMFILES || "C:\\Program Files";
const pwsh7 = path.join(programFiles, "PowerShell", "7", "pwsh.exe");
if (fs.existsSync(pwsh7)) {
return pwsh7;
}
const programW6432 = process.env.ProgramW6432;
if (programW6432 && programW6432 !== programFiles) {
const pwsh7Alt = path.join(programW6432, "PowerShell", "7", "pwsh.exe");
if (fs.existsSync(pwsh7Alt)) {
return pwsh7Alt;
}
}
const pwshInPath = resolveShellFromPath("pwsh");
if (pwshInPath) {
return pwshInPath;
}
const systemRoot = process.env.SystemRoot || process.env.WINDIR;
if (systemRoot) {
const candidate = path.join(
systemRoot,
"System32",
"WindowsPowerShell",
"v1.0",
"powershell.exe",
);
if (fs.existsSync(candidate)) {
return candidate;
}
}
return "powershell.exe";
}
// Non-interactive placeholder shells that reject "-c"-style invocations.
// macOS LaunchDaemon service users commonly use /usr/bin/false so login sessions
// cannot be opened; honoring SHELL in that case causes every exec to exit 1.
// See https://github.com/openclaw/openclaw/issues/69077.
const NON_INTERACTIVE_SHELLS = new Set(["false", "nologin"]);
function isNonInteractiveShell(shellPath: string): boolean {
if (!shellPath) {
return false;
}
return NON_INTERACTIVE_SHELLS.has(path.basename(shellPath));
}
function getPosixShellArgs(shellPath: string): string[] {
switch (path.basename(shellPath)) {
case "bash":
return ["--noprofile", "--norc", "-c"];
case "zsh":
return ["-f", "-c"];
case "fish":
return ["--no-config", "-c"];
default:
return ["-c"];
}
}
export function getShellConfig(): { shell: string; args: string[] } {
if (process.platform === "win32") {
// Use PowerShell instead of cmd.exe on Windows.
// Problem: Many Windows system utilities (ipconfig, systeminfo, etc.) write
// directly to the console via WriteConsole API, bypassing stdout pipes.
// When Node.js spawns cmd.exe with piped stdio, these utilities produce no output.
// PowerShell properly captures and redirects their output to stdout.
return {
shell: resolvePowerShellPath(),
args: ["-NoProfile", "-NonInteractive", "-Command"],
};
}
const rawEnvShell = process.env.SHELL?.trim();
const envShell = rawEnvShell && !isNonInteractiveShell(rawEnvShell) ? rawEnvShell : undefined;
const shellName = envShell ? path.basename(envShell) : "";
// Fish rejects common bashisms used by tools, so prefer bash when detected.
if (shellName === "fish") {
const bash = resolveShellFromPath("bash");
if (bash) {
return { shell: bash, args: getPosixShellArgs(bash) };
}
const sh = resolveShellFromPath("sh");
if (sh) {
return { shell: sh, args: getPosixShellArgs(sh) };
}
}
if (envShell) {
return { shell: envShell, args: getPosixShellArgs(envShell) };
}
// Placeholder SHELL (or unset): prefer a resolved sh/bash on PATH so we do not
// re-invoke the placeholder and get a spurious exitCode=1.
const shell = resolveShellFromPath("sh") ?? resolveShellFromPath("bash") ?? "sh";
return { shell, args: getPosixShellArgs(shell) };
}
export function resolveShellFromPath(name: string): string | undefined {
const envPath = process.env.PATH ?? "";
if (!envPath) {
return undefined;
}
const entries = envPath.split(path.delimiter).filter(Boolean);
for (const entry of entries) {
const candidate = path.join(entry, name);
try {
fs.accessSync(candidate, fs.constants.X_OK);
return candidate;
} catch {
// ignore missing or non-executable entries
}
}
return undefined;
}
function normalizeShellName(value: string): string {
const trimmed = value.trim();
if (!trimmed) {
return "";
}
return path
.basename(trimmed)
.replace(/\.(exe|cmd|bat)$/i, "")
.replace(/[^a-zA-Z0-9_-]/g, "");
}
export function detectRuntimeShell(): string | undefined {
const overrideShell = process.env.OPENCLAW_SHELL?.trim();
if (overrideShell) {
const name = normalizeShellName(overrideShell);
if (name) {
return name;
}
}
if (process.platform === "win32") {
if (process.env.POWERSHELL_DISTRIBUTION_CHANNEL) {
return "pwsh";
}
return "powershell";
}
const envShell = process.env.SHELL?.trim();
if (envShell && !isNonInteractiveShell(envShell)) {
const name = normalizeShellName(envShell);
if (name) {
return name;
}
}
if (process.env.POWERSHELL_DISTRIBUTION_CHANNEL) {
return "pwsh";
}
if (process.env.BASH_VERSION) {
return "bash";
}
if (process.env.ZSH_VERSION) {
return "zsh";
}
if (process.env.FISH_VERSION) {
return "fish";
}
if (process.env.KSH_VERSION) {
return "ksh";
}
if (process.env.NU_VERSION || process.env.NUSHELL_VERSION) {
return "nu";
}
return undefined;
}
export function sanitizeBinaryOutput(text: string): string {
const scrubbed = text.replace(/[\p{Format}\p{Surrogate}]/gu, "");
if (!scrubbed) {
return scrubbed;
}
const chunks: string[] = [];
for (const char of scrubbed) {
const code = char.codePointAt(0);
if (code == null) {
continue;
}
if (code === 0x09 || code === 0x0a || code === 0x0d) {
chunks.push(char);
continue;
}
if (code < 0x20) {
continue;
}
chunks.push(char);
}
return chunks.join("");
}