-
-
Notifications
You must be signed in to change notification settings - Fork 591
/
paths.ts
59 lines (55 loc) · 1.38 KB
/
paths.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
import { platform } from 'os';
import Uri from 'vscode-uri';
/**
* Vetur mainly deals with paths / uris from two objects
*
* - `TextDocument` from `vscode-languageserver`
* - `SourceFile` from `typescript`
*
* ## `TextDocument.uri`
*
* - macOS / Linux: file:///foo/bar.vue
* - Windows: file:///c%3A/foo/bar.vue (%3A is `:`)
*
* ## `SourceFile.fileName`
*
* - macOS / Linux: /foo/bar.vue
* - Windows: c:/foo/bar.vue
*
* ## vscode-uri
*
* - `Uri.parse`: Takes full URI starting with `file://`
* - `Uri.file`: Takes file path
*
* ### `fsPath` vs `path`
*
* - macOS / Linux:
* ```
* > Uri.parse('file:///foo/bar.vue').fsPath
* '/foo/bar.vue'
* > Uri.parse('file:///foo/bar.vue').path
* '/foo/bar.vue'
* ```
* - Windows
* ```
* > Uri.parse('file:///c%3A/foo/bar.vue').fsPath
* 'c:\\foo\\bar.vue' (\\ escapes to \)
* > Uri.parse('file:///c%3A/foo/bar.vue').path
* '/c:/foo/bar.vue'
* ```
*/
export function getFileFsPath(documentUri: string): string {
return Uri.parse(documentUri).fsPath;
}
export function getFilePath(documentUri: string): string {
const IS_WINDOWS = platform() === 'win32';
if (IS_WINDOWS) {
// Windows have a leading slash like /C:/Users/pine
return Uri.parse(documentUri).path.slice(1);
} else {
return Uri.parse(documentUri).path;
}
}
export function normalizeFileNameToFsPath(fileName: string) {
return Uri.file(fileName).fsPath;
}