forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathUtils.ts
More file actions
59 lines (52 loc) · 1.58 KB
/
pathUtils.ts
File metadata and controls
59 lines (52 loc) · 1.58 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
import * as os from 'os';
import * as path from 'path';
import { Uri } from 'vscode';
import { isWindows } from './platformUtils';
export function checkUri(scope?: Uri | Uri[] | string): Uri | Uri[] | string | undefined {
if (scope instanceof Uri) {
if (scope.scheme === 'vscode-notebook-cell') {
return Uri.from({
scheme: 'vscode-notebook',
path: scope.path,
authority: scope.authority,
});
}
}
if (Array.isArray(scope)) {
return scope.map((item) => {
return checkUri(item) as Uri;
});
}
return scope;
}
export function normalizePath(fsPath: string): string {
const path1 = fsPath.replace(/\\/g, '/');
if (isWindows()) {
return path1.toLowerCase();
}
return path1;
}
export function getResourceUri(resourcePath: string, root?: string): Uri | undefined {
try {
if (!resourcePath) {
return undefined;
}
const normalizedPath = normalizePath(resourcePath);
if (normalizedPath.includes('://')) {
return Uri.parse(normalizedPath);
}
if (!path.isAbsolute(resourcePath) && root) {
const absolutePath = path.resolve(root, resourcePath);
return Uri.file(absolutePath);
}
return Uri.file(resourcePath);
} catch (_err) {
return undefined;
}
}
export function untildify(path: string): string {
return path.replace(/^~($|\/|\\)/, `${os.homedir()}$1`);
}
export function getUserHomeDir(): string {
return os.homedir();
}