os-user-dirs 3.0.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +125 -149
  2. package/index.d.ts +280 -19
  3. package/index.js +19 -19
  4. package/package.json +44 -4
package/README.md CHANGED
@@ -1,18 +1,33 @@
1
- # os-user-dirs [![CI](https://github.com/velocitylabo/os-user-dirs/actions/workflows/ci.yml/badge.svg)](https://github.com/velocitylabo/os-user-dirs/actions/workflows/ci.yml)
1
+ # os-user-dirs
2
2
 
3
- Get OS-specific user directories (Downloads, Desktop, Documents, etc.) and XDG base directories (config, data, cache, state, log, runtime) with zero dependencies. Also provides `projectDirs()` for app-scoped directories (similar to `env-paths`).
3
+ [![npm version](https://img.shields.io/npm/v/os-user-dirs.svg)](https://www.npmjs.com/package/os-user-dirs)
4
+ [![npm downloads](https://img.shields.io/npm/dw/os-user-dirs.svg)](https://www.npmjs.com/package/os-user-dirs)
5
+ [![CI](https://github.com/velocitylabo/os-user-dirs/actions/workflows/ci.yml/badge.svg)](https://github.com/velocitylabo/os-user-dirs/actions/workflows/ci.yml)
6
+ [![Node.js](https://img.shields.io/node/v/os-user-dirs.svg)](https://nodejs.org)
7
+ [![license](https://img.shields.io/npm/l/os-user-dirs.svg)](https://github.com/velocitylabo/os-user-dirs/blob/master/LICENSE)
4
8
 
5
- > **Note:** This package was previously published as [`os-downloads`](https://www.npmjs.com/package/os-downloads). The old package is deprecatedplease use `os-user-dirs` instead.
9
+ > All-in-one OS directory paths user directories, XDG base directories, and app-scoped project directories with zero dependencies.
6
10
 
7
- ## Requirements
11
+ - Node.js 20 or later
8
12
 
9
- - Node.js 18 or later
13
+ Replaces [`env-paths`](https://github.com/sindresorhus/env-paths), [`xdg-basedir`](https://github.com/sindresorhus/xdg-basedir), and [`platform-folders`](https://github.com/nicbarker/platform-folders) in a single package. CJS + ESM dual support, TypeScript included.
10
14
 
11
- ## Supported platforms
15
+ > **Note:** This package was previously published as [`os-downloads`](https://www.npmjs.com/package/os-downloads). The old package is deprecated — please use `os-user-dirs` instead.
12
16
 
13
- - Windows
14
- - macOS
15
- - Linux (with XDG user-dirs.dirs support)
17
+ ## Why os-user-dirs?
18
+
19
+ | Feature | os-user-dirs | env-paths | xdg-basedir | platform-folders |
20
+ |---|:---:|:---:|:---:|:---:|
21
+ | Weekly downloads | Growing | ~30M | ~7M | ~642 |
22
+ | Last updated | **Active** | 4 years ago | 4 years ago | Low activity |
23
+ | Dependencies | **0** | 0 | 0 | C++ native |
24
+ | Platforms | **All** | All | Linux only | All |
25
+ | User directories (8) | **Yes** | - | - | Partial |
26
+ | XDG base directories (6) | **Yes** | Partial | **Yes** | Partial |
27
+ | XDG search paths | **Yes** | - | **Yes** | - |
28
+ | Project directories | **Yes** | **Yes** | - | - |
29
+ | CJS + ESM | **Both** | ESM only (v3) | ESM only (v5) | CJS |
30
+ | TypeScript | **Included** | Included | Included | - |
16
31
 
17
32
  ## Install
18
33
 
@@ -20,135 +35,35 @@ Get OS-specific user directories (Downloads, Desktop, Documents, etc.) and XDG b
20
35
  $ npm install os-user-dirs
21
36
  ```
22
37
 
23
- ## Usage
38
+ Requires Node.js 20 or later. Works on Windows, macOS, and Linux.
24
39
 
25
- ### ESM (recommended)
40
+ ## Quick Start
26
41
 
27
42
  ```javascript
28
- import { downloads, desktop, documents, music, pictures, videos, templates, publicshare, getPath, homeDir } from "os-user-dirs";
29
-
30
- homeDir();
31
- //=> '/home/user'
43
+ import { downloads, configDir, projectDirs, ensureDir } from "os-user-dirs";
32
44
 
45
+ // User directories
33
46
  downloads();
34
47
  //=> '/home/user/Downloads'
35
48
 
36
- desktop();
37
- //=> '/home/user/Desktop'
38
-
39
- templates();
40
- //=> '/home/user/Templates'
41
-
42
- publicshare();
43
- //=> '/home/user/Public'
44
-
45
- getPath("music");
46
- //=> '/home/user/Music'
47
- ```
48
-
49
- #### Base directories
50
-
51
- ```javascript
52
- import { configDir, dataDir, cacheDir, stateDir, logDir, runtimeDir, getBasePath } from "os-user-dirs";
53
-
49
+ // XDG base directories
54
50
  configDir();
55
51
  //=> '/home/user/.config'
56
52
 
57
- dataDir();
58
- //=> '/home/user/.local/share'
59
-
60
- cacheDir();
61
- //=> '/home/user/.cache'
62
-
63
- stateDir();
64
- //=> '/home/user/.local/state'
65
-
66
- logDir();
67
- //=> '/home/user/.local/state' (Linux), '~/Library/Logs' (macOS)
68
-
69
- runtimeDir();
70
- //=> '/run/user/1000' (or null)
71
-
72
- getBasePath("config");
73
- //=> '/home/user/.config'
74
- ```
75
-
76
- #### System search directories
77
-
78
- ```javascript
79
- import { configDirs, dataDirs } from "os-user-dirs";
80
-
81
- configDirs();
82
- //=> ['/etc/xdg'] (Linux)
83
- //=> ['/Library/Application Support', '/Library/Preferences'] (macOS)
84
- //=> ['C:\\ProgramData'] (Windows)
85
-
86
- dataDirs();
87
- //=> ['/usr/local/share', '/usr/share'] (Linux)
88
- //=> ['/Library/Application Support'] (macOS)
89
- //=> ['C:\\ProgramData'] (Windows)
90
- ```
91
-
92
- #### Project directories
93
-
94
- ```javascript
95
- import { projectDirs } from "os-user-dirs";
96
-
53
+ // App-scoped project directories (env-paths alternative)
97
54
  const dirs = projectDirs("my-app");
98
55
  dirs.config //=> '/home/user/.config/my-app'
99
56
  dirs.data //=> '/home/user/.local/share/my-app'
100
57
  dirs.cache //=> '/home/user/.cache/my-app'
101
- dirs.state //=> '/home/user/.local/state/my-app'
102
- dirs.log //=> '/home/user/.local/state/my-app'
103
- dirs.temp //=> '/tmp/my-app'
104
- dirs.runtime //=> '/run/user/1000/my-app' (or null)
105
-
106
- // With suffix option
107
- const dirs2 = projectDirs("my-app", { suffix: "-nodejs" });
108
- dirs2.config //=> '/home/user/.config/my-app-nodejs'
109
-
110
- // With vendor option
111
- const dirs3 = projectDirs("my-app", { vendor: "My Org" });
112
- dirs3.config //=> '/home/user/.config/my-org/my-app' (Linux)
113
- dirs3.config //=> '~/Library/Application Support/My Org/my-app' (macOS)
114
- ```
115
-
116
- #### Project user directories
117
-
118
- ```javascript
119
- import { projectUserDirs } from "os-user-dirs";
120
-
121
- const dirs = projectUserDirs("my-app");
122
- dirs.desktop //=> '/home/user/Desktop/my-app'
123
- dirs.downloads //=> '/home/user/Downloads/my-app'
124
- dirs.documents //=> '/home/user/Documents/my-app'
125
- dirs.music //=> '/home/user/Music/my-app'
126
- dirs.pictures //=> '/home/user/Pictures/my-app'
127
- dirs.videos //=> '/home/user/Videos/my-app'
128
- dirs.templates //=> '/home/user/Templates/my-app'
129
- dirs.publicshare //=> '/home/user/Public/my-app'
130
- ```
131
-
132
- ### CommonJS
133
-
134
- ```javascript
135
- const { downloads, desktop, documents, music, pictures, videos, templates, publicshare, getPath } = require("os-user-dirs");
136
58
 
137
- downloads();
138
- //=> '/home/user/Downloads'
59
+ // Ensure directory exists before use
60
+ await ensureDir(dirs.config);
139
61
  ```
140
62
 
141
- ### Default export (backward compatibility)
63
+ CommonJS is also supported:
142
64
 
143
65
  ```javascript
144
- // ESM
145
- import downloads from "os-user-dirs";
146
-
147
- // CommonJS
148
- const downloads = require("os-user-dirs");
149
-
150
- downloads();
151
- //=> '/home/user/Downloads'
66
+ const { downloads, configDir, projectDirs } = require("os-user-dirs");
152
67
  ```
153
68
 
154
69
  ### TypeScript
@@ -166,6 +81,8 @@ getPath("unknown"); // Type error
166
81
 
167
82
  ## API
168
83
 
84
+ > For detailed API documentation with platform-specific behavior, environment variables, and edge cases, see the **[API Reference](docs/api.md)**.
85
+
169
86
  ### `downloads()`
170
87
  Returns the path to the Downloads directory.
171
88
 
@@ -236,60 +153,119 @@ Returns the path to the user fonts directory (`~/.local/share/fonts` on Linux, `
236
153
 
237
154
  #### `getBasePath(name)`
238
155
  Returns the path to the specified base directory. Valid names: `config`, `data`, `cache`, `state`, `log`, `runtime`.
156
+ ## API Overview
157
+
158
+ ### User Directories
159
+
160
+ | Function | Description |
161
+ |---|---|
162
+ | `downloads()` | Downloads directory |
163
+ | `desktop()` | Desktop directory |
164
+ | `documents()` | Documents directory |
165
+ | `music()` | Music directory |
166
+ | `pictures()` | Pictures directory |
167
+ | `videos()` | Videos directory (Movies on macOS) |
168
+ | `templates()` | Templates directory |
169
+ | `publicshare()` | Public Share directory |
170
+ | `homeDir()` | Home directory |
171
+ | `getPath(name)` | Get user directory by name |
172
+
173
+ ### Base Directories (XDG)
174
+
175
+ | Function | Linux | macOS | Windows |
176
+ |---|---|---|---|
177
+ | `configDir()` | `~/.config` | `~/Library/Application Support` | `%APPDATA%` |
178
+ | `dataDir()` | `~/.local/share` | `~/Library/Application Support` | `%LOCALAPPDATA%` |
179
+ | `cacheDir()` | `~/.cache` | `~/Library/Caches` | `%LOCALAPPDATA%` |
180
+ | `stateDir()` | `~/.local/state` | `~/Library/Application Support` | `%LOCALAPPDATA%` |
181
+ | `logDir()` | `~/.local/state` | `~/Library/Logs` | `%LOCALAPPDATA%` |
182
+ | `runtimeDir()` | `$XDG_RUNTIME_DIR` | `null` | `null` |
183
+ | `getBasePath(name)` | Get base directory by name | | |
239
184
 
240
185
  ### System Search Directories
241
186
 
242
- #### `configDirs()`
243
- Returns the system config directory search path list (`string[]`). On Linux, reads `$XDG_CONFIG_DIRS` (default: `["/etc/xdg"]`). On macOS: `["/Library/Application Support", "/Library/Preferences"]`. On Windows: `[%PROGRAMDATA%]`.
187
+ | Function | Linux | macOS | Windows |
188
+ |---|---|---|---|
189
+ | `configDirs()` | `$XDG_CONFIG_DIRS` | `["/Library/Application Support", "/Library/Preferences"]` | `[%PROGRAMDATA%]` |
190
+ | `dataDirs()` | `$XDG_DATA_DIRS` | `["/Library/Application Support"]` | `[%PROGRAMDATA%]` |
191
+
192
+ ### Additional Directories
244
193
 
245
- #### `dataDirs()`
246
- Returns the system data directory search path list (`string[]`). On Linux, reads `$XDG_DATA_DIRS` (default: `["/usr/local/share", "/usr/share"]`). On macOS: `["/Library/Application Support"]`. On Windows: `[%PROGRAMDATA%]`.
194
+ | Function | Linux | macOS | Windows |
195
+ |---|---|---|---|
196
+ | `fontsDir()` | `~/.local/share/fonts` | `~/Library/Fonts` | `%LOCALAPPDATA%/Microsoft/Windows/Fonts` |
197
+ | `binDir()` | `~/.local/bin` | `~/.local/bin` | `null` |
198
+ | `applicationsDir()` | `~/.local/share/applications` | `~/Applications` | Start Menu |
199
+ | `trashDir()` | `~/.local/share/Trash` | `~/.Trash` | `null` |
247
200
 
248
201
  ### Project Directories
249
202
 
250
- #### `projectDirs(name, options?)`
251
- Returns an object with app-scoped directories for the given application name. This is a zero-dependency alternative to [`env-paths`](https://github.com/sindresorhus/env-paths).
203
+ ```javascript
204
+ import { projectDirs } from "os-user-dirs";
252
205
 
253
- **Parameters:**
254
- - `name` (string) — Application name
255
- - `options.suffix` (string, optional) Suffix appended to the app name (e.g. `"-nodejs"`)
256
- - `options.vendor` (string, optional) — Vendor/organization name used as a parent directory. On Linux, the vendor name is normalized to lowercase with spaces replaced by hyphens (e.g. `"My Org"` → `"my-org"`). On macOS and Windows, it is used as-is.
206
+ // Basic usage
207
+ const dirs = projectDirs("my-app");
208
+ // => { config, data, cache, state, log, temp, runtime }
257
209
 
258
- **Returns:** `{ config, data, cache, state, log, temp, runtime }`
210
+ // With vendor (organization) prefix
211
+ const dirs2 = projectDirs("my-app", { vendor: "My Org" });
212
+ dirs2.config //=> '~/.config/my-org/my-app' (Linux)
259
213
 
260
- On Windows, each directory uses a subdirectory structure (e.g. `%LOCALAPPDATA%/my-app/Config`, `%LOCALAPPDATA%/my-app/Data`).
214
+ // With suffix
215
+ const dirs3 = projectDirs("my-app", { suffix: "-nodejs" });
216
+ dirs3.config //=> '~/.config/my-app-nodejs'
217
+ ```
261
218
 
262
- #### `projectUserDirs(name)`
263
- Returns an object with project-scoped user directories. Each value is the corresponding user directory with the app name appended as a subdirectory.
219
+ ### Project User Directories
264
220
 
265
- **Parameters:**
266
- - `name` (string) Application name
221
+ ```javascript
222
+ import { projectUserDirs } from "os-user-dirs";
267
223
 
268
- **Returns:** `{ desktop, downloads, documents, music, pictures, videos, templates, publicshare }`
224
+ const dirs = projectUserDirs("my-app");
225
+ // => { desktop, downloads, documents, music, pictures, videos, templates, publicshare }
226
+ dirs.downloads //=> '/home/user/Downloads/my-app'
227
+ ```
269
228
 
270
229
  ### Utilities
271
230
 
272
- #### `ensureDirSync(dirPath)`
273
- Ensures the specified directory exists, creating it recursively if necessary. Returns the directory path.
231
+ | Function | Description |
232
+ |---|---|
233
+ | `ensureDirSync(dirPath)` | Creates directory recursively if needed (sync) |
234
+ | `ensureDir(dirPath)` | Creates directory recursively if needed (async) |
235
+ | `getXDGUserDir(key)` | Parse XDG user-dirs.dirs config |
274
236
 
275
- #### `ensureDir(dirPath)`
276
- Async version of `ensureDirSync`. Returns a promise that resolves with the directory path.
237
+ ## TypeScript
277
238
 
278
- ```javascript
279
- import { projectDirs, ensureDirSync, ensureDir } from "os-user-dirs";
239
+ Full type definitions are included. `getPath()` and `getBasePath()` accept union types for auto-completion:
280
240
 
281
- const dirs = projectDirs("my-app");
241
+ ```typescript
242
+ import { getPath, getBasePath } from "os-user-dirs";
282
243
 
283
- // Sync
284
- ensureDirSync(dirs.config);
244
+ getPath("downloads"); // OK — type: string
245
+ getPath("unknown"); // Type error
285
246
 
286
- // Async
287
- await ensureDir(dirs.data);
247
+ getBasePath("config"); // OK — type: string
248
+ getBasePath("unknown"); // Type error
288
249
  ```
289
250
 
290
- ## Migration from env-paths
251
+ ## Integration guides
252
+
253
+ - **[Electron Guide](docs/guide-electron.md)** — Using os-user-dirs in Electron apps: `app.getPath()` mapping, main/renderer process patterns, vendor-scoped directories
254
+ - **[CLI Tools Guide](docs/guide-cli-tools.md)** — Using `projectDirs()` with commander, yargs, and oclif for config, cache, and log management
255
+
256
+ ## Migration from xdg-basedir
257
+ ## Migration Guides
258
+
259
+ Switching from another library? We have you covered:
260
+
261
+ - **[From xdg-basedir](docs/migration-from-xdg-basedir.md)** — API mapping, code examples (v4 CJS and v5 ESM), cross-platform benefits
262
+ - **[From env-paths](docs/migration-from-env-paths.md)** — API mapping, code examples, additional features summary
263
+
264
+ ## Documentation
291
265
 
292
- Switching from [env-paths](https://github.com/sindresorhus/env-paths)? See the **[Migration Guide](docs/migration-from-env-paths.md)** for API mapping, code examples, and a summary of additional features.
266
+ - [API Reference](docs/api.md) Detailed documentation for all functions
267
+ - [Migration from xdg-basedir](docs/migration-from-xdg-basedir.md)
268
+ - [Migration from env-paths](docs/migration-from-env-paths.md)
293
269
 
294
270
  ## Migration from v2.x
295
271
 
package/index.d.ts CHANGED
@@ -1,59 +1,236 @@
1
1
  type DirName = "desktop" | "downloads" | "documents" | "music" | "pictures" | "videos" | "templates" | "publicshare";
2
2
 
3
- /** Returns the path to the Desktop directory. */
3
+ /**
4
+ * Returns the path to the Desktop directory.
5
+ * @example
6
+ * ```js
7
+ * const { desktop } = require('os-user-dirs');
8
+ * console.log(desktop());
9
+ * // Linux: '/home/user/Desktop'
10
+ * // macOS: '/Users/user/Desktop'
11
+ * // Windows: 'C:\Users\user\Desktop'
12
+ * ```
13
+ */
4
14
  export function desktop(): string;
5
15
 
6
- /** Returns the path to the Downloads directory. */
16
+ /**
17
+ * Returns the path to the Downloads directory.
18
+ * @example
19
+ * ```js
20
+ * const { downloads } = require('os-user-dirs');
21
+ * console.log(downloads());
22
+ * // Linux: '/home/user/Downloads'
23
+ * // macOS: '/Users/user/Downloads'
24
+ * // Windows: 'C:\Users\user\Downloads'
25
+ * ```
26
+ */
7
27
  export function downloads(): string;
8
28
 
9
- /** Returns the path to the Documents directory. */
29
+ /**
30
+ * Returns the path to the Documents directory.
31
+ * @example
32
+ * ```js
33
+ * const { documents } = require('os-user-dirs');
34
+ * console.log(documents());
35
+ * // Linux: '/home/user/Documents'
36
+ * // macOS: '/Users/user/Documents'
37
+ * // Windows: 'C:\Users\user\Documents'
38
+ * ```
39
+ */
10
40
  export function documents(): string;
11
41
 
12
- /** Returns the path to the Music directory. */
42
+ /**
43
+ * Returns the path to the Music directory.
44
+ * @example
45
+ * ```js
46
+ * const { music } = require('os-user-dirs');
47
+ * console.log(music());
48
+ * // Linux: '/home/user/Music'
49
+ * // macOS: '/Users/user/Music'
50
+ * // Windows: 'C:\Users\user\Music'
51
+ * ```
52
+ */
13
53
  export function music(): string;
14
54
 
15
- /** Returns the path to the Pictures directory. */
55
+ /**
56
+ * Returns the path to the Pictures directory.
57
+ * @example
58
+ * ```js
59
+ * const { pictures } = require('os-user-dirs');
60
+ * console.log(pictures());
61
+ * // Linux: '/home/user/Pictures'
62
+ * // macOS: '/Users/user/Pictures'
63
+ * // Windows: 'C:\Users\user\Pictures'
64
+ * ```
65
+ */
16
66
  export function pictures(): string;
17
67
 
18
- /** Returns the path to the Videos directory (Movies on macOS). */
68
+ /**
69
+ * Returns the path to the Videos directory (Movies on macOS).
70
+ * @example
71
+ * ```js
72
+ * const { videos } = require('os-user-dirs');
73
+ * console.log(videos());
74
+ * // Linux: '/home/user/Videos'
75
+ * // macOS: '/Users/user/Movies'
76
+ * // Windows: 'C:\Users\user\Videos'
77
+ * ```
78
+ */
19
79
  export function videos(): string;
20
80
 
21
- /** Returns the path to the Templates directory. */
81
+ /**
82
+ * Returns the path to the Templates directory.
83
+ * @example
84
+ * ```js
85
+ * const { templates } = require('os-user-dirs');
86
+ * console.log(templates());
87
+ * // Linux: '/home/user/Templates'
88
+ * // macOS: '/Users/user/Templates'
89
+ * // Windows: 'C:\Users\user\Templates'
90
+ * ```
91
+ */
22
92
  export function templates(): string;
23
93
 
24
- /** Returns the path to the Public Share directory. */
94
+ /**
95
+ * Returns the path to the Public Share directory.
96
+ * @example
97
+ * ```js
98
+ * const { publicshare } = require('os-user-dirs');
99
+ * console.log(publicshare());
100
+ * // Linux: '/home/user/Public'
101
+ * // macOS: '/Users/user/Public'
102
+ * // Windows: 'C:\Users\user\Public'
103
+ * ```
104
+ */
25
105
  export function publicshare(): string;
26
106
 
27
- /** Returns the path to the specified user directory. */
107
+ /**
108
+ * Returns the path to the specified user directory.
109
+ * @example
110
+ * ```js
111
+ * const { getPath } = require('os-user-dirs');
112
+ * console.log(getPath('downloads')); // '/home/user/Downloads'
113
+ * console.log(getPath('desktop')); // '/home/user/Desktop'
114
+ * ```
115
+ */
28
116
  export function getPath(name: DirName): string;
29
117
 
30
- /** Returns the path to the user's home directory. */
118
+ /**
119
+ * Returns the path to the user's home directory.
120
+ * @example
121
+ * ```js
122
+ * const { homeDir } = require('os-user-dirs');
123
+ * console.log(homeDir());
124
+ * // Linux: '/home/user'
125
+ * // macOS: '/Users/user'
126
+ * // Windows: 'C:\Users\user'
127
+ * ```
128
+ */
31
129
  export function homeDir(): string;
32
130
 
33
- /** Returns the path to the user local bin directory (~/.local/bin), or null on Windows. */
131
+ /**
132
+ * Returns the path to the user local bin directory (~/.local/bin), or null on Windows.
133
+ * @example
134
+ * ```js
135
+ * const { binDir } = require('os-user-dirs');
136
+ * console.log(binDir());
137
+ * // Linux/macOS: '/home/user/.local/bin'
138
+ * // Windows: null
139
+ * ```
140
+ */
34
141
  export function binDir(): string | null;
35
142
 
36
143
  type BaseDirName = "config" | "data" | "cache" | "state" | "log" | "runtime";
37
144
 
38
- /** Returns the path to the XDG config directory. */
145
+ /**
146
+ * Returns the path to the XDG config directory.
147
+ * @example
148
+ * ```js
149
+ * const { configDir } = require('os-user-dirs');
150
+ * console.log(configDir());
151
+ * // Linux: '/home/user/.config'
152
+ * // macOS: '/Users/user/Library/Application Support'
153
+ * // Windows: 'C:\Users\user\AppData\Roaming'
154
+ * ```
155
+ */
39
156
  export function configDir(): string;
40
157
 
41
- /** Returns the path to the XDG data directory. */
158
+ /**
159
+ * Returns the path to the XDG data directory.
160
+ * @example
161
+ * ```js
162
+ * const { dataDir } = require('os-user-dirs');
163
+ * console.log(dataDir());
164
+ * // Linux: '/home/user/.local/share'
165
+ * // macOS: '/Users/user/Library/Application Support'
166
+ * // Windows: 'C:\Users\user\AppData\Local'
167
+ * ```
168
+ */
42
169
  export function dataDir(): string;
43
170
 
44
- /** Returns the path to the XDG cache directory. */
171
+ /**
172
+ * Returns the path to the XDG cache directory.
173
+ * @example
174
+ * ```js
175
+ * const { cacheDir } = require('os-user-dirs');
176
+ * console.log(cacheDir());
177
+ * // Linux: '/home/user/.cache'
178
+ * // macOS: '/Users/user/Library/Caches'
179
+ * // Windows: 'C:\Users\user\AppData\Local'
180
+ * ```
181
+ */
45
182
  export function cacheDir(): string;
46
183
 
47
- /** Returns the path to the XDG state directory. */
184
+ /**
185
+ * Returns the path to the XDG state directory.
186
+ * @example
187
+ * ```js
188
+ * const { stateDir } = require('os-user-dirs');
189
+ * console.log(stateDir());
190
+ * // Linux: '/home/user/.local/state'
191
+ * // macOS: '/Users/user/Library/Application Support'
192
+ * // Windows: 'C:\Users\user\AppData\Local'
193
+ * ```
194
+ */
48
195
  export function stateDir(): string;
49
196
 
50
- /** Returns the path to the log directory. */
197
+ /**
198
+ * Returns the path to the log directory.
199
+ * @example
200
+ * ```js
201
+ * const { logDir } = require('os-user-dirs');
202
+ * console.log(logDir());
203
+ * // Linux: '/home/user/.local/state'
204
+ * // macOS: '/Users/user/Library/Logs'
205
+ * // Windows: 'C:\Users\user\AppData\Local'
206
+ * ```
207
+ */
51
208
  export function logDir(): string;
52
209
 
53
- /** Returns the path to the XDG runtime directory, or null if unavailable. */
210
+ /**
211
+ * Returns the path to the XDG runtime directory, or null if unavailable.
212
+ * @example
213
+ * ```js
214
+ * const { runtimeDir } = require('os-user-dirs');
215
+ * console.log(runtimeDir());
216
+ * // Linux: '/run/user/1000' (if $XDG_RUNTIME_DIR is set)
217
+ * // macOS: null
218
+ * // Windows: null
219
+ * ```
220
+ */
54
221
  export function runtimeDir(): string | null;
55
222
 
56
- /** Returns the path to the user fonts directory. */
223
+ /**
224
+ * Returns the path to the user fonts directory.
225
+ * @example
226
+ * ```js
227
+ * const { fontsDir } = require('os-user-dirs');
228
+ * console.log(fontsDir());
229
+ * // Linux: '/home/user/.local/share/fonts'
230
+ * // macOS: '/Users/user/Library/Fonts'
231
+ * // Windows: 'C:\Users\user\AppData\Local\Microsoft\Windows\Fonts'
232
+ * ```
233
+ */
57
234
  export function fontsDir(): string;
58
235
 
59
236
  /**
@@ -61,6 +238,14 @@ export function fontsDir(): string;
61
238
  * On Linux, reads `$XDG_CONFIG_DIRS` (default: `["/etc/xdg"]`).
62
239
  * On macOS: `["/Library/Application Support", "/Library/Preferences"]`.
63
240
  * On Windows: `[%PROGRAMDATA%]`.
241
+ * @example
242
+ * ```js
243
+ * const { configDirs } = require('os-user-dirs');
244
+ * console.log(configDirs());
245
+ * // Linux: ['/etc/xdg']
246
+ * // macOS: ['/Library/Application Support', '/Library/Preferences']
247
+ * // Windows: ['C:\ProgramData']
248
+ * ```
64
249
  */
65
250
  export function configDirs(): string[];
66
251
 
@@ -69,10 +254,27 @@ export function configDirs(): string[];
69
254
  * On Linux, reads `$XDG_DATA_DIRS` (default: `["/usr/local/share", "/usr/share"]`).
70
255
  * On macOS: `["/Library/Application Support"]`.
71
256
  * On Windows: `[%PROGRAMDATA%]`.
257
+ * @example
258
+ * ```js
259
+ * const { dataDirs } = require('os-user-dirs');
260
+ * console.log(dataDirs());
261
+ * // Linux: ['/usr/local/share', '/usr/share']
262
+ * // macOS: ['/Library/Application Support']
263
+ * // Windows: ['C:\ProgramData']
264
+ * ```
72
265
  */
73
266
  export function dataDirs(): string[];
74
267
 
75
- /** Returns the path to the specified base directory. */
268
+ /**
269
+ * Returns the path to the specified base directory.
270
+ * @example
271
+ * ```js
272
+ * const { getBasePath } = require('os-user-dirs');
273
+ * console.log(getBasePath('config')); // '/home/user/.config'
274
+ * console.log(getBasePath('data')); // '/home/user/.local/share'
275
+ * console.log(getBasePath('runtime')); // '/run/user/1000' or null
276
+ * ```
277
+ */
76
278
  export function getBasePath(name: "config"): string;
77
279
  export function getBasePath(name: "data"): string;
78
280
  export function getBasePath(name: "cache"): string;
@@ -102,6 +304,23 @@ interface ProjectDirsResult {
102
304
  * Returns application-scoped directories for the given app name.
103
305
  * @param name - Application name used to derive directory paths
104
306
  * @param options - Optional settings (e.g. suffix)
307
+ * @example
308
+ * ```js
309
+ * const { projectDirs } = require('os-user-dirs');
310
+ * const dirs = projectDirs('my-app');
311
+ * console.log(dirs.config); // Linux: '/home/user/.config/my-app'
312
+ * console.log(dirs.data); // Linux: '/home/user/.local/share/my-app'
313
+ * console.log(dirs.cache); // Linux: '/home/user/.cache/my-app'
314
+ * ```
315
+ * @example
316
+ * ```js
317
+ * // With vendor option
318
+ * const dirs = projectDirs('my-app', { vendor: 'My Org' });
319
+ * console.log(dirs.config);
320
+ * // Linux: '/home/user/.config/my-org/my-app'
321
+ * // macOS: '/Users/user/Library/Application Support/My Org/my-app'
322
+ * // Windows: 'C:\Users\user\AppData\Local\My Org\my-app\Config'
323
+ * ```
105
324
  */
106
325
  export function projectDirs(name: string, options?: ProjectDirsOptions): ProjectDirsResult;
107
326
 
@@ -120,6 +339,14 @@ interface ProjectUserDirsResult {
120
339
  * Returns project-scoped user directories for the given app name.
121
340
  * Each value is the user directory path with the app name appended as a subdirectory.
122
341
  * @param name - Application name used to derive directory paths
342
+ * @example
343
+ * ```js
344
+ * const { projectUserDirs } = require('os-user-dirs');
345
+ * const dirs = projectUserDirs('my-app');
346
+ * console.log(dirs.downloads); // '/home/user/Downloads/my-app'
347
+ * console.log(dirs.documents); // '/home/user/Documents/my-app'
348
+ * console.log(dirs.desktop); // '/home/user/Desktop/my-app'
349
+ * ```
123
350
  */
124
351
  export function projectUserDirs(name: string): ProjectUserDirsResult;
125
352
 
@@ -128,6 +355,14 @@ export function projectUserDirs(name: string): ProjectUserDirsResult;
128
355
  * Linux: `$XDG_DATA_HOME/Trash` (default `~/.local/share/Trash`)
129
356
  * macOS: `~/.Trash`
130
357
  * Windows: `null` (Recycle Bin requires Shell API)
358
+ * @example
359
+ * ```js
360
+ * const { trashDir } = require('os-user-dirs');
361
+ * console.log(trashDir());
362
+ * // Linux: '/home/user/.local/share/Trash'
363
+ * // macOS: '/Users/user/.Trash'
364
+ * // Windows: null
365
+ * ```
131
366
  */
132
367
  export function trashDir(): string | null;
133
368
 
@@ -136,6 +371,14 @@ export function trashDir(): string | null;
136
371
  * Linux: `$XDG_DATA_HOME/applications` (default `~/.local/share/applications`)
137
372
  * macOS: `~/Applications`
138
373
  * Windows: `%APPDATA%/Microsoft/Windows/Start Menu/Programs`
374
+ * @example
375
+ * ```js
376
+ * const { applicationsDir } = require('os-user-dirs');
377
+ * console.log(applicationsDir());
378
+ * // Linux: '/home/user/.local/share/applications'
379
+ * // macOS: '/Users/user/Applications'
380
+ * // Windows: 'C:\Users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs'
381
+ * ```
139
382
  */
140
383
  export function applicationsDir(): string;
141
384
 
@@ -144,6 +387,12 @@ export function applicationsDir(): string;
144
387
  * @param key - XDG key (e.g. "XDG_DOWNLOAD_DIR")
145
388
  * @param configPath - Optional path to user-dirs.dirs config file
146
389
  * @returns The resolved directory path, or null if not found
390
+ * @example
391
+ * ```js
392
+ * const { getXDGUserDir } = require('os-user-dirs');
393
+ * console.log(getXDGUserDir('XDG_DOWNLOAD_DIR')); // '/home/user/Downloads'
394
+ * console.log(getXDGUserDir('XDG_DESKTOP_DIR')); // '/home/user/Desktop'
395
+ * ```
147
396
  */
148
397
  export function getXDGUserDir(key: string, configPath?: string): string | null;
149
398
 
@@ -151,6 +400,12 @@ export function getXDGUserDir(key: string, configPath?: string): string | null;
151
400
  * Ensures the specified directory exists, creating it recursively if necessary.
152
401
  * @param dirPath - The directory path to ensure exists
153
402
  * @returns The directory path
403
+ * @example
404
+ * ```js
405
+ * const { ensureDirSync } = require('os-user-dirs');
406
+ * const dir = ensureDirSync('/tmp/my-app/data');
407
+ * console.log(dir); // '/tmp/my-app/data'
408
+ * ```
154
409
  */
155
410
  export function ensureDirSync(dirPath: string): string;
156
411
 
@@ -158,6 +413,12 @@ export function ensureDirSync(dirPath: string): string;
158
413
  * Ensures the specified directory exists, creating it recursively if necessary (async version).
159
414
  * @param dirPath - The directory path to ensure exists
160
415
  * @returns A promise that resolves with the directory path
416
+ * @example
417
+ * ```js
418
+ * const { ensureDir } = require('os-user-dirs');
419
+ * const dir = await ensureDir('/tmp/my-app/data');
420
+ * console.log(dir); // '/tmp/my-app/data'
421
+ * ```
161
422
  */
162
423
  export function ensureDir(dirPath: string): Promise<string>;
163
424
 
package/index.js CHANGED
@@ -152,11 +152,11 @@ function logDir() { return resolveBase("log"); }
152
152
  function runtimeDir() { return resolveBase("runtime"); }
153
153
 
154
154
  function fontsDir() {
155
- var platform = process.platform;
156
- var homedir = os.homedir();
155
+ const platform = process.platform;
156
+ const homedir = os.homedir();
157
157
 
158
158
  if (platform === "linux") {
159
- var envVal = process.env.XDG_DATA_HOME;
159
+ const envVal = process.env.XDG_DATA_HOME;
160
160
  if (envVal) {
161
161
  return path.join(path.resolve(envVal), "fonts");
162
162
  }
@@ -168,7 +168,7 @@ function fontsDir() {
168
168
  }
169
169
 
170
170
  if (platform === "win32") {
171
- var localAppData = process.env.LOCALAPPDATA || path.join(homedir, "AppData", "Local");
171
+ const localAppData = process.env.LOCALAPPDATA || path.join(homedir, "AppData", "Local");
172
172
  return path.join(localAppData, "Microsoft", "Windows", "Fonts");
173
173
  }
174
174
 
@@ -262,7 +262,7 @@ function projectDirs(name, options) {
262
262
  const vendorDir = vendor ? normalizeVendor(vendor, platform) : "";
263
263
 
264
264
  function joinWithVendor() {
265
- var segments = Array.prototype.slice.call(arguments);
265
+ const segments = Array.prototype.slice.call(arguments);
266
266
  if (vendorDir) {
267
267
  segments.splice(1, 0, vendorDir);
268
268
  }
@@ -315,21 +315,21 @@ function projectUserDirs(name) {
315
315
  throw new Error("projectUserDirs requires a non-empty string name");
316
316
  }
317
317
 
318
- var result = {};
319
- var keys = Object.keys(XDG_KEYS);
320
- for (var i = 0; i < keys.length; i++) {
318
+ const result = {};
319
+ const keys = Object.keys(XDG_KEYS);
320
+ for (let i = 0; i < keys.length; i++) {
321
321
  result[keys[i]] = path.join(resolve(keys[i]), name);
322
322
  }
323
323
  return result;
324
324
  }
325
325
 
326
326
  function trashDir() {
327
- var platform = process.platform;
328
- var homedir = os.homedir();
327
+ const platform = process.platform;
328
+ const homedir = os.homedir();
329
329
 
330
330
  if (platform === "linux") {
331
- var envVal = process.env.XDG_DATA_HOME;
332
- var base = envVal ? path.resolve(envVal) : path.join(homedir, ".local", "share");
331
+ const envVal = process.env.XDG_DATA_HOME;
332
+ const base = envVal ? path.resolve(envVal) : path.join(homedir, ".local", "share");
333
333
  return path.join(base, "Trash");
334
334
  }
335
335
 
@@ -342,18 +342,18 @@ function trashDir() {
342
342
  }
343
343
 
344
344
  // Unknown platform: use XDG-style default
345
- var envVal = process.env.XDG_DATA_HOME;
346
- var base = envVal ? path.resolve(envVal) : path.join(homedir, ".local", "share");
345
+ const envVal = process.env.XDG_DATA_HOME;
346
+ const base = envVal ? path.resolve(envVal) : path.join(homedir, ".local", "share");
347
347
  return path.join(base, "Trash");
348
348
  }
349
349
 
350
350
  function applicationsDir() {
351
- var homedir = os.homedir();
352
- var platform = process.platform;
351
+ const homedir = os.homedir();
352
+ const platform = process.platform;
353
353
 
354
354
  if (platform === "linux") {
355
- var envVal = process.env.XDG_DATA_HOME;
356
- var base = envVal ? path.resolve(envVal) : path.join(homedir, ".local", "share");
355
+ const envVal = process.env.XDG_DATA_HOME;
356
+ const base = envVal ? path.resolve(envVal) : path.join(homedir, ".local", "share");
357
357
  return path.join(base, "applications");
358
358
  }
359
359
 
@@ -362,7 +362,7 @@ function applicationsDir() {
362
362
  }
363
363
 
364
364
  if (platform === "win32") {
365
- var appdata = process.env.APPDATA || path.join(homedir, "AppData", "Roaming");
365
+ const appdata = process.env.APPDATA || path.join(homedir, "AppData", "Roaming");
366
366
  return path.join(appdata, "Microsoft", "Windows", "Start Menu", "Programs");
367
367
  }
368
368
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "os-user-dirs",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "Get OS-specific user directories and XDG base directories (config, data, cache, runtime) with zero dependencies.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -17,7 +17,8 @@
17
17
  "index.d.ts"
18
18
  ],
19
19
  "scripts": {
20
- "test": "mocha test.js"
20
+ "test": "mocha test.js",
21
+ "coverage": "c8 --all mocha test.js"
21
22
  },
22
23
  "repository": {
23
24
  "type": "git",
@@ -39,7 +40,27 @@
39
40
  "cache",
40
41
  "xdg",
41
42
  "user-dirs",
42
- "platform-folders"
43
+ "platform-folders",
44
+ "env-paths",
45
+ "xdg-basedir",
46
+ "appdata-path",
47
+ "xdg-base-directory",
48
+ "user-directories",
49
+ "app-paths",
50
+ "config-dir",
51
+ "data-dir",
52
+ "cache-dir",
53
+ "downloads-dir",
54
+ "desktop-dir",
55
+ "cross-platform",
56
+ "linux",
57
+ "macos",
58
+ "windows",
59
+ "zero-dependencies",
60
+ "cjs",
61
+ "esm",
62
+ "dual-module",
63
+ "typescript"
43
64
  ],
44
65
  "author": "piroz",
45
66
  "license": "MIT",
@@ -47,10 +68,29 @@
47
68
  "url": "https://github.com/velocitylabo/os-user-dirs/issues"
48
69
  },
49
70
  "engines": {
50
- "node": ">=18"
71
+ "node": ">=20"
51
72
  },
52
73
  "homepage": "https://github.com/velocitylabo/os-user-dirs#readme",
74
+ "c8": {
75
+ "all": true,
76
+ "src": [
77
+ "."
78
+ ],
79
+ "include": [
80
+ "index.js"
81
+ ],
82
+ "reporter": [
83
+ "text",
84
+ "lcov"
85
+ ],
86
+ "check-coverage": true,
87
+ "branches": 80,
88
+ "lines": 75,
89
+ "functions": 100,
90
+ "statements": 75
91
+ },
53
92
  "devDependencies": {
93
+ "c8": "^11.0.0",
54
94
  "mocha": "^11.7.5"
55
95
  }
56
96
  }