Repository navigation
Expand file tree
/
Copy pathopenInLinux.ts
More file actions
78 lines (64 loc) · 2.26 KB
/
Copy pathopenInLinux.ts
File metadata and controls
78 lines (64 loc) · 2.26 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
import { exec } from 'child_process';
var gnomeTerminalIsInstalled: boolean | null = null;
const checkGnomeTerminal = async (): Promise<boolean> => new Promise<boolean>(r => {
if (typeof gnomeTerminalIsInstalled === 'boolean') {
r(gnomeTerminalIsInstalled);
return;
}
exec('which gnome-terminal', err => {
gnomeTerminalIsInstalled = err == null;
r(gnomeTerminalIsInstalled);
});
});
const openInXTerm = (folderPath: string): void => {
exec(`xterm -e "cd '${folderPath.replace(/'/g, "'\\''")}' && bash"`);
};
const tryTerminal = (command: string): Promise<boolean> => {
return new Promise(resolve => {
exec(command, (error) => {
resolve(error == null);
});
});
};
const openInFallbackTerminal = async (folderPath: string): Promise<void> => {
const escapedPath = folderPath.replace(/'/g, "'\\''");
// Try common Linux terminals in order of preference
const terminals = [
`konsole --workdir "${folderPath}"`,
`xfce4-terminal --working-directory="${folderPath}"`,
`mate-terminal --working-directory="${folderPath}"`,
`tilix --working-directory="${folderPath}"`,
`terminator --working-directory="${folderPath}"`,
`x-terminal-emulator --working-directory="${folderPath}"`
];
for (const terminal of terminals) {
const success = await tryTerminal(terminal);
if (success) {
return;
}
}
// Final fallback to xterm
openInXTerm(folderPath);
};
export const openInBash = async (folderPath: string): Promise<void> => {
const installed = await checkGnomeTerminal();
if (installed) {
exec(`gnome-terminal --working-directory="${folderPath}"`);
} else {
await openInFallbackTerminal(folderPath);
}
};
export const openInLinux = (folderPath: string, defaultProfile?: string): void => {
if (defaultProfile == null) {
openInBash(folderPath);
return;
}
// Check for common shell types in the profile name
const profileLower = defaultProfile.toLowerCase();
if (profileLower.includes('bash') || profileLower.includes('sh')) {
openInBash(folderPath);
return;
}
// Default to bash for most cases
openInBash(folderPath);
};