-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathshell-utils.ts
More file actions
258 lines (222 loc) · 7.69 KB
/
shell-utils.ts
File metadata and controls
258 lines (222 loc) · 7.69 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { execFileSync } from "child_process";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import * as pathWin32 from "path/win32";
const WINDOWS_GIT_LOCATIONS = ["C:\\Program Files\\Git\\cmd\\git.exe", "C:\\Program Files (x86)\\Git\\cmd\\git.exe"];
const WINDOWS_BASH_LOCATIONS = ["C:\\Program Files\\Git\\bin\\bash.exe", "C:\\Program Files (x86)\\Git\\bin\\bash.exe"];
const NUL_REDIRECT_REGEX = /(\d?&?>+\s*)[Nn][Uu][Ll](?=\s|$|[|&;)\n])/g;
let cachedGitBashPath: string | null = null;
export type ShellKind = "bash" | "zsh" | "unknown";
type WindowsGitBashLookup = {
findExecutableCandidates: (executable: string) => string[];
findGitExecPath: () => string | null;
existsSync: (candidate: string) => boolean;
};
export function setShellIfWindows(): void {
if (process.platform !== "win32") {
return;
}
process.env.SHELL = findGitBashPath();
}
export function findGitBashPath(): string {
if (cachedGitBashPath) {
return cachedGitBashPath;
}
const bashPath = resolveWindowsGitBashPath({
findExecutableCandidates: findAllWindowsExecutableCandidates,
findGitExecPath,
existsSync: fs.existsSync,
});
if (bashPath) {
cachedGitBashPath = bashPath;
return bashPath;
}
throw new Error(
"Deep Code on Windows requires Git Bash. Install Git for Windows, or ensure Git's bash.exe is available in PATH."
);
}
export function resolveWindowsGitBashPath(lookup: WindowsGitBashLookup): string | null {
return firstExistingWindowsPath(
[
...lookup.findExecutableCandidates("bash"),
...WINDOWS_BASH_LOCATIONS,
...gitExecPathToBashCandidates(lookup.findGitExecPath()),
...lookup.findExecutableCandidates("git").flatMap(gitExecutableToBashCandidates),
],
lookup.existsSync
);
}
export function resolveShellPath(): string {
if (process.platform === "win32") {
return findGitBashPath();
}
const envShell = process.env.SHELL;
if (envShell && getShellKind(envShell) !== "unknown") {
return envShell;
}
return "/bin/bash";
}
export function getShellKind(shellPath: string): ShellKind {
const executable = shellPath.replace(/\\/g, "/").split("/").pop()?.toLowerCase() ?? "";
if (executable === "bash" || executable === "bash.exe") {
return "bash";
}
if (executable === "zsh" || executable === "zsh.exe") {
return "zsh";
}
return "unknown";
}
export function buildShellInitCommand(shellPath: string): string | null {
switch (getShellKind(shellPath)) {
case "zsh":
return ['ZSHRC="${ZDOTDIR:-$HOME}/.zshrc"', 'if [ -f "$ZSHRC" ]; then . "$ZSHRC"; fi'].join("; ");
case "bash":
return ['BASHRC="${BASH_ENV:-$HOME/.bashrc}"', 'if [ -f "$BASHRC" ]; then . "$BASHRC"; fi'].join("; ");
default:
return null;
}
}
export function buildDisableExtglobCommand(shellPath: string): string | null {
switch (getShellKind(shellPath)) {
case "bash":
return "shopt -u extglob 2>/dev/null || true";
case "zsh":
return "setopt NO_EXTENDED_GLOB 2>/dev/null || true";
default:
return null;
}
}
export function rewriteWindowsNullRedirect(command: string): string {
return command.replace(NUL_REDIRECT_REGEX, "$1/dev/null");
}
export function windowsPathToPosixPath(windowsPath: string): string {
if (windowsPath.startsWith("\\\\")) {
return windowsPath.replace(/\\/g, "/");
}
const driveMatch = windowsPath.match(/^([A-Za-z]):[/\\]/);
if (driveMatch) {
const driveLetter = driveMatch[1].toLowerCase();
return `/${driveLetter}${windowsPath.slice(2).replace(/\\/g, "/")}`;
}
return windowsPath.replace(/\\/g, "/");
}
export function posixPathToWindowsPath(posixPath: string): string {
if (posixPath.startsWith("//")) {
return posixPath.replace(/\//g, "\\");
}
const cygdriveMatch = posixPath.match(/^\/cygdrive\/([A-Za-z])(\/|$)/);
if (cygdriveMatch) {
const driveLetter = cygdriveMatch[1].toUpperCase();
const rest = posixPath.slice(`/cygdrive/${cygdriveMatch[1]}`.length);
return `${driveLetter}:${(rest || "\\").replace(/\//g, "\\")}`;
}
const driveMatch = posixPath.match(/^\/([A-Za-z])(\/|$)/);
if (driveMatch) {
const driveLetter = driveMatch[1].toUpperCase();
const rest = posixPath.slice(2);
return `${driveLetter}:${(rest || "\\").replace(/\//g, "\\")}`;
}
return posixPath.replace(/\//g, "\\");
}
export function toNativeCwd(shellCwd: string): string {
if (process.platform !== "win32") {
return shellCwd;
}
return posixPathToWindowsPath(shellCwd);
}
export function buildShellEnv(shellPath: string, extraEnv: Record<string, string> = {}): NodeJS.ProcessEnv {
const env: NodeJS.ProcessEnv = {
...process.env,
...extraEnv,
SHELL: shellPath,
GIT_EDITOR: "true",
};
if (process.platform === "win32") {
const tmpdir = windowsPathToPosixPath(os.tmpdir());
env.TMPDIR = tmpdir;
env.TMPPREFIX = path.posix.join(tmpdir, "zsh");
}
return env;
}
function findAllWindowsExecutableCandidates(executable: string): string[] {
const extraCandidates =
executable === "git" ? WINDOWS_GIT_LOCATIONS : executable === "bash" ? WINDOWS_BASH_LOCATIONS : [];
try {
const output = execFileSync("where.exe", [executable], {
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
windowsHide: true,
});
let whereResults = output
.split(/\r?\n/)
.map((line) => line.trim())
.filter(Boolean);
if (executable === "bash") {
// Skip WSL's deprecated bash.exe launcher (C:\Windows\System32\bash.exe).
// It would start commands inside the Linux distro instead of the Windows host,
// breaking all path translations and tool invocations.
whereResults = whereResults.filter((candidate) => !/system32[\\/]bash\.exe$/i.test(candidate));
}
return filterWindowsExecutableCandidates([...whereResults, ...extraCandidates]);
} catch {
return filterWindowsExecutableCandidates(extraCandidates);
}
}
function findGitExecPath(): string | null {
try {
const output = execFileSync("git", ["--exec-path"], {
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
windowsHide: true,
}).trim();
return output || null;
} catch {
return null;
}
}
function gitExecPathToBashCandidates(execPath: string | null): string[] {
if (!execPath) {
return [];
}
const normalized = execPath.replace(/\//g, "\\");
return [
pathWin32.join(normalized, "..", "..", "..", "bin", "bash.exe"),
pathWin32.join(normalized, "..", "..", "bin", "bash.exe"),
];
}
function gitExecutableToBashCandidates(gitPath: string): string[] {
return [pathWin32.join(gitPath, "..", "..", "bin", "bash.exe"), pathWin32.join(gitPath, "..", "bin", "bash.exe")];
}
function firstExistingWindowsPath(candidates: string[], existsSync: (candidate: string) => boolean): string | null {
const seen = new Set<string>();
for (const candidate of candidates) {
const normalized = pathWin32.resolve(candidate);
const key = normalized.toLowerCase();
if (seen.has(key)) {
continue;
}
seen.add(key);
if (getShellKind(normalized) === "bash" && existsSync(normalized)) {
return normalized;
}
}
return null;
}
function filterWindowsExecutableCandidates(candidates: string[]): string[] {
const cwd = process.cwd().toLowerCase();
const seen = new Set<string>();
const results: string[] = [];
for (const candidate of candidates) {
const normalized = path.resolve(candidate).toLowerCase();
const candidateDir = path.dirname(normalized).toLowerCase();
if (candidateDir === cwd || normalized.startsWith(`${cwd}${path.sep}`)) {
continue;
}
if (!seen.has(normalized) && fs.existsSync(candidate)) {
seen.add(normalized);
results.push(candidate);
}
}
return results;
}