forked from doctly/switchboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell-profiles.js
More file actions
221 lines (197 loc) · 8.46 KB
/
Copy pathshell-profiles.js
File metadata and controls
221 lines (197 loc) · 8.46 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
const path = require('path');
const fs = require('fs');
// --- Cross-platform shell resolution ---
const isWindows = process.platform === 'win32';
// Discover available shell profiles on this system.
// Returns an array of { id, name, path, args? } objects.
function discoverShellProfiles() {
const profiles = [];
if (isWindows) {
const { execSync } = require('child_process');
// CMD
const comspec = process.env.COMSPEC || 'C:\\WINDOWS\\system32\\cmd.exe';
if (fs.existsSync(comspec)) {
profiles.push({ id: 'cmd', name: 'Command Prompt', path: comspec });
}
// PowerShell 7+ (pwsh)
const pwshCandidates = [
path.join(process.env.ProgramFiles || 'C:\\Program Files', 'PowerShell', '7', 'pwsh.exe'),
path.join(process.env.ProgramFiles || 'C:\\Program Files', 'PowerShell', '7-preview', 'pwsh.exe'),
];
for (const p of pwshCandidates) {
if (fs.existsSync(p)) {
profiles.push({ id: 'pwsh', name: 'PowerShell 7', path: p });
break;
}
}
// Windows PowerShell 5.x
const ps5 = path.join(process.env.SystemRoot || 'C:\\WINDOWS', 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe');
if (fs.existsSync(ps5)) {
profiles.push({ id: 'powershell', name: 'Windows PowerShell', path: ps5 });
}
// Git Bash
const gitBashCandidates = [
path.join(process.env.ProgramFiles || 'C:\\Program Files', 'Git', 'bin', 'bash.exe'),
path.join(process.env['ProgramFiles(x86)'] || 'C:\\Program Files (x86)', 'Git', 'bin', 'bash.exe'),
path.join(process.env.LOCALAPPDATA || '', 'Programs', 'Git', 'bin', 'bash.exe'),
];
for (const p of gitBashCandidates) {
if (p && fs.existsSync(p)) {
profiles.push({ id: 'git-bash', name: 'Git Bash', path: p });
break;
}
}
// MSYS2
if (fs.existsSync('C:\\msys64\\usr\\bin\\bash.exe')) {
profiles.push({ id: 'msys2', name: 'MSYS2', path: 'C:\\msys64\\usr\\bin\\bash.exe' });
}
// WSL distributions
try {
const raw = execSync('wsl.exe --list --quiet', { timeout: 5000, encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] });
const distros = raw.replace(/\0/g, '').split(/\r?\n/).map(s => s.trim()).filter(Boolean);
for (const distro of distros) {
profiles.push({ id: 'wsl:' + distro, name: 'WSL — ' + distro, path: 'wsl.exe', args: ['-d', distro] });
}
} catch {}
} else {
// macOS / Linux: read /etc/shells for the canonical list
const seen = new Set();
const shellNames = {
'zsh': 'Zsh', 'bash': 'Bash', 'sh': 'POSIX Shell',
'fish': 'Fish', 'nu': 'Nushell', 'pwsh': 'PowerShell',
'dash': 'Dash', 'ksh': 'Korn Shell', 'tcsh': 'tcsh', 'csh': 'C Shell',
};
try {
const lines = fs.readFileSync('/etc/shells', 'utf8').split('\n')
.map(l => l.trim())
.filter(l => l && !l.startsWith('#'));
for (const shellPath of lines) {
if (!fs.existsSync(shellPath)) continue;
const base = path.basename(shellPath);
// Deduplicate by basename (e.g. /bin/bash and /usr/bin/bash)
if (seen.has(base)) continue;
seen.add(base);
const name = shellNames[base] || base;
profiles.push({ id: base, name, path: shellPath });
}
} catch {
// Fallback if /etc/shells is unreadable
for (const [id, name, p] of [
['zsh', 'Zsh', '/bin/zsh'],
['bash', 'Bash', '/bin/bash'],
['sh', 'POSIX Shell', '/bin/sh'],
]) {
if (fs.existsSync(p)) {
profiles.push({ id, name, path: p });
}
}
}
}
return profiles;
}
// Cache profiles (discovered once on startup, refreshed via IPC if needed)
let _shellProfiles = null;
function getShellProfiles() {
if (!_shellProfiles) _shellProfiles = discoverShellProfiles();
return _shellProfiles;
}
function resolveShell(profileId) {
// If a profile is selected, use it
if (profileId && profileId !== 'auto') {
const profiles = getShellProfiles();
const profile = profiles.find(p => p.id === profileId);
if (profile && (profile.path === 'wsl.exe' || isSshProfile(profile.path) || fs.existsSync(profile.path))) {
return profile;
}
}
// Auto: original detection logic
// 1. Respect explicit SHELL env (set by Git Bash, MSYS2, WSL, etc.)
if (process.env.SHELL && fs.existsSync(process.env.SHELL)) {
return { id: 'auto', name: 'Auto', path: process.env.SHELL };
}
if (isWindows) {
// 2. Look for Git Bash in common locations
const candidates = [
path.join(process.env.ProgramFiles || 'C:\\Program Files', 'Git', 'bin', 'bash.exe'),
path.join(process.env['ProgramFiles(x86)'] || 'C:\\Program Files (x86)', 'Git', 'bin', 'bash.exe'),
path.join(process.env.LOCALAPPDATA || '', 'Programs', 'Git', 'bin', 'bash.exe'),
'C:\\msys64\\usr\\bin\\bash.exe',
];
for (const c of candidates) {
if (c && fs.existsSync(c)) return { id: 'auto', name: 'Auto', path: c };
}
// 3. Fall back to PowerShell / cmd
return { id: 'auto', name: 'Auto', path: process.env.COMSPEC || 'powershell.exe' };
}
// Unix fallback chain
for (const s of ['/bin/zsh', '/bin/bash', '/bin/sh']) {
if (fs.existsSync(s)) return { id: 'auto', name: 'Auto', path: s };
}
return { id: 'auto', name: 'Auto', path: '/bin/sh' };
}
// Convert a Windows path to a WSL /mnt/ path
function windowsToWslPath(winPath) {
if (!winPath) return winPath;
// C:\Users\foo → /mnt/c/Users/foo
const normalized = winPath.replace(/\\/g, '/');
const match = normalized.match(/^([A-Za-z]):(\/.*)/);
if (match) return '/mnt/' + match[1].toLowerCase() + match[2];
return normalized;
}
function isWslShell(shellPath) {
const base = path.basename(shellPath).toLowerCase();
return base === 'wsl.exe' || base === 'wsl';
}
// An SSH profile wraps the `ssh` command (path: 'ssh'). Detected by basename
// so it works whether the caller passes 'ssh' or an absolute '/usr/bin/ssh'.
function isSshProfile(shellPath) {
const base = path.basename(String(shellPath || '')).toLowerCase().replace(/\.exe$/, '');
return base === 'ssh';
}
// Single-quote a string for a POSIX shell (escaping embedded single quotes).
function sshSingleQuote(s) {
return "'" + String(s).replace(/'/g, "'\\''") + "'";
}
// Returns spawn args appropriate for the resolved shell
function shellArgs(shellPath, cmd, extraArgs) {
const base = path.basename(shellPath).toLowerCase();
const isBashLike = base.includes('bash') || base.includes('zsh') || base === 'sh';
const isFish = base === 'fish';
const isNushell = base === 'nu';
// WSL: pass command via -- to the distribution shell
// cwd is handled separately via --cd in the spawn call
if (isWslShell(shellPath)) {
if (cmd) return [...(extraArgs || []), '--', 'bash', '-l', '-i', '-c', cmd];
return [...(extraArgs || []), '--', 'bash', '-l', '-i'];
}
// SSH: run the command on the remote inside a login+interactive shell so the
// remote PATH is set up (mirrors WSL). ssh concatenates the argv after the
// hostname with spaces and the remote shell re-parses it, so the payload must
// be single-quoted to survive as one argument, while ${SHELL:-bash} is left
// unquoted on purpose so the remote expands it.
//
// It has to be the login shell rather than a hardcoded bash: anything the user
// set up in ~/.zshrc — version managers like fnm/nvm/rbenv, ~/.local/bin — is
// invisible to `bash -l -i` when their login shell is zsh, which is the default
// on every macOS since Catalina. A remote `claude` installed through a version
// manager then fails with "command not found". bash stays the fallback for the
// rare account with no $SHELL. extraArgs carries the ssh options and target
// (e.g. ['-t', 'user@host']).
if (isSshProfile(shellPath)) {
const remote = cmd || 'exec "${SHELL:-bash}" -l';
return [...(extraArgs || []), '${SHELL:-bash}', '-l', '-i', '-c', sshSingleQuote(remote)];
}
if (cmd) {
if (isBashLike) return ['-l', '-i', '-c', cmd];
if (isFish) return ['-l', '-c', cmd];
if (isNushell) return ['-l', '-c', cmd];
if (base.includes('powershell') || base.includes('pwsh')) return ['-NoLogo', '-Command', cmd];
return ['/C', cmd];
}
if (isBashLike) return ['-l', '-i'];
if (isFish) return ['-l', '-i'];
if (isNushell) return ['-l', '-i'];
if (base.includes('powershell') || base.includes('pwsh')) return ['-NoLogo', '-NoExit'];
return [];
}
module.exports = { discoverShellProfiles, getShellProfiles, resolveShell, isWindows, isWslShell, windowsToWslPath, shellArgs, isSshProfile };