11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst path = require('path');
31cb0ef41Sopenharmony_ciconst os = require('os');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst homedir = os.homedir();
61cb0ef41Sopenharmony_ciconst tmpdir = os.tmpdir();
71cb0ef41Sopenharmony_ciconst {env} = process;
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst macos = name => {
101cb0ef41Sopenharmony_ci	const library = path.join(homedir, 'Library');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci	return {
131cb0ef41Sopenharmony_ci		data: path.join(library, 'Application Support', name),
141cb0ef41Sopenharmony_ci		config: path.join(library, 'Preferences', name),
151cb0ef41Sopenharmony_ci		cache: path.join(library, 'Caches', name),
161cb0ef41Sopenharmony_ci		log: path.join(library, 'Logs', name),
171cb0ef41Sopenharmony_ci		temp: path.join(tmpdir, name)
181cb0ef41Sopenharmony_ci	};
191cb0ef41Sopenharmony_ci};
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst windows = name => {
221cb0ef41Sopenharmony_ci	const appData = env.APPDATA || path.join(homedir, 'AppData', 'Roaming');
231cb0ef41Sopenharmony_ci	const localAppData = env.LOCALAPPDATA || path.join(homedir, 'AppData', 'Local');
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci	return {
261cb0ef41Sopenharmony_ci		// Data/config/cache/log are invented by me as Windows isn't opinionated about this
271cb0ef41Sopenharmony_ci		data: path.join(localAppData, name, 'Data'),
281cb0ef41Sopenharmony_ci		config: path.join(appData, name, 'Config'),
291cb0ef41Sopenharmony_ci		cache: path.join(localAppData, name, 'Cache'),
301cb0ef41Sopenharmony_ci		log: path.join(localAppData, name, 'Log'),
311cb0ef41Sopenharmony_ci		temp: path.join(tmpdir, name)
321cb0ef41Sopenharmony_ci	};
331cb0ef41Sopenharmony_ci};
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
361cb0ef41Sopenharmony_ciconst linux = name => {
371cb0ef41Sopenharmony_ci	const username = path.basename(homedir);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci	return {
401cb0ef41Sopenharmony_ci		data: path.join(env.XDG_DATA_HOME || path.join(homedir, '.local', 'share'), name),
411cb0ef41Sopenharmony_ci		config: path.join(env.XDG_CONFIG_HOME || path.join(homedir, '.config'), name),
421cb0ef41Sopenharmony_ci		cache: path.join(env.XDG_CACHE_HOME || path.join(homedir, '.cache'), name),
431cb0ef41Sopenharmony_ci		// https://wiki.debian.org/XDGBaseDirectorySpecification#state
441cb0ef41Sopenharmony_ci		log: path.join(env.XDG_STATE_HOME || path.join(homedir, '.local', 'state'), name),
451cb0ef41Sopenharmony_ci		temp: path.join(tmpdir, username, name)
461cb0ef41Sopenharmony_ci	};
471cb0ef41Sopenharmony_ci};
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciconst envPaths = (name, options) => {
501cb0ef41Sopenharmony_ci	if (typeof name !== 'string') {
511cb0ef41Sopenharmony_ci		throw new TypeError(`Expected string, got ${typeof name}`);
521cb0ef41Sopenharmony_ci	}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci	options = Object.assign({suffix: 'nodejs'}, options);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci	if (options.suffix) {
571cb0ef41Sopenharmony_ci		// Add suffix to prevent possible conflict with native apps
581cb0ef41Sopenharmony_ci		name += `-${options.suffix}`;
591cb0ef41Sopenharmony_ci	}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci	if (process.platform === 'darwin') {
621cb0ef41Sopenharmony_ci		return macos(name);
631cb0ef41Sopenharmony_ci	}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci	if (process.platform === 'win32') {
661cb0ef41Sopenharmony_ci		return windows(name);
671cb0ef41Sopenharmony_ci	}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci	return linux(name);
701cb0ef41Sopenharmony_ci};
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_cimodule.exports = envPaths;
731cb0ef41Sopenharmony_ci// TODO: Remove this for the next major release
741cb0ef41Sopenharmony_cimodule.exports.default = envPaths;
75