-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathshellDetector.ts
More file actions
173 lines (153 loc) · 5.39 KB
/
shellDetector.ts
File metadata and controls
173 lines (153 loc) · 5.39 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
import * as os from 'os';
import { Terminal } from 'vscode';
import { isWindows } from '../../managers/common/utils';
import { vscodeShell } from '../../common/vscodeEnv.apis';
import { getConfiguration } from '../../common/workspace.apis';
import { traceLog } from '../../common/logging';
/*
When identifying the shell use the following algorithm:
* 1. Identify shell based on the name of the terminal (if there is one already opened and used).
* 2. Identify shell based on the api provided by VSC.
* 2. Identify shell based on the settings in VSC.
* 3. Identify shell based on users environment variables.
* 4. Use default shells (bash for mac and linux, cmd for windows).
*/
// Types of shells can be found here:
// 1. https://wiki.ubuntu.com/ChangingShells
const IS_GITBASH = /(gitbash$|git.bin.bash$|git-bash$)/i;
const IS_BASH = /(bash$)/i;
const IS_WSL = /(wsl$)/i;
const IS_ZSH = /(zsh$)/i;
const IS_KSH = /(ksh$)/i;
const IS_COMMAND = /(cmd$)/i;
const IS_POWERSHELL = /(powershell$|pwsh$)/i;
const IS_FISH = /(fish$)/i;
const IS_CSHELL = /(csh$)/i;
const IS_TCSHELL = /(tcsh$)/i;
const IS_NUSHELL = /(nu$)/i;
const IS_XONSH = /(xonsh$)/i;
const detectableShells = new Map<string, RegExp>([
['pwsh', IS_POWERSHELL],
['gitbash', IS_GITBASH],
['bash', IS_BASH],
['wsl', IS_WSL],
['zsh', IS_ZSH],
['ksh', IS_KSH],
['cmd', IS_COMMAND],
['fish', IS_FISH],
['tcsh', IS_TCSHELL],
['csh', IS_CSHELL],
['nu', IS_NUSHELL],
['xonsh', IS_XONSH],
]);
function identifyShellFromShellPath(shellPath: string): string {
// Remove .exe extension so shells can be more consistently detected
// on Windows (including Cygwin).
const basePath = shellPath.replace(/\.exe$/i, '');
const shell = Array.from(detectableShells.keys()).reduce((matchedShell, shellToDetect) => {
if (matchedShell === 'unknown') {
const pat = detectableShells.get(shellToDetect);
if (pat && pat.test(basePath)) {
return shellToDetect;
}
}
return matchedShell;
}, 'unknown');
return shell;
}
function identifyShellFromTerminalName(terminal: Terminal): string {
if (terminal.name === 'sh') {
// Specifically checking this because other shells have `sh` at the end of their name
// We can match and return bash for this case
return 'bash';
}
return identifyShellFromShellPath(terminal.name);
}
function identifyPlatformDefaultShell(): string {
if (isWindows()) {
return identifyShellFromShellPath(getTerminalDefaultShellWindows());
}
const shellPath = process.env.SHELL && process.env.SHELL !== '/bin/false' ? process.env.SHELL : '/bin/bash';
return identifyShellFromShellPath(shellPath);
}
function getTerminalDefaultShellWindows(): string {
const isAtLeastWindows10 = parseFloat(os.release()) >= 10;
const syspath = process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432') ? 'Sysnative' : 'System32';
const windir = process.env.windir ?? 'C:\\Windows';
const powerShellPath = `${windir}\\${syspath}\\WindowsPowerShell\\v1.0\\powershell.exe`;
return isAtLeastWindows10 ? powerShellPath : process.env.comspec || 'cmd.exe';
}
function identifyShellFromVSC(terminal: Terminal): string {
const shellPath =
terminal?.creationOptions && 'shellPath' in terminal.creationOptions && terminal.creationOptions.shellPath
? terminal.creationOptions.shellPath
: vscodeShell();
return shellPath ? identifyShellFromShellPath(shellPath) : 'unknown';
}
function identifyShellFromSettings(): string {
const shellConfig = getConfiguration('terminal.integrated.shell');
let shellPath: string | undefined;
switch (process.platform) {
case 'win32': {
shellPath = shellConfig.get<string>('windows');
break;
}
case 'darwin': {
shellPath = shellConfig.get<string>('osx');
break;
}
case 'freebsd':
case 'openbsd':
case 'linux': {
shellPath = shellConfig.get<string>('linux');
break;
}
default: {
shellPath = undefined;
}
}
return shellPath ? identifyShellFromShellPath(shellPath) : 'unknown';
}
function fromShellTypeApi(terminal: Terminal): string {
try {
const known = [
'bash',
'cmd',
'csh',
'fish',
'gitbash',
'julia',
'ksh',
'node',
'nu',
'pwsh',
'python',
'sh',
'wsl',
'zsh',
];
if (terminal.state.shell && known.includes(terminal.state.shell.toLowerCase())) {
return terminal.state.shell.toLowerCase();
}
} catch {
// If the API is not available, return unknown
}
return 'unknown';
}
export function identifyTerminalShell(terminal: Terminal): string {
let shellType = fromShellTypeApi(terminal);
traceLog('IdentifyTerminalShell: Shell type from API:', shellType);
if (shellType === 'unknown') {
shellType = identifyShellFromVSC(terminal);
}
if (shellType === 'unknown') {
shellType = identifyShellFromTerminalName(terminal);
}
if (shellType === 'unknown') {
shellType = identifyShellFromSettings();
}
if (shellType === 'unknown') {
shellType = identifyPlatformDefaultShell();
}
return shellType;
}