forked from johannesjo/parallel-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-shell.ts
More file actions
43 lines (36 loc) · 1.23 KB
/
Copy pathuser-shell.ts
File metadata and controls
43 lines (36 loc) · 1.23 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
import fs from 'fs';
import * as os from 'os';
interface ResolveUserShellDeps {
userInfo?: () => { shell: string | null | undefined };
env?: NodeJS.ProcessEnv;
platform?: NodeJS.Platform;
canUseShell?: (shell: string) => boolean;
}
function normalizeShell(shell: string | null | undefined): string | null {
const value = shell?.trim();
return value ? value : null;
}
function isExecutablePosixShell(shell: string): boolean {
try {
fs.accessSync(shell, fs.constants.X_OK);
return true;
} catch {
return false;
}
}
export function resolveUserShell(deps: ResolveUserShellDeps = {}): string {
const env = deps.env ?? process.env;
const platform = deps.platform ?? process.platform;
const userInfo = deps.userInfo ?? os.userInfo;
const canUseShell =
deps.canUseShell ?? ((shell: string) => platform === 'win32' || isExecutablePosixShell(shell));
try {
const osShell = normalizeShell(userInfo().shell);
if (osShell && canUseShell(osShell)) return osShell;
} catch {
// Fall back to inherited environment if the OS lookup is unavailable.
}
const envShell = normalizeShell(env.SHELL);
if (envShell && canUseShell(envShell)) return envShell;
return platform === 'win32' ? 'cmd.exe' : '/bin/sh';
}