-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathpathUtils.ts
207 lines (182 loc) · 6.03 KB
/
pathUtils.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import execa from 'execa';
import * as path from 'path';
import { FsPromises } from '../ioc-extras';
import { EnvironmentVars } from './environmentVars';
import { existsInjected } from './fsUtils';
import { removeNulls } from './objUtils';
/*
* Lookup the given program on the PATH and return its absolute path on success and undefined otherwise.
*/
export async function findInPath(
fs: FsPromises,
program: string,
env: { [key: string]: string | null | undefined },
): Promise<string | undefined> {
let locator: string;
if (process.platform === 'win32') {
const windir = env['WINDIR'] || 'C:\\Windows';
locator = path.join(windir, 'System32', 'where.exe');
} else {
locator = '/usr/bin/which';
}
try {
if (await existsInjected(fs, locator)) {
const located = await execa(locator, [program], { env: removeNulls(env) });
const lines = located.stdout.split(/\r?\n/);
if (process.platform === 'win32') {
// return the first path that has a executable extension
const executableExtensions = String(env['PATHEXT'] || '.exe')
.toUpperCase()
.split(';');
for (const candidate of lines) {
const ext = path.extname(candidate).toUpperCase();
if (ext && executableExtensions.includes(ext)) {
return candidate;
}
}
} else {
// return the first path
if (lines.length > 0) {
return lines[0];
}
}
return undefined;
} else {
// do not report failure if 'locator' app doesn't exist
}
return program;
} catch (err) {
// fall through
}
// fail
return undefined;
}
/*
* Ensures the program exists, adding its executable as necessary on Windows.
*/
export async function findExecutable(
fs: FsPromises,
program: string | undefined,
env: EnvironmentVars,
): Promise<string | undefined> {
if (!program) {
return undefined;
}
if (process.platform === 'win32' && !path.extname(program)) {
const pathExtension = env.lookup('PATHEXT');
if (pathExtension) {
const executableExtensions = pathExtension.split(';');
for (const extension of executableExtensions) {
const path = program + extension;
if (await existsInjected(fs, path)) {
return path;
}
}
}
}
if (await existsInjected(fs, program)) {
return program;
}
return undefined;
}
/**
* Join path segments properly based on whether they appear to be c:/ -style or / style.
* Note - must check posix first because win32.isAbsolute includes posix.isAbsolute
*/
export function properJoin(...segments: string[]): string {
if (path.posix.isAbsolute(segments[0])) {
return forceForwardSlashes(path.posix.join(...segments));
} else if (path.win32.isAbsolute(segments[0])) {
return path.win32.join(...segments);
} else {
return path.join(...segments);
}
}
/**
* Resolves path segments properly based on whether they appear to be c:/ -style or / style.
*/
export function properResolve(...segments: string[]): string {
if (path.posix.isAbsolute(segments[0])) {
return path.posix.resolve(...segments);
} else if (path.win32.isAbsolute(segments[0])) {
return path.win32.resolve(...segments);
} else {
return path.resolve(...segments);
}
}
/**
* Resolves path segments properly based on whether they appear to be c:/ -style or / style.
*/
export function properRelative(fromPath: string, toPath: string): string {
if (path.posix.isAbsolute(fromPath)) {
return path.posix.relative(fromPath, toPath);
} else if (path.win32.isAbsolute(fromPath)) {
return path.win32.relative(fromPath, toPath);
} else {
return path.relative(fromPath, toPath);
}
}
export function fixDriveLetter(aPath: string, uppercaseDriveLetter = false): string {
if (!aPath) return aPath;
if (aPath.match(/file:\/\/\/[A-Za-z]:/)) {
const prefixLen = 'file:///'.length;
aPath = 'file:///' + aPath[prefixLen].toLowerCase() + aPath.substr(prefixLen + 1);
} else if (isWindowsPath(aPath)) {
// If the path starts with a drive letter, ensure lowercase. VS Code uses a lowercase drive letter
const driveLetter = uppercaseDriveLetter ? aPath[0].toUpperCase() : aPath[0].toLowerCase();
aPath = driveLetter + aPath.substr(1);
}
return aPath;
}
/**
* Ensure lower case drive letter and \ on Windows
*/
export function fixDriveLetterAndSlashes(aPath: string, uppercaseDriveLetter = false): string {
if (!aPath) return aPath;
aPath = fixDriveLetter(aPath, uppercaseDriveLetter);
if (aPath.match(/file:\/\/\/[A-Za-z]:/)) {
const prefixLen = 'file:///'.length;
aPath = aPath.substr(0, prefixLen + 1) + aPath.substr(prefixLen + 1).replace(/\//g, '\\');
} else if (isWindowsPath(aPath)) {
aPath = aPath.replace(/\//g, '\\');
}
return aPath;
}
/**
* Replace any backslashes with forward slashes
* blah\something => blah/something
*/
export function forceForwardSlashes(aUrl: string): string {
return aUrl
.replace(/\\\//g, '/') // Replace \/ (unnecessarily escaped forward slash)
.replace(/\\/g, '/');
}
/**
* Splits the path with the drive letter included with a trailing slash
* such that path.join, readdir, etc. work on it standalone.
*/
export const splitWithDriveLetter = (inputPath: string) => {
const parts = inputPath.split(path.sep);
if (/^[a-z]:$/i.test(parts[0])) {
parts[0] += path.sep;
}
return parts;
};
/**
* Gets whether the child is a subdirectory of its parent.
*/
export const isSubdirectoryOf = (parent: string, child: string) => {
const rel = path.relative(parent, child);
return rel.length && !path.isAbsolute(rel) && !rel.startsWith('..');
};
/**
* Returns whether the path looks like a UNC path.
*/
export const isUncPath = (path: string) => path.startsWith('\\\\');
/**
* Returns whether the path looks like a Windows path.
*/
export const isWindowsPath = (path: string) => /^[A-Za-z]:/.test(path);