forked from neo4j-devtools/relate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env-paths.ts
127 lines (107 loc) · 3.9 KB
/
env-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
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
/**
* This utility returns the directories where the application should store
* information. These paths can be overridden by the user either globally
* using the XDG base dir environment variables, or just for our application
* using our environment variables.
*
* The priority of these environment variables is the following:
*
* Mac & Linux: NEO4J_RELATE_*_HOME > XDG_*_HOME > System defaults
* Windows: NEO4J_RELATE_*_HOME > APPDATA & LOCALAPPDATA > System defaults
*
* Docs:
* - MacOS: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW1
* - Windows: https://docs.microsoft.com/en-us/windows/uwp/design/app-settings/store-and-retrieve-app-data
* - Linux: https://specifications.freedesktop.org/basedir-spec/latest/
*/
import path from 'path';
import os from 'os';
export type EnvPaths = {
cache: string;
config: string;
data: string;
tmp: string;
};
type Overrides = {
cache: string | undefined;
config: string | undefined;
data: string | undefined;
};
const homedir = os.homedir();
const tmpdir = os.tmpdir();
const getOverrides = (): {app: Overrides; system: Overrides} => {
const {env} = process;
return {
app: {
cache: env.NEO4J_RELATE_CACHE_HOME,
config: env.NEO4J_RELATE_CONFIG_HOME,
data: env.NEO4J_RELATE_DATA_HOME,
},
system: {
cache: env.XDG_CACHE_HOME,
config: env.XDG_CONFIG_HOME,
data: env.XDG_DATA_HOME,
},
};
};
export const macOS = (): EnvPaths => {
const {app, system} = getOverrides();
const name = 'com.Neo4j.Relate';
const systemFullPaths = {
cache: system.cache && path.join(system.cache, name),
config: system.config && path.join(system.config, name),
data: system.data && path.join(system.data, name),
};
const defaults = {
cache: path.join(homedir, 'Library', 'Caches', name),
config: path.join(homedir, 'Library', 'Application Support', name, 'Config'),
data: path.join(homedir, 'Library', 'Application Support', name, 'Data'),
};
return {
cache: app.cache || systemFullPaths.cache || defaults.cache,
config: app.config || systemFullPaths.config || defaults.config,
data: app.data || systemFullPaths.data || defaults.data,
tmp: path.join(tmpdir, name),
};
};
export const windows = (): EnvPaths => {
const {app} = getOverrides();
const name = path.join('Neo4j', 'Relate');
const appData = process.env.APPDATA || path.join(homedir, 'AppData', 'Roaming');
const localAppData = process.env.LOCALAPPDATA || path.join(homedir, 'AppData', 'Local');
const defaults = {
cache: path.join(localAppData, name, 'Cache'),
config: path.join(appData, name, 'Config'),
data: path.join(localAppData, name, 'Data'),
};
return {
cache: app.cache || defaults.cache,
config: app.config || defaults.config,
data: app.data || defaults.data,
tmp: path.join(tmpdir, name),
};
};
export const linux = (): EnvPaths => {
const {app, system} = getOverrides();
const name = 'neo4j-relate';
const defaults = {
cache: path.join(homedir, '.cache'),
config: path.join(homedir, '.config'),
data: path.join(homedir, '.local', 'share'),
};
return {
cache: app.cache || path.join(system.cache || defaults.cache, name),
config: app.config || path.join(system.config || defaults.config, name),
data: app.data || path.join(system.data || defaults.data, name),
tmp: path.join(tmpdir, name),
};
};
export const envPaths = (): EnvPaths => {
if (process.platform === 'darwin') {
return macOS();
}
if (process.platform === 'win32') {
return windows();
}
return linux();
};