-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.ts
88 lines (79 loc) · 1.86 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
import {osWindows} from "./os.ts";
/**
* Convert from backslash to slash.
* Useful for converting from Windows path to UNIX path.
* @example
* ```ts
* const path = pathSepL("C:\\file");
* ```
*/
export function pathSepL(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 = pathSepW("C:/file");
* ```
*/
export function pathSepW(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 = pathTmp();
* ```
*/
export function pathTmp():string{
return osWindows ? "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 = pathVar();
* ```
*/
export function pathVar():string{
return osWindows ? "C:/ProgramData" : "/var";
}
/**
* System-wide user config directory path for each OS.
* `~/.config` for UNIX and `~/AppData/Roaming` for Windows.
* @example
* ```ts
* const path = pathConfig();
* ```
*/
export function pathConfig():string{
return `${pathHome()}/${osWindows ? "AppData/Roaming" : ".config"}`;
}
/**
* System-wide home directory path for each OS.
* `${HOME}` for UNIX and `%USERPROFILE%` for Windows.
* @example
* ```ts
* const path = pathHome();
* ```
*/
export function pathHome():string{
const {HOME, USERPROFILE} = Deno.env.toObject();
return osWindows ? pathSepL(USERPROFILE) : HOME;
}
/**
* Directory of `Deno.mainModule`.
* @example
* ```ts
* const path = pathMain();
* ```
*/
export function pathMain():string{
const directory = new URL(Deno.mainModule).pathname.replace(/[^/]*$/, "");
return osWindows ? directory.replace(/^\//, "") : directory;
}