Skip to content

Commit

Permalink
add cross-platform XDG support (plus comment polish)
Browse files Browse the repository at this point in the history
  • Loading branch information
rivy committed Jun 29, 2019
1 parent a0b2f75 commit 4d87f8d
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,43 @@ const homedir = os.homedir();
const tmpdir = os.tmpdir();
const {env} = process;

//# ref: <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html> @@ <https://archive.is/aAhtw>
//# ref: <https://wiki.debian.org/XDGBaseDirectorySpecification#state> @@ <http://archive.is/pahId>

const macos = name => {
const library = path.join(homedir, 'Library');

return {
cache: path.join(library, 'Caches', name),
config: path.join(library, 'Preferences', name),
data: path.join(library, 'Application Support', name),
log: path.join(library, 'Logs', name),
cache: env.XDG_CACHE_HOME ? path.join(env.XDG_CACHE_HOME, name) : path.join(library, 'Caches', name),
config: env.XDG_CONFIG_HOME ? path.join(env.XDG_CONFIG_HOME, name) : path.join(library, 'Preferences', name),
data: env.XDG_DATA_HOME ? path.join(env.XDG_DATA_HOME, name) : path.join(library, 'Application Support', name),
log: env.XDG_STATE_HOME ? path.join(env.XDG_STATE_HOME, name) : path.join(library, 'Logs', name),
temp: path.join(tmpdir, name)
};
};

const windows = name => {
const appData = env.APPDATA || path.join(homedir, 'AppData', 'Roaming');
const localAppData = env.LOCALAPPDATA || path.join(homedir, 'AppData', 'Local');
// #ref: <https://www.thewindowsclub.com/local-localnow-roaming-folders-windows-10> @@ <http://archive.is/tDEPl>
const appData = env.APPDATA || path.join(homedir, 'AppData', 'Roaming'); // "AppData/Roaming" data *may* follow user between machines
const localAppData = env.LOCALAPPDATA || path.join(homedir, 'AppData', 'Local'); // "AppData/Local" is local-machine-only user data

return {
// Data/config/cache/log are invented by me as Windows isn't opinionated about this
cache: path.join(localAppData, name, 'Cache'),
config: path.join(appData, name, 'Config'),
data: path.join(appData, name, 'Data'),
log: path.join(localAppData, name, 'Log'),
// Locations for data/config/cache/log are invented (Windows doesn't have a popular convention)
cache: env.XDG_CACHE_HOME ? path.join(env.XDG_CACHE_HOME, name) : path.join(localAppData, name, 'Cache'),
config: env.XDG_CONFIG_HOME ? path.join(env.XDG_CONFIG_HOME, name) : path.join(appData, name, 'Config'),
data: env.XDG_DATA_HOME ? path.join(env.XDG_DATA_HOME, name) : path.join(appData, name, 'Data'),
log: env.XDG_STATE_HOME ? path.join(env.XDG_STATE_HOME, name) : path.join(localAppData, name, 'Log'),
temp: path.join(tmpdir, name)
};
};

// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
const linux = name => {
const username = path.basename(homedir);

return {
cache: path.join(env.XDG_CACHE_HOME || path.join(homedir, '.cache'), name),
config: path.join(env.XDG_CONFIG_HOME || path.join(homedir, '.config'), name),
data: path.join(env.XDG_DATA_HOME || path.join(homedir, '.local', 'share'), name),
// https://wiki.debian.org/XDGBaseDirectorySpecification#state
log: path.join(env.XDG_STATE_HOME || path.join(homedir, '.local', 'state'), name),
temp: path.join(tmpdir, username, name)
};
Expand Down

0 comments on commit 4d87f8d

Please sign in to comment.