├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── index.test-d.ts ├── package.json ├── index.js ├── license ├── test.js ├── index.d.ts └── readme.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/xdg-basedir 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | steps: 16 | - uses: actions/checkout@v2 17 | - uses: actions/setup-node@v1 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType, expectError} from 'tsd'; 2 | import {xdgData, xdgConfig, xdgState, xdgCache, xdgRuntime, xdgConfigDirectories, xdgDataDirectories} from './index.js'; 3 | 4 | expectType(xdgData); 5 | expectError(xdgData); 6 | expectType(xdgConfig); 7 | expectError(xdgConfig); 8 | expectType(xdgState); 9 | expectError(xdgState); 10 | expectType(xdgCache); 11 | expectError(xdgCache); 12 | expectType(xdgRuntime); 13 | expectError(xdgRuntime); 14 | expectType(xdgConfigDirectories); 15 | expectError(xdgConfigDirectories); 16 | expectType(xdgDataDirectories); 17 | expectError(xdgDataDirectories); 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "xdg-basedir", 3 | "version": "5.1.0", 4 | "description": "Get XDG Base Directory paths", 5 | "license": "MIT", 6 | "repository": "sindresorhus/xdg-basedir", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": ">=12" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "xdg", 27 | "base", 28 | "directory", 29 | "basedir", 30 | "path", 31 | "data", 32 | "config", 33 | "cache", 34 | "linux", 35 | "unix", 36 | "spec" 37 | ], 38 | "devDependencies": { 39 | "ava": "^1.4.1", 40 | "import-fresh": "^3.0.0", 41 | "tsd": "^0.7.2", 42 | "xo": "^0.24.0" 43 | }, 44 | "ava": { 45 | "serial": true 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import os from 'os'; 2 | import path from 'path'; 3 | 4 | const homeDirectory = os.homedir(); 5 | const {env} = process; 6 | 7 | export const xdgData = env.XDG_DATA_HOME || 8 | (homeDirectory ? path.join(homeDirectory, '.local', 'share') : undefined); 9 | 10 | export const xdgConfig = env.XDG_CONFIG_HOME || 11 | (homeDirectory ? path.join(homeDirectory, '.config') : undefined); 12 | 13 | export const xdgState = env.XDG_STATE_HOME || 14 | (homeDirectory ? path.join(homeDirectory, '.local', 'state') : undefined); 15 | 16 | export const xdgCache = env.XDG_CACHE_HOME || (homeDirectory ? path.join(homeDirectory, '.cache') : undefined); 17 | 18 | export const xdgRuntime = env.XDG_RUNTIME_DIR || undefined; 19 | 20 | export const xdgDataDirectories = (env.XDG_DATA_DIRS || '/usr/local/share/:/usr/share/').split(':'); 21 | 22 | if (xdgData) { 23 | xdgDataDirectories.unshift(xdgData); 24 | } 25 | 26 | export const xdgConfigDirectories = (env.XDG_CONFIG_DIRS || '/etc/xdg').split(':'); 27 | 28 | if (xdgConfig) { 29 | xdgConfigDirectories.unshift(xdgConfig); 30 | } 31 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | /// import importFresh from 'import-fresh'; 3 | 4 | // TODO: Enable tests again when `import-fresh` supports ESM. 5 | 6 | test('x', t => { 7 | t.pass(); 8 | }); 9 | 10 | /// test('xdgData', t => { 11 | // process.env.XDG_DATA_HOME = 'data'; 12 | // const {xdgData} = importFresh('.'); 13 | // t.is(xdgData, 'data'); 14 | // }); 15 | // 16 | // test('xdgConfig', t => { 17 | // process.env.XDG_CONFIG_HOME = 'config'; 18 | // const {xdgConfig} = importFresh('.'); 19 | // t.is(xdgConfig, 'config'); 20 | // }); 21 | // 22 | // test('xdgState', t => { 23 | // process.env.XDG_CONFIG_HOME = 'state'; 24 | // const {xdgState} = importFresh('.'); 25 | // t.is(xdgState, 'state'); 26 | // }); 27 | // 28 | // test('xdgCache', t => { 29 | // process.env.XDG_CACHE_HOME = 'cache'; 30 | // const {xdgCache} = importFresh('.'); 31 | // t.is(xdgCache, 'cache'); 32 | // }); 33 | // 34 | // test('xdgRuntime', t => { 35 | // process.env.XDG_RUNTIME_DIR = 'runtime'; 36 | // const {xdgRuntime} = importFresh('.'); 37 | // t.is(xdgRuntime, 'runtime'); 38 | // }); 39 | // 40 | // test('xdgDataDirectories', t => { 41 | // process.env.XDG_DATA_DIRS = 'dirs:data_dirs'; 42 | // const {xdgDataDirectories} = importFresh('.'); 43 | // t.is(xdgDataDirectories[0], 'data'); 44 | // t.is(xdgDataDirectories[1], 'dirs'); 45 | // t.is(xdgDataDirectories[2], 'data_dirs'); 46 | // }); 47 | // 48 | // test('xdgConfigDirectories', t => { 49 | // process.env.XDG_CONFIG_DIRS = 'dirs:config_dirs'; 50 | // const {xdgConfigDirectories} = importFresh('.'); 51 | // t.is(xdgConfigDirectories[0], 'config'); 52 | // t.is(xdgConfigDirectories[1], 'dirs'); 53 | // t.is(xdgConfigDirectories[2], 'config_dirs'); 54 | // }); 55 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Directory for user-specific data files. 3 | 4 | @example 5 | ``` 6 | import {xdgData} from 'xdg-basedir'; 7 | 8 | console.log(xdgData); 9 | //=> '/home/sindresorhus/.local/share' 10 | ``` 11 | */ 12 | export const xdgData: string | undefined; 13 | 14 | /** 15 | Directory for user-specific configuration files. 16 | 17 | @example 18 | ``` 19 | import {xdgConfig} from 'xdg-basedir'; 20 | 21 | console.log(xdgConfig); 22 | //=> '/home/sindresorhus/.config' 23 | ``` 24 | */ 25 | export const xdgConfig: string | undefined; 26 | 27 | /** 28 | Directory for user-specific state files. 29 | 30 | @example 31 | ``` 32 | import {xdgState} from 'xdg-basedir'; 33 | 34 | console.log(xdgState); 35 | //=> '/home/sindresorhus/.local/state' 36 | ``` 37 | */ 38 | export const xdgState: string | undefined; 39 | 40 | /** 41 | Directory for user-specific non-essential data files. 42 | 43 | @example 44 | ``` 45 | import {xdgCache} from 'xdg-basedir'; 46 | 47 | console.log(xdgCache); 48 | //=> '/home/sindresorhus/.cache' 49 | ``` 50 | */ 51 | export const xdgCache: string | undefined; 52 | 53 | /** 54 | Directory for user-specific non-essential runtime files and other file objects (such as sockets, named pipes, etc). 55 | 56 | @example 57 | ``` 58 | import {xdgRuntime} from 'xdg-basedir'; 59 | 60 | console.log(xdgRuntime); 61 | //=> '/run/user/sindresorhus' 62 | ``` 63 | */ 64 | export const xdgRuntime: string | undefined; 65 | 66 | /** 67 | Preference-ordered array of base directories to search for data files in addition to `xdgData`. 68 | 69 | @example 70 | ``` 71 | import {xdgDataDirectories} from 'xdg-basedir'; 72 | 73 | console.log(xdgDataDirectories); 74 | //=> ['/home/sindresorhus/.local/share', '/usr/local/share/', '/usr/share/'] 75 | ``` 76 | */ 77 | export const xdgDataDirectories: readonly string[]; 78 | 79 | /** 80 | Preference-ordered array of base directories to search for configuration files in addition to `xdgConfig`. 81 | 82 | @example 83 | ``` 84 | import {xdgConfigDirectories} from 'xdg-basedir'; 85 | 86 | console.log(xdgConfigDirectories); 87 | //=> ['/home/sindresorhus/.config', '/etc/xdg'] 88 | ``` 89 | */ 90 | export const xdgConfigDirectories: readonly string[]; 91 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # xdg-basedir 2 | 3 | > Get [XDG Base Directory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) paths 4 | 5 | This package is meant for Linux. You should not use XDG on macOS or Windows. Instead, you should follow their platform conventions. You can use [`env-paths`](https://github.com/sindresorhus/env-paths) for that. 6 | 7 | ## Install 8 | 9 | ``` 10 | $ npm install xdg-basedir 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import {xdgData, xdgConfig, xdgDataDirectories} from 'xdg-basedir'; 17 | 18 | console.log(xdgData); 19 | //=> '/home/sindresorhus/.local/share' 20 | 21 | console.log(xdgConfig); 22 | //=> '/home/sindresorhus/.config' 23 | 24 | console.log(xdgDataDirectories); 25 | //=> ['/home/sindresorhus/.local/share', '/usr/local/share/', '/usr/share/'] 26 | ``` 27 | 28 | ## API 29 | 30 | The exports `xdgData`, `xdgConfig`, `xdgCache`, `xdgRuntime` will return `undefined` in the uncommon case that both the XDG environment variable is not set and the users home directory can't be found. You need to handle this case. A common solution is to [fall back to a temporary directory](https://github.com/yeoman/configstore/blob/b82690fc401318ad18dcd7d151a0003a4898a314/index.js#L15). 31 | 32 | ### xdgData 33 | 34 | Directory for user-specific data files. 35 | 36 | ### xdgConfig 37 | 38 | Directory for user-specific configuration files. 39 | 40 | ### xdgState 41 | 42 | Directory for user-specific state files. 43 | 44 | ### xdgCache 45 | 46 | Directory for user-specific non-essential data files. 47 | 48 | ### xdgRuntime 49 | 50 | Directory for user-specific non-essential runtime files and other file objects (such as sockets, named pipes, etc). 51 | 52 | ### xdgDataDirectories 53 | 54 | Preference-ordered array of base directories to search for data files in addition to `xdgData`. 55 | 56 | ### xdgConfigDirectories 57 | 58 | Preference-ordered array of base directories to search for configuration files in addition to `xdgConfig`. 59 | 60 | --- 61 | 62 |
63 | 64 | Get professional support for this package with a Tidelift subscription 65 | 66 |
67 | 68 | Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies. 69 |
70 |
71 | --------------------------------------------------------------------------------