-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod.ts
107 lines (88 loc) · 2.52 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
import {
join as pathJoin,
basename as pathBasename,
} from "https://deno.land/std/path/mod.ts";
const homedir: string | null = Deno.dir("home");
const tmpdir: string | null = Deno.dir("tmp");
interface IOptions {
suffix: string;
}
const macos = (name: string) => {
if (!homedir) {
throw new Error("Can't extract the home directory.");
}
if (!tmpdir) {
throw new Error("Can't extract the tmp directory.");
}
const library = pathJoin(homedir, "Library");
return {
data: pathJoin(library, "Application Support", name),
config: pathJoin(library, "Preferences", name),
cache: pathJoin(library, "Caches", name),
log: pathJoin(library, "Logs", name),
temp: pathJoin(tmpdir, name),
};
};
const windows = (name: string) => {
if (!homedir) {
throw new Error("Can't extract the home directory.");
}
if (!tmpdir) {
throw new Error("Can't extract the tmp directory.");
}
const appData = Deno.env.get("APPDATA") ||
pathJoin(homedir, "AppData", "Roaming");
const localAppData = Deno.env.get("LOCALAPPDATA") ||
pathJoin(homedir, "AppData", "Local");
return {
data: pathJoin(localAppData, name, "Data"),
config: pathJoin(appData, name, "Config"),
cache: pathJoin(localAppData, name, "Cache"),
log: pathJoin(localAppData, name, "Log"),
temp: pathJoin(tmpdir, name),
};
};
const linux = (name: string) => {
if (!homedir) {
throw new Error("Can't extract the home directory.");
}
if (!tmpdir) {
throw new Error("Can't extract the tmp directory.");
}
const username = pathBasename(homedir);
return {
data: pathJoin(
Deno.env.get("XDG_DATA_HOME") || pathJoin(homedir, ".local", "share"),
name,
),
config: pathJoin(
Deno.env.get("XDG_CONFIG_HOME") || pathJoin(homedir, ".config"),
name,
),
cache: pathJoin(
Deno.env.get("XDG_CACHE_HOME") || pathJoin(homedir, ".cache"),
name,
),
log: pathJoin(
Deno.env.get("XDG_STATE_HOME") || pathJoin(homedir, ".local", "state"),
name,
),
temp: pathJoin(tmpdir, username, name),
};
};
export default (name: string, options: IOptions = { suffix: "deno" }) => {
if (typeof name !== "string") {
throw new TypeError(`Expected string, got ${typeof name}`);
}
// Add suffix to prevent possible conflict with native apps
if (options.suffix) {
name += `-${options.suffix}`;
}
if (Deno.build.os === "darwin") {
return macos(name);
}
if (Deno.build.os === "windows") {
return windows(name);
}
return linux(name);
};