-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathpathUtils.ts
More file actions
100 lines (88 loc) · 2.91 KB
/
pathUtils.ts
File metadata and controls
100 lines (88 loc) · 2.91 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
import * as os from 'os';
import * as path from 'path';
import { NotebookCell, NotebookDocument, Uri, workspace } from 'vscode';
import { isWindows } from './platformUtils';
export function checkUri(scope?: Uri | Uri[] | string): Uri | Uri[] | string | undefined {
if (!scope) {
return undefined;
}
if (Array.isArray(scope)) {
// if the scope is an array, all items must be Uri, check each item
return scope.map((item) => {
const s = checkUri(item);
if (s instanceof Uri) {
return s;
}
throw new Error('Invalid entry, expected Uri.');
});
}
if (scope instanceof Uri) {
if (scope.scheme === 'vscode-notebook-cell') {
const matchingDoc = workspace.notebookDocuments.find((doc) => findCell(scope, doc));
// If we find a matching notebook document, return the Uri of the cell.
return matchingDoc ? matchingDoc.uri : scope;
}
}
return scope;
}
/**
* Find a notebook document by cell Uri.
*/
export function findCell(cellUri: Uri, notebook: NotebookDocument): NotebookCell | undefined {
// Fragment is not unique to a notebook, hence ensure we compare the path as well.
return notebook.getCells().find((cell) => {
return isEqual(cell.document.uri, cellUri);
});
}
function isEqual(uri1: Uri | undefined, uri2: Uri | undefined): boolean {
if (uri1 === uri2) {
return true;
}
if (!uri1 || !uri2) {
return false;
}
return getComparisonKey(uri1) === getComparisonKey(uri2);
}
function getComparisonKey(uri: Uri): string {
return uri
.with({
path: isWindows() ? uri.path.toLowerCase() : uri.path,
fragment: undefined,
})
.toString();
}
export function normalizePath(fsPath: string): string {
const resolvedPath = path.resolve(fsPath).replace(/\\/g, '/');
return isWindows() ? resolvedPath.toLowerCase() : resolvedPath;
}
export function getResourceUri(resourcePath: string, root?: string): Uri | undefined {
try {
if (!resourcePath) {
return undefined;
}
if (resourcePath.includes('://')) {
return Uri.parse(resourcePath);
}
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();
}
/**
* Applies untildify to an array of paths
* @param paths Array of potentially tilde-containing paths
* @returns Array of expanded paths
*/
export function untildifyArray(paths: string[]): string[] {
return paths.map((p) => untildify(p));
}