1'use strict'; 2const path = require('path'); 3const os = require('os'); 4 5const homedir = os.homedir(); 6const tmpdir = os.tmpdir(); 7const {env} = process; 8 9const macos = name => { 10 const library = path.join(homedir, 'Library'); 11 12 return { 13 data: path.join(library, 'Application Support', name), 14 config: path.join(library, 'Preferences', name), 15 cache: path.join(library, 'Caches', name), 16 log: path.join(library, 'Logs', name), 17 temp: path.join(tmpdir, name) 18 }; 19}; 20 21const windows = name => { 22 const appData = env.APPDATA || path.join(homedir, 'AppData', 'Roaming'); 23 const localAppData = env.LOCALAPPDATA || path.join(homedir, 'AppData', 'Local'); 24 25 return { 26 // Data/config/cache/log are invented by me as Windows isn't opinionated about this 27 data: path.join(localAppData, name, 'Data'), 28 config: path.join(appData, name, 'Config'), 29 cache: path.join(localAppData, name, 'Cache'), 30 log: path.join(localAppData, name, 'Log'), 31 temp: path.join(tmpdir, name) 32 }; 33}; 34 35// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html 36const linux = name => { 37 const username = path.basename(homedir); 38 39 return { 40 data: path.join(env.XDG_DATA_HOME || path.join(homedir, '.local', 'share'), name), 41 config: path.join(env.XDG_CONFIG_HOME || path.join(homedir, '.config'), name), 42 cache: path.join(env.XDG_CACHE_HOME || path.join(homedir, '.cache'), name), 43 // https://wiki.debian.org/XDGBaseDirectorySpecification#state 44 log: path.join(env.XDG_STATE_HOME || path.join(homedir, '.local', 'state'), name), 45 temp: path.join(tmpdir, username, name) 46 }; 47}; 48 49const envPaths = (name, options) => { 50 if (typeof name !== 'string') { 51 throw new TypeError(`Expected string, got ${typeof name}`); 52 } 53 54 options = Object.assign({suffix: 'nodejs'}, options); 55 56 if (options.suffix) { 57 // Add suffix to prevent possible conflict with native apps 58 name += `-${options.suffix}`; 59 } 60 61 if (process.platform === 'darwin') { 62 return macos(name); 63 } 64 65 if (process.platform === 'win32') { 66 return windows(name); 67 } 68 69 return linux(name); 70}; 71 72module.exports = envPaths; 73// TODO: Remove this for the next major release 74module.exports.default = envPaths; 75