-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.ts
89 lines (80 loc) · 1.9 KB
/
path.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
import {osWin} from "./os.ts";
/**
* Convert from backslash to slash.
* Useful for converting from Windows path to UNIX path.
* @example
* ```ts
* const path = slashU("C:\\file");
* ```
*/
export function slashU(path:string):string{
return path.replace(/\\/g, "/");
}
/**
* Convert from slash to backslash.
* Useful for converting from UNIX path to Windows path.
* @example
* ```ts
* const path = slashW("C:/file");
* ```
*/
export function slashW(path:string):string{
return path.replace(/\//g, "\\");
}
/**
* System-wide temporary directory path for each OS.
* `/tmp` for UNIX and `C:/Windows/Temp` for Windows.
* @example
* ```ts
* const path = tmpPath();
* ```
*/
export function tmpPath():string{
return osWin ? "C:/Windows/Temp" : "/tmp";
}
/**
* System-wide application data directory path for each OS.
* `/var` for UNIX and `C:/ProgramData` for Windows.
* @example
* ```ts
* const path = dataPath();
* ```
*/
export function dataPath():string{
return osWin ? "C:/ProgramData" : "/var";
}
/**
* System-wide user config directory path for each OS.
* `~/.config` for UNIX and `~/AppData/Roaming` for Windows.
* @example
* ```ts
* const path = configPath();
* ```
*/
export function configPath():string{
return `${homePath()}/${osWin ? "AppData/Roaming" : ".config"}`;
}
/**
* System-wide home directory path for each OS.
* `${HOME}` for UNIX and `%USERPROFILE%` for Windows.
* @example
* ```ts
* const path = homePath();
* ```
*/
export function homePath():string{
const {HOME, USERPROFILE} = Deno.env.toObject();
return osWin ? slashU(USERPROFILE) : HOME;
}
/**
* Directory of `Deno.mainModule`.
* @example
* ```ts
* const path = mainPath();
* ```
*/
export function mainPath():string{
const {protocol, origin, pathname} = new URL(Deno.mainModule);
const path = pathname.replace(/[^/]*$/, "");
return protocol === "file:" ? osWin ? path.replace(/^\//, "") : path : `${origin}${path}`;
}