forked from folder/xdg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userdirs.js
265 lines (231 loc) · 7.96 KB
/
userdirs.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
'use strict';
const isWindows = process.platform === 'win32';
const path = require('path');
const utils = require('./utils');
const { homedir, load } = utils;
/**
* Get the XDG User Directories for Linux, or equivalents for Windows or MaxOS.
*
* @name .userdirs()
* @param {Object} `options`
* @return {Object} Returns an object of directory paths.
* @api public
*/
const userdirs = (options = {}) => {
const platform = options.platform || (isWindows ? 'win32' : 'linux');
const fn = userdirs[platform];
if (typeof fn !== 'function') {
throw new Error(`Platform "${platform}" is not supported`);
}
return fn(options);
};
/**
* Returns an object to with paths to `user-dirs.*` files, as well as functions to
* load each file.
*
* @name .userdirs.expand()
* @param {Object} `options`
* @param {Object} `paths` Optionally pass the paths from the `userdirs()` function to avoid creating them again.
* @return {Object} Returns an object with a `paths` object, and `config`, `defaults`, `dirs`, and `create` functions for actually loading the user-dir files.
* @api public
*/
userdirs.expand = (options = {}, paths = userdirs(options)) => {
const home = userdirs.home(options);
const resolve = filepath => path.join(home, filepath);
const data = {
paths,
config: opts => userdirs.load(paths.conf, { ...options, ...opts }),
defaults: opts => userdirs.load(paths.defaults, { ...options, ...opts }, resolve),
dirs: opts => userdirs.load(paths.dirs, { ...options, ...opts }, resolve),
create: opts => {
const config = data.config(opts);
if (config.enabled !== false) {
return Object.assign(data.defaults(opts), data.dirs(opts));
}
return {};
}
};
return data;
};
/**
* Loads and parses the contents of `user-dirs.conf`, if one exists in user home.
*
* ```js
* const config = userdirs.conf();
* console.log(config);
* //=> { enabled: true, filename_encoding: 'UTF-8' }
* ```
* @name .userdirs.conf()
* @param {Object} `options`
* @return {Object} Returns configur
* @api public
*/
userdirs.conf = options => {
const dirs = userdirs({ ...options, load: false });
return userdirs.load(dirs.conf, options);
};
/**
* Loads and parses the contents of `user-dirs.defaults`, if one exists in user home.
*
* ```js
* const defaults = userdirs.defaults();
* console.log(defaults);
* ```
* // Example results:
* // {
* // XDG_DESKTOP_DIR: '/Users/jonschlinkert/Desktop',
* // XDG_DOWNLOAD_DIR: '/Users/jonschlinkert/Downloads',
* // XDG_TEMPLATES_DIR: '/Users/jonschlinkert/Templates',
* // XDG_PUBLICSHARE_DIR: '/Users/jonschlinkert/Public',
* // XDG_DOCUMENTS_DIR: '/Users/jonschlinkert/Documents',
* // XDG_MUSIC_DIR: '/Users/jonschlinkert/Music',
* // XDG_PICTURES_DIR: '/Users/jonschlinkert/Pictures',
* // XDG_VIDEOS_DIR: '/Users/jonschlinkert/Videos'
* // }
* @name .userdirs.defaults()
* @param {Object} `options`
* @return {Object} Returns an object of paths.
* @api public
*/
userdirs.defaults = (options = {}) => {
const home = userdirs.home(options);
const dirs = userdirs({ ...options, load: false });
const resolve = filepath => path.join(home, filepath);
return userdirs.load(dirs.defaults, options, resolve);
};
/**
* Loads and parses the contents of `user-dirs.dirs`, if one exists in user home.
*
* ```js
* const dirs = userdirs.dirs();
* console.log(dirs);
* // Example results:
* // {
* // XDG_MUSIC_DIR: '/Users/jonschlinkert/Documents/Music',
* // XDG_PICTURES_DIR: '/Users/jonschlinkert/Documents/Pictures',
* // XDG_TEMPLATES_DIR: '/Users/jonschlinkert/Documents/Templates',
* // XDG_VIDEOS_DIR: '/Users/jonschlinkert/Documents/Videos'
* // }
* ```
* @name .userdirs.dirs()
* @param {Object} `options`
* @return {Object} Returns an object of paths.
* @api public
*/
userdirs.dirs = (options = {}) => {
const home = userdirs.home(options);
const dirs = userdirs({ ...options, load: false });
const resolve = filepath => path.join(home, filepath);
return userdirs.load(dirs.dirs, options, resolve);
};
/**
* Get the actual XDG User Directories to use for MacOS. Gets the `user-dirs.conf`,
* `user-dirs.defaults`, and the `user-dirs.dirs` files and, if not disabled in
* `user-dirs.conf`, merges the values in defaults and dirs to create the paths to use.
*
* ```js
* const dirs = userdirs.create();
* console.log(dirs);
* // Example results:
* // {
* // XDG_DESKTOP_DIR: '/Users/jonschlinkert/Desktop',
* // XDG_DOWNLOAD_DIR: '/Users/jonschlinkert/Downloads',
* // XDG_TEMPLATES_DIR: '/Users/jonschlinkert/Documents/Templates',
* // XDG_PUBLICSHARE_DIR: '/Users/jonschlinkert/Public',
* // XDG_DOCUMENTS_DIR: '/Users/jonschlinkert/Documents',
* // XDG_MUSIC_DIR: '/Users/jonschlinkert/Documents/Music',
* // XDG_PICTURES_DIR: '/Users/jonschlinkert/Documents/Pictures',
* // XDG_VIDEOS_DIR: '/Users/jonschlinkert/Documents/Videos'
* // }
* ```
* @name .userdirs.create()
* @param {Object} `options`
* @return {Object} Returns an object of paths.
* @api public
*/
userdirs.create = (...args) => userdirs.expand(...args).create();
/**
* Get the XDG User Directories for MacOS. This method is used by the main function
* when `process.platform` is `darwin`. Exposed as a method so you can call it directly
* if necessary. Also aliased as `userdirs.macos()`.
*
* ```js
* const { dirs, conf, defaults } = userdirs.darwin(); // or userdirs.macos();
* ```
* @name .userdirs.darwin()
* @param {Object} `options`
* @return {Object} Returns an object of paths.
* @api public
*/
userdirs.darwin = (options = {}) => {
const env = options.env || process.env;
const etc = '/etc/xdg';
const home = options.homedir || homedir('darwin');
const lib = path.join(home, 'Library');
const app = env.XDG_CONFIG_HOME || path.join(lib, 'Application Support');
return {
conf: env.XDG_USER_DIRS_CONF || path.join(etc, 'user-dirs.conf'),
defaults: env.XDG_USER_DIRS_DEFAULTS || path.join(etc, 'user-dirs.defaults'),
dirs: env.XDG_USER_DIRS || path.join(app, 'user-dirs.dirs')
};
};
/**
* Gets the XDG User Directories for Linux. Used by the main export when
* `process.platform` is `linux`.
*
* ```js
* const { dirs, conf, defaults } = userdirs.linux();
* ```
* @name .userdirs.linux()
* @return {Object} Returns an object of paths.
* @return {Object} Returns an object of paths.
* @api public
*/
userdirs.linux = (options = {}) => {
const env = options.env || process.env;
const home = options.homedir || homedir('linux');
const config = env.XDG_CONFIG_HOME || path.join(home, '.config');
const etc = '/etc/xdg';
return {
conf: env.XDG_USER_DIRS_CONF || path.join(etc, 'user-dirs.conf'),
defaults: env.XDG_USER_DIRS_DEFAULTS || path.join(etc, 'user-dirs.defaults'),
dirs: env.XDG_USER_DIRS || path.join(config, 'user-dirs.dirs')
};
};
/**
* Gets the XDG User Directories for MacOS. Used by the `userdirs()` function when
* `process.platform` is `win32`. Also aliased as `userdirs.windows()`.
*
* ```js
* const { dirs, conf, defaults } = userdirs.win32(); // or userdirs.windows();
* ```
* @name .userdirs.win32()
* @param {Object} `options`
* @return {Object} Returns an object of paths.
* @api public
*/
userdirs.win32 = (options = {}) => {
const env = options.env || process.env;
const home = options.homedir || homedir('win32');
const { APPDATA = path.join(home, 'AppData', 'Roaming') } = env;
const appdata = env.XDG_CONFIG_HOME || APPDATA;
return {
conf: env.XDG_USER_DIRS_CONF || path.join(home, 'user-dirs.conf'),
defaults: env.XDG_USER_DIRS_DEFAULTS || path.join(home, 'user-dirs.defaults'),
dirs: env.XDG_USER_DIRS || path.join(appdata, 'user-dirs.dirs')
};
};
/**
* Convenience methods
*/
userdirs.home = (options = {}) => {
const platform = options.platform || (isWindows ? 'win32' : 'linux');
return options.homedir || homedir(platform);
};
userdirs.load = load;
userdirs.windows = userdirs.win32;
userdirs.macos = userdirs.darwin;
/**
* Expose "userdirs"
*/
module.exports = userdirs;