forked from building5/appdirsjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
403 lines (388 loc) · 12.9 KB
/
index.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (c) 2014. David M. Lee, II.
/*jshint unused:vars */
'use strict';
/**
* Platform specific directory locator.
*
* The API's all follow the same general pattern:
* <ul>
* <li>All arguments are optional. When called with no arguments, the function
* returns the base path, instead of one specific for an application.</li>
* <li>The version is only appended if the <code>appname</code> is provided.
* </li>
* <li>Windows requires the application author, in addition to the application
* name, for locating application specific directories. If not give, this
* defaults to <code>appname</code>.</li>
* <li>Both the OS X and XDG implementations are portable, and will run on any
* platform.</li>
* <li>Windows support is unimplemented, because reasons. When it is
* implemented, it will probably only run on Windows</li>
* </ul>
* @module appdirs
*/
var os = require('os'),
path = require('path');
/**
* Append the name and and version to a path.
*
* Both the appname and version are optional. The version is only appended if
* the appname.
*
* @param {string} dir Base directory.
* @param {string} [appname] Optional name to append.
* @param {string} [version] Optional version to append.
* @returns {string} Resulting path
* @private
*/
var appendNameVersion = function (dir, appname, version) {
if (appname) {
dir = path.join(dir, appname);
if (version) {
dir = path.join(dir, version);
}
}
return dir;
};
/*jshint maxlen:false */
/**
* Windows appdirs implementation.
*
* The standard directory structure for Windows can be found on
* [MSDN]{@link http://support.microsoft.com/default.aspx?scid=kb;en-us;310294#XSLTH3194121123120121120120}.
*/
/*jshint maxlen:80 */
exports.windows = {
userDataDir: function (appname, appauthor, version, roaming) {
var dir = roaming ? process.env.APPDATA : process.env.LOCALAPPDATA;
return appendNameVersion(dir, appname, version);
},
userConfigDir: function (appname, appauthor, version, roaming) {
var dir = roaming ? process.env.APPDATA : process.env.LOCALAPPDATA;
return appendNameVersion(dir, appname, version);
},
userCacheDir: function (appname, appauthor, version) {
return appendNameVersion(process.env.LOCALAPPDATA, appname, version);
},
siteDataDir: function (appname, appauthor, version, multipath) {
var dir = appendNameVersion(process.env.ALLUSERSPROFILE, appname, version);
if (multipath) {
return [dir];
}
return dir;
},
siteConfigDir: function (appname, appauthor, version, multipath) {
var dir = appendNameVersion(process.env.ALLUSERSPROFILE, appname, version);
if (multipath) {
return [dir];
}
return dir;
},
userLogDir: function (appname, appauthor, version) {
return appendNameVersion(process.env.ALLUSERSPROFILE, appname, version);
}
};
/*jshint maxlen:false */
/**
* OS X appdirs implementation.
*
* The standard directory structure for OS X can be found on
* [the apple developer site]{@link https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html}.
*/
/*jshint maxlen:80 */
exports.darwin = {
userDataDir: function (appname, appauthor, version, roaming) {
var dir = path.join(process.env.HOME, 'Library/Application Support');
return appendNameVersion(dir, appname, version);
},
userConfigDir: function (appname, appauthor, version, roaming) {
return exports.darwin.userDataDir(appname, appauthor, version, roaming);
},
userCacheDir: function (appname, appauthor, version) {
var dir = path.join(process.env.HOME, 'Library/Caches');
return appendNameVersion(dir, appname, version);
},
siteDataDir: function (appname, appauthor, version, multipath) {
var dir = appendNameVersion('/Library/Application Support', appname, version);
if (multipath) {
return [dir];
}
return dir;
},
siteConfigDir: function (appname, appauthor, version, multipath) {
return exports.darwin.siteDataDir(appname, appauthor, version, multipath);
},
userLogDir: function (appname, appauthor, version) {
var dir = path.join(process.env.HOME, 'Library/Logs');
return appendNameVersion(dir, appname, version);
}
};
/*jshint maxlen:false */
/**
* XDG appdirs implementation.
*
* The is the standard directory structure appdirs uses for *NIX operating
* systems. The XDG spec can be found on
* [the FreeDesktop standards site]{@link http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html}.
*/
/*jshint maxlen:80 */
exports.xdg = {
userDataDir: function (appname, appauthor, version, roaming) {
var dir = process.env.XDG_DATA_HOME ||
path.join(process.env.HOME, '.local/share');
return appendNameVersion(dir, appname, version);
},
userConfigDir: function (appname, appauthor, version, roaming) {
var dir = process.env.XDG_CONFIG_HOME ||
path.join(process.env.HOME, '.config');
return appendNameVersion(dir, appname, version);
},
userCacheDir: function (appname, appauthor, version) {
var dir = process.env.XDG_CACHE_HOME ||
path.join(process.env.HOME, '.cache');
return appendNameVersion(dir, appname, version);
},
siteDataDir: function (appname, appauthor, version, multipath) {
var dirstr = process.env.XDG_DATA_DIRS ||
['/usr/local/share', '/usr/share'].join(path.delimiter),
dirs = dirstr.split(path.delimiter).map(function (dir) {
return appendNameVersion(dir, appname, version);
});
if (multipath) {
return dirs;
} else {
return dirs[0];
}
},
siteConfigDir: function (appname, appauthor, version, multipath) {
var dirstr = process.env.XDG_CONFIG_DIRS || '/etc/xdg',
dirs = dirstr.split(path.delimiter).map(function (dir) {
return appendNameVersion(dir, appname, version);
});
if (multipath) {
return dirs;
} else {
return dirs[0];
}
},
userLogDir: function (appname, appauthor, version) {
var cacheDir = exports.xdg.userCacheDir(appname, appauthor, version);
return path.join(cacheDir, 'log');
}
};
var impl = (function () {
switch (os.platform()) {
case 'win32':
return exports.windows;
case 'darwin':
return exports.darwin;
default:
return exports.xdg;
}
})();
/**
* Build an AppDirs convenience object.
*
* @param {string} appname Application name.
* @param {string} [appauthor] Application author. Defaults to appname.
* @param {string} [version] Application version.
* @param {boolean} [roaming] If true, use directory for roaming profile.
* @param {boolean} [multipath] If true, return arrays for multipath functions
* (siteDataDir, siteConfigDir).
* @constructor
*/
var AppDirs = exports.AppDirs =
function AppDirs(appname, appauthor, version, roaming, multipath) {
this.appname = appname;
this.appauthor = appauthor;
this.version = version;
this.roaming = roaming;
this.multipath = multipath;
};
/**
* User data directory.
* @returns {string}
*/
AppDirs.prototype.userDataDir = function () {
return impl.userDataDir(this.appname, this.appauthor, this.version,
this.roaming);
};
/**
* User configuration directory.
* @returns {string}
*/
AppDirs.prototype.userConfigDir = function () {
return impl.userConfigDir(this.appname, this.appauthor, this.version,
this.roaming);
};
/**
* User cache directory.
* @returns {string}
*/
AppDirs.prototype.userCacheDir = function () {
return impl.userCacheDir(this.appname, this.appauthor, this.version);
};
/**
* Site data directory.
* @returns {string}
*/
AppDirs.prototype.siteDataDir = function () {
return impl.siteDataDir(this.appname, this.appauthor, this.version,
this.multipath);
};
/**
* Site configuration directory.
* @returns {string}
*/
AppDirs.prototype.siteConfigDir = function () {
return impl.siteConfigDir(this.appname, this.appauthor, this.version,
this.multipath);
};
/**
* User log directory.
* @returns {string}
*/
AppDirs.prototype.userLogDir = function () {
return impl.userLogDir(this.appname, this.appauthor, this.version);
};
/**
* Return full path to the user-specific data dir for this application.
*
* Typical user cache directories are:
* <dl>
* <dt>Mac OS X</dt>
* <dd>~/Library/Application Support/{AppName}</dd>
* <dt>Unix</dt>
* <dd>~/.local/share/{AppName}/log</dd>
* <dt>Win 7 (not roaming)</dt>
* <dd>C:\Users\{username}\AppData\Local\{AppAuthor}\{AppName}</dd>
* <dt>Win 7 (roaming)</dt>
* <dd>C:\Users\{username}\AppData\Roaming\{AppAuthor}\{AppName}</dd>
* </dl>
*
* @param {string} [appname] Application name. If not give, then the base user
* data directory is returned.
* @param {string} [appauthor] Application author's name. This falls back to
* appname.
* @param {string} [version] Optional version to append to the path. Only
* applied when appname is present.
* @param {boolean} [roaming] When set, use the Windows roaming
* appdata directory
* @returns {string} User data directory.
* @function
*/
exports.userDataDir = impl.userDataDir;
/**
* Return full path to the user-specific config dir for this application.
*
* Typical user data directories are:
* <dl>
* <dt>Mac OS X</dt>
* <dd>same as user_data_dir</dd>
* <dt>Unix</dt>
* <dd>~/.config/{AppName}</dd>
* <dt>Windows</dt>
* <dd>same as user_data_dir</dd>
* </dl>
*
* @param {string} [appname] Application name. If not give, then the base user
* config directory is returned.
* @param {string} [appauthor] Application author's name. This falls back to
* appname.
* @param {string} [version] Optional version to append to the path. Only
* applied when appname is present.
* @param {boolean} [roaming] When set, use the Windows roaming
* appdata directory
* @returns {string} User config directory.
* @function
*/
exports.userConfigDir = impl.userConfigDir;
/**
* Return full path to the user-specific cache dir for this application.
*
* Typical user cache directories are:
* <dl>
* <dt>Mac OS X</dt>
* <dd>~/Library/Caches/{AppName}</dd>
* <dt>Unix</dt>
* <dd>~/.cache/{AppName}</dd>
* </dl>
*
* @param {string} [appname] Application name. If not give, then the base user
* cache directory is returned.
* @param {string} [appauthor] Application author's name. This falls back to
* appname.
* @param {string} [version] Optional version to append to the path. Only
* applied when appname is present.
* @returns {string} User cache directory
* @function
*/
exports.userCacheDir = impl.userCacheDir;
/**
* Return full path to the user-shared data dir for this application.
*
* Typical site data directories are:
* <dl>
* <dt>Mac OS X</dt>
* <dd>/Library/Application Support/{AppName}</dd>
* <dt>Unix</dt>
* <dd>/usr/local/share/{AppName} or /usr/share/{AppName}</dd>
* </dl>
*
* @param {string} [appname] Application name. If not give, then the base site
* data directory is returned.
* @param {string} [appauthor] Application author's name. This falls back to
* appname.
* @param {string} [version] Optional version to append to the path. Only
* applied when appname is present.
* @param {boolean} [multipath] If true, on *NIX, all site data dirs are
* returned.
* @returns {string} Site data directory.
* @function
*/
exports.siteDataDir = impl.siteDataDir;
/**
* Return full path to the user-shared config dir for this application.
*
* Typical user data directories are:
* <dl>
* <dt>Mac OS X</dt>
* <dd>same as site_data_dir</dd>
* <dt>Unix</dt>
* <dd>/etc/xdg/{AppName}</dd>
* <dt>Windows</dt>
* <dd>same as site_data_dir</dd>
* </dl>
*
* @param {string} [appname] Application name. If not give, then the base site
* data directory is returned.
* @param {string} [appauthor] Application author's name. This falls back to
* appname.
* @param {string} [version] Optional version to append to the path. Only
* applied when appname is present.
* @param {boolean} [multipath] If true, on *NIX, all site data dirs are
* returned.
* @returns {string} Site config directory.
* @function
*/
exports.siteConfigDir = impl.siteConfigDir;
/**
* Return full path to the user-specific log dir for this application.
*
* Typical user cache directories are:
* <dl>
* <dt>Mac OS X</dt>
* <dd>~/Library/Logs/{AppName}</dd>
* <dt>Unix</dt>
* <dd>~/.cache/{AppName}/log</dd>
* <dl>
*
* @param {string} [appname] Application name. If not give, then the base site
* data directory is returned.
* @param {string} [appauthor] Application author's name. This falls back to
* appname.
* @param {string} [version] Optional version to append to the path. Only
* applied when appname is present.
* @returns {string} User log directory.
* @function
*/
exports.userLogDir = impl.userLogDir;