-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathshellUtils.ts
109 lines (98 loc) · 3.39 KB
/
shellUtils.ts
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { Terminal, env } from "vscode";
import { executeCommand } from "./cpUtils";
export enum WindowsShellType {
CMD = "Command Prompt",
POWER_SHELL = "PowerShell",
GIT_BASH = "Git Bash",
WSL = "WSL Bash",
OTHERS = "Others"
}
export async function formattedPathForTerminal(filepath: string): Promise<string> {
if (process.platform === "win32") {
switch (currentWindowsShell()) {
case WindowsShellType.WSL:
return await toWslPath(filepath);
default:
return filepath;
}
} else {
return filepath;
}
}
export function getCommand(cmd: string): string {
if (process.platform === "win32") {
switch (currentWindowsShell()) {
case WindowsShellType.POWER_SHELL:
return `cmd /c ${cmd}`; // PowerShell
default:
return cmd; // others, try using common one.
}
} else {
return cmd;
}
}
export async function getCDCommand(cwd: string): Promise<string> {
if (process.platform === "win32") {
switch (currentWindowsShell()) {
case WindowsShellType.GIT_BASH:
return `cd "${cwd.replace(/\\+$/, "")}"`; // Git Bash: remove trailing '\'
case WindowsShellType.POWER_SHELL:
// Escape '[' and ']' in PowerShell
// See: https://github.com/microsoft/vscode-maven/issues/324
const escaped: string = cwd.replace(/([\[\]])/g, "``$1");
return `cd "${escaped}"`; // PowerShell
case WindowsShellType.CMD:
return `cd /d "${cwd}"`; // CMD
case WindowsShellType.WSL:
return `cd "${await toWslPath(cwd)}"`; // WSL
default:
return `cd "${cwd}"`; // Unknown, try using common one.
}
} else {
return `cd "${cwd}"`;
}
}
export function currentWindowsShell(): WindowsShellType {
const currentWindowsShellPath: string = env.shell;
if (currentWindowsShellPath.endsWith("cmd.exe")) {
return WindowsShellType.CMD;
} else if (currentWindowsShellPath.endsWith("powershell.exe")) {
return WindowsShellType.POWER_SHELL;
} else if (currentWindowsShellPath.endsWith("bash.exe") || currentWindowsShellPath.endsWith("wsl.exe")) {
if (currentWindowsShellPath.includes("Git")) {
return WindowsShellType.GIT_BASH;
}
return WindowsShellType.WSL;
} else {
return WindowsShellType.OTHERS;
}
}
export function toDefaultWslPath(p: string): string {
const arr: string[] = p.split(":\\");
if (arr.length === 2) {
const drive: string = arr[0].toLowerCase();
const dir: string = arr[1].replace(/\\/g, "/");
return `/mnt/${drive}/${dir}`;
} else {
return p.replace(/\\/g, "/");
}
}
export async function toWslPath(path: string): Promise<string> {
try {
return (await executeCommand("wsl", ["wslpath", "-u", `"${path.replace(/\\/g, "/")}"`])).trim();
} catch (error) {
return toDefaultWslPath(path);
}
}
export async function toWinPath(path: string): Promise<string> {
return (await executeCommand("wsl", ["wslpath", "-w", `"${path}"`])).trim();
}
export function setupEnvForWSL(terminal: Terminal, env: { [envKey: string]: string }): void {
if (terminal) {
Object.keys(env).forEach(key => {
terminal.sendText(`export ${key}="${env[key]}"`, true);
});
}
}