Repository navigation
Expand file tree
/
Copy pathopenInWindows.ts
More file actions
101 lines (85 loc) · 2.83 KB
/
Copy pathopenInWindows.ts
File metadata and controls
101 lines (85 loc) · 2.83 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
import { exec } from 'child_process';
var powershellIsInstalled: boolean | null = null;
var pwshIsInstalled: boolean | null = null;
var windowsTerminalIsInstalled: boolean | null = null;
const checkPowerShell = async (): Promise<boolean> => new Promise<boolean>(r => {
if (typeof powershellIsInstalled === 'boolean') {
r(powershellIsInstalled)
return
}
exec('powershell.exe -Command "exit"', err => {
powershellIsInstalled = err == null;
r(powershellIsInstalled);
})
})
const checkPwsh = async (): Promise<boolean> => new Promise<boolean>(r => {
if (typeof pwshIsInstalled === 'boolean') {
r(pwshIsInstalled)
return
}
exec('pwsh.exe -Command "exit"', err => {
pwshIsInstalled = err == null;
r(pwshIsInstalled);
})
})
const checkWindowsTerminal = async (): Promise<boolean> => new Promise<boolean>(r => {
if (typeof windowsTerminalIsInstalled === 'boolean') {
r(windowsTerminalIsInstalled)
return
}
exec('where wt.exe', err => {
windowsTerminalIsInstalled = err == null;
r(windowsTerminalIsInstalled);
})
})
const openInCmd = (folderPath: string): void => {
exec(`start cmd.exe /k "cd /d "${folderPath}""`);
};
const openInWindowsTerminal = (folderPath: string): void => {
const escapedPath = folderPath.replace(/"/g, '\\"');
exec(`wt.exe -d "${escapedPath}"`);
};
const openInPwsh = async (folderPath: string): Promise<void> => {
const installed = await checkPwsh();
if (installed) {
exec(`start pwsh.exe -NoExit -Command "Set-Location -LiteralPath '${folderPath.replace(/'/g, "''")}';"`);
} else {
openInPowershell(folderPath)
}
};
export const openInPowershell = async (folderPath: string): Promise<void> => {
const installed = await checkPowerShell();
if (installed) {
exec(`start powershell.exe -NoExit -Command "Set-Location -LiteralPath '${folderPath.replace(/'/g, "''")}';"`);
} else {
openInCmd(folderPath)
}
};
export const openInWindows = async (folderPath: string, defaultProfile?: string): Promise<void> => {
// 优先使用 Windows Terminal
const hasWt = await checkWindowsTerminal();
if (hasWt) {
openInWindowsTerminal(folderPath);
return;
}
if (defaultProfile != null) {
const profileLower = defaultProfile.toLowerCase();
if (profileLower.startsWith('command') || profileLower.includes('cmd')) {
openInCmd(folderPath);
return;
}
}
// 尝试 pwsh 7
const hasPwsh = await checkPwsh();
if (hasPwsh) {
openInPwsh(folderPath);
return;
}
// 尝试普通 PowerShell
const hasPowershell = await checkPowerShell();
if (hasPowershell) {
openInPowershell(folderPath);
return;
}
openInCmd(folderPath);
};