generated from justjavac/deno_starter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.ts
190 lines (187 loc) · 8.39 KB
/
mod.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import home_dir from "./home_dir/mod.ts";
import cache_dir from "./cache_dir/mod.ts";
import config_dir from "./config_dir/mod.ts";
import data_dir from "./data_dir/mod.ts";
import data_local_dir from "./data_local_dir/mod.ts";
import download_dir from "./download_dir/mod.ts";
import tmp_dir from "./tmp_dir/mod.ts";
export type DirKind =
| "home"
| "cache"
| "config"
| "data"
| "data_local"
| "download"
| "tmp";
// | "executable"
// | "audio"
// | "desktop"
// | "document"
// | "font"
// | "picture"
// | "public"
// | "template"
// | "tmp"
// | "video";
/** Returns the user and platform specific directories.
*
* ```ts
* import { dir } from "https://deno.land/x/dir/mod.ts";
* const homeDirectory = dir("home");
* ```
*
* Requires `allow-env` permission.
*
* Returns `null` if there is no applicable directory or if any other error
* occurs.
*
* Argument values: `"home"`, `"cache"`, `"config"`, `"data"`, `"data_local"`, `"download"`,
*
* `"home"`
*
* |Platform | Value | Example |
* | ------- | -----------------------------------------| -----------------------|
* | Linux | `$HOME` | /home/justjavac |
* | macOS | `$HOME` | /Users/justjavac |
* | Windows | `{FOLDERID_Profile}` | C:\Users\justjavac |
*
* `"cache"`
*
* |Platform | Value | Example |
* | ------- | ----------------------------------- | -------------------------------- |
* | Linux | `$XDG_CACHE_HOME` or `$HOME`/.cache | /home/justjavac/.cache |
* | macOS | `$HOME`/Library/Caches | /Users/justjavac/Library/Caches |
* | Windows | `{FOLDERID_LocalAppData}` | C:\Users\justjavac\AppData\Local |
*
* `"config"`
*
* |Platform | Value | Example |
* | ------- | ------------------------------------- | ------------------------------------ |
* | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/justjavac/.config |
* | macOS | `$HOME`/Library/Preferences | /Users/justjavac/Library/Preferences |
* | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\justjavac\AppData\Roaming |
*
* `"executable"`
*
* |Platform | Value | Example |
* | ------- | --------------------------------------------------------------- | ---------------------------|
* | Linux | `XDG_BIN_HOME` or `$XDG_DATA_HOME`/../bin or `$HOME`/.local/bin | /home/justjavac/.local/bin |
* | macOS | - | - |
* | Windows | - | - |
*
* `"data"`
*
* | Platform | Value | Example |
* | -------- | ---------------------------------------- | -------------------------------------------- |
* | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/justjavac/.local/share |
* | macOS | `$HOME`/Library/Application Support | /Users/justjavac/Library/Application Support |
* | Windows | `$APPDATA` | C:\Users\justjavac\AppData\Roaming |
*
* `"data_local"`
*
* | Platform | Value | Example |
* | -------- | ---------------------------------------- | -------------------------------------------- |
* | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/justjavac/.local/share |
* | macOS | `$HOME`/Library/Application Support | /Users/justjavac/Library/Application Support |
* | Windows | `$LOCALAPPDATA` | C:\Users\justjavac\AppData\Local |
*
* `"audio"`
*
* |Platform | Value | Example |
* | ------- | ------------------ | ------------------------ |
* | Linux | `XDG_MUSIC_DIR` | /home/justjavac/Music |
* | macOS | `$HOME`/Music | /Users/justjavac/Music |
* | Windows | `{FOLDERID_Music}` | C:\Users\justjavac\Music |
*
* `"desktop"`
*
* |Platform | Value | Example |
* | ------- | -------------------- | -------------------------- |
* | Linux | `XDG_DESKTOP_DIR` | /home/justjavac/Desktop |
* | macOS | `$HOME`/Desktop | /Users/justjavac/Desktop |
* | Windows | `{FOLDERID_Desktop}` | C:\Users\justjavac\Desktop |
*
* `"document"`
*
* |Platform | Value | Example |
* | ------- | ---------------------- | ---------------------------- |
* | Linux | `XDG_DOCUMENTS_DIR` | /home/justjavac/Documents |
* | macOS | `$HOME`/Documents | /Users/justjavac/Documents |
* | Windows | `{FOLDERID_Documents}` | C:\Users\justjavac\Documents |
*
* `"download"`
*
* |Platform | Value | Example |
* | ------- | ---------------------- | ---------------------------- |
* | Linux | `XDG_DOWNLOAD_DIR` | /home/justjavac/Downloads |
* | macOS | `$HOME`/Downloads | /Users/justjavac/Downloads |
* | Windows | `{FOLDERID_Downloads}` | C:\Users\justjavac\Downloads |
*
* `"font"`
*
* |Platform | Value | Example |
* | ------- | ---------------------------------------------------- | ---------------------------------- |
* | Linux | `$XDG_DATA_HOME`/fonts or `$HOME`/.local/share/fonts | /home/justjavac/.local/share/fonts |
* | macOS | `$HOME/Library/Fonts` | /Users/justjavac/Library/Fonts |
* | Windows | – | – |
*
* `"picture"`
*
* |Platform | Value | Example |
* | ------- | --------------------- | --------------------------- |
* | Linux | `XDG_PICTURES_DIR` | /home/justjavac/Pictures |
* | macOS | `$HOME`/Pictures | /Users/justjavac/Pictures |
* | Windows | `{FOLDERID_Pictures}` | C:\Users\justjavac\Pictures |
*
* `"public"`
*
* |Platform | Value | Example |
* | ------- | --------------------- | ----------------------- |
* | Linux | `XDG_PUBLICSHARE_DIR` | /home/justjavac/Public |
* | macOS | `$HOME`/Public | /Users/justjavac/Public |
* | Windows | `{FOLDERID_Public}` | C:\Users\Public |
*
* `"template"`
*
* |Platform | Value | Example |
* | ------- | ---------------------- | -------------------------------------------------------------- |
* | Linux | `XDG_TEMPLATES_DIR` | /home/justjavac/Templates |
* | macOS | – | – |
* | Windows | `{FOLDERID_Templates}` | C:\Users\justjavac\AppData\Roaming\Microsoft\Windows\Templates |
*
* `"tmp"`
*
* |Platform | Value | Example |
* | ------- | ---------------------- | ---------------------------------------------------------- |
* | Linux | `TMPDIR` | /tmp |
* | macOS | `TMPDIR` | /tmp |
* | Windows | `{TMP}` | C:\Users\justjavac\AppData\Local\Temp |
*
* `"video"`
*
* |Platform | Value | Example |
* | ------- | ------------------- | ------------------------- |
* | Linux | `XDG_VIDEOS_DIR` | /home/justjavac/Videos |
* | macOS | `$HOME`/Movies | /Users/justjavac/Movies |
* | Windows | `{FOLDERID_Videos}` | C:\Users\justjavac\Videos |
*/
export default function dir(kind: DirKind): string | null {
switch (kind) {
case "home":
return home_dir();
case "cache":
return cache_dir();
case "config":
return config_dir();
case "data":
return data_dir();
case "data_local":
return data_local_dir();
case "download":
return download_dir();
case "tmp":
return tmp_dir();
default:
return null;
}
}