os-user-dirs 2.7.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 (5) hide show
  1. package/README.md +166 -131
  2. package/index.d.ts +305 -21
  3. package/index.js +65 -21
  4. package/index.mjs +4 -1
  5. package/package.json +46 -3
package/README.md CHANGED
@@ -1,14 +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.
10
+
11
+ - Node.js 20 or later
12
+
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.
6
14
 
7
- ## 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.
8
16
 
9
- - Windows
10
- - macOS
11
- - 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 | - |
12
31
 
13
32
  ## Install
14
33
 
@@ -16,132 +35,35 @@ Get OS-specific user directories (Downloads, Desktop, Documents, etc.) and XDG b
16
35
  $ npm install os-user-dirs
17
36
  ```
18
37
 
19
- ## Usage
38
+ Requires Node.js 20 or later. Works on Windows, macOS, and Linux.
20
39
 
21
- ### ESM (recommended)
40
+ ## Quick Start
22
41
 
23
42
  ```javascript
24
- import { downloads, desktop, documents, music, pictures, videos, templates, publicshare, getPath } from "os-user-dirs";
43
+ import { downloads, configDir, projectDirs, ensureDir } from "os-user-dirs";
25
44
 
45
+ // User directories
26
46
  downloads();
27
47
  //=> '/home/user/Downloads'
28
48
 
29
- desktop();
30
- //=> '/home/user/Desktop'
31
-
32
- templates();
33
- //=> '/home/user/Templates'
34
-
35
- publicshare();
36
- //=> '/home/user/Public'
37
-
38
- getPath("music");
39
- //=> '/home/user/Music'
40
- ```
41
-
42
- #### Base directories
43
-
44
- ```javascript
45
- import { configDir, dataDir, cacheDir, stateDir, logDir, runtimeDir, getBasePath } from "os-user-dirs";
46
-
49
+ // XDG base directories
47
50
  configDir();
48
51
  //=> '/home/user/.config'
49
52
 
50
- dataDir();
51
- //=> '/home/user/.local/share'
52
-
53
- cacheDir();
54
- //=> '/home/user/.cache'
55
-
56
- stateDir();
57
- //=> '/home/user/.local/state'
58
-
59
- logDir();
60
- //=> '/home/user/.local/state' (Linux), '~/Library/Logs' (macOS)
61
-
62
- runtimeDir();
63
- //=> '/run/user/1000' (or null)
64
-
65
- getBasePath("config");
66
- //=> '/home/user/.config'
67
- ```
68
-
69
- #### System search directories
70
-
71
- ```javascript
72
- import { configDirs, dataDirs } from "os-user-dirs";
73
-
74
- configDirs();
75
- //=> ['/etc/xdg'] (Linux)
76
- //=> ['/Library/Application Support', '/Library/Preferences'] (macOS)
77
- //=> ['C:\\ProgramData'] (Windows)
78
-
79
- dataDirs();
80
- //=> ['/usr/local/share', '/usr/share'] (Linux)
81
- //=> ['/Library/Application Support'] (macOS)
82
- //=> ['C:\\ProgramData'] (Windows)
83
- ```
84
-
85
- #### Project directories
86
-
87
- ```javascript
88
- import { projectDirs } from "os-user-dirs";
89
-
53
+ // App-scoped project directories (env-paths alternative)
90
54
  const dirs = projectDirs("my-app");
91
55
  dirs.config //=> '/home/user/.config/my-app'
92
56
  dirs.data //=> '/home/user/.local/share/my-app'
93
57
  dirs.cache //=> '/home/user/.cache/my-app'
94
- dirs.state //=> '/home/user/.local/state/my-app'
95
- dirs.log //=> '/home/user/.local/state/my-app'
96
- dirs.temp //=> '/tmp/my-app'
97
- dirs.runtime //=> '/run/user/1000/my-app' (or null)
98
-
99
- // With suffix option
100
- const dirs2 = projectDirs("my-app", { suffix: "-nodejs" });
101
- dirs2.config //=> '/home/user/.config/my-app-nodejs'
102
-
103
- // With vendor option
104
- const dirs3 = projectDirs("my-app", { vendor: "My Org" });
105
- dirs3.config //=> '/home/user/.config/my-org/my-app' (Linux)
106
- dirs3.config //=> '~/Library/Application Support/My Org/my-app' (macOS)
107
- ```
108
-
109
- #### Project user directories
110
58
 
111
- ```javascript
112
- import { projectUserDirs } from "os-user-dirs";
113
-
114
- const dirs = projectUserDirs("my-app");
115
- dirs.desktop //=> '/home/user/Desktop/my-app'
116
- dirs.downloads //=> '/home/user/Downloads/my-app'
117
- dirs.documents //=> '/home/user/Documents/my-app'
118
- dirs.music //=> '/home/user/Music/my-app'
119
- dirs.pictures //=> '/home/user/Pictures/my-app'
120
- dirs.videos //=> '/home/user/Videos/my-app'
121
- dirs.templates //=> '/home/user/Templates/my-app'
122
- dirs.publicshare //=> '/home/user/Public/my-app'
123
- ```
124
-
125
- ### CommonJS
126
-
127
- ```javascript
128
- const { downloads, desktop, documents, music, pictures, videos, templates, publicshare, getPath } = require("os-user-dirs");
129
-
130
- downloads();
131
- //=> '/home/user/Downloads'
59
+ // Ensure directory exists before use
60
+ await ensureDir(dirs.config);
132
61
  ```
133
62
 
134
- ### Default export (backward compatibility)
63
+ CommonJS is also supported:
135
64
 
136
65
  ```javascript
137
- // ESM
138
- import downloads from "os-user-dirs";
139
-
140
- // CommonJS
141
- const downloads = require("os-user-dirs");
142
-
143
- downloads();
144
- //=> '/home/user/Downloads'
66
+ const { downloads, configDir, projectDirs } = require("os-user-dirs");
145
67
  ```
146
68
 
147
69
  ### TypeScript
@@ -159,6 +81,8 @@ getPath("unknown"); // Type error
159
81
 
160
82
  ## API
161
83
 
84
+ > For detailed API documentation with platform-specific behavior, environment variables, and edge cases, see the **[API Reference](docs/api.md)**.
85
+
162
86
  ### `downloads()`
163
87
  Returns the path to the Downloads directory.
164
88
 
@@ -186,9 +110,18 @@ Returns the path to the Public Share directory.
186
110
  ### `getPath(name)`
187
111
  Returns the path to the specified user directory. Valid names: `desktop`, `downloads`, `documents`, `music`, `pictures`, `videos`, `templates`, `publicshare`.
188
112
 
113
+ ### `homeDir()`
114
+ Returns the path to the user's home directory. Uses `os.homedir()` internally.
115
+
189
116
  ### `binDir()`
190
117
  Returns the path to the user local bin directory (`~/.local/bin` on Linux/macOS), or `null` on Windows.
191
118
 
119
+ ### `trashDir()`
120
+ Returns the path to the user trash directory, or `null` on Windows.
121
+ - Linux: `$XDG_DATA_HOME/Trash` (default `~/.local/share/Trash`) — FreeDesktop Trash spec
122
+ - macOS: `~/.Trash`
123
+ - Windows: `null` (Recycle Bin requires Shell API)
124
+
192
125
  ### `applicationsDir()`
193
126
  Returns the path to the user applications directory.
194
127
  - Linux: `$XDG_DATA_HOME/applications` (default `~/.local/share/applications`)
@@ -220,36 +153,138 @@ Returns the path to the user fonts directory (`~/.local/share/fonts` on Linux, `
220
153
 
221
154
  #### `getBasePath(name)`
222
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 | | |
223
184
 
224
185
  ### System Search Directories
225
186
 
226
- #### `configDirs()`
227
- 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
228
193
 
229
- #### `dataDirs()`
230
- 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` |
231
200
 
232
201
  ### Project Directories
233
202
 
234
- #### `projectDirs(name, options?)`
235
- 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";
205
+
206
+ // Basic usage
207
+ const dirs = projectDirs("my-app");
208
+ // => { config, data, cache, state, log, temp, runtime }
209
+
210
+ // With vendor (organization) prefix
211
+ const dirs2 = projectDirs("my-app", { vendor: "My Org" });
212
+ dirs2.config //=> '~/.config/my-org/my-app' (Linux)
213
+
214
+ // With suffix
215
+ const dirs3 = projectDirs("my-app", { suffix: "-nodejs" });
216
+ dirs3.config //=> '~/.config/my-app-nodejs'
217
+ ```
218
+
219
+ ### Project User Directories
220
+
221
+ ```javascript
222
+ import { projectUserDirs } from "os-user-dirs";
223
+
224
+ const dirs = projectUserDirs("my-app");
225
+ // => { desktop, downloads, documents, music, pictures, videos, templates, publicshare }
226
+ dirs.downloads //=> '/home/user/Downloads/my-app'
227
+ ```
228
+
229
+ ### Utilities
230
+
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 |
236
+
237
+ ## TypeScript
238
+
239
+ Full type definitions are included. `getPath()` and `getBasePath()` accept union types for auto-completion:
240
+
241
+ ```typescript
242
+ import { getPath, getBasePath } from "os-user-dirs";
243
+
244
+ getPath("downloads"); // OK — type: string
245
+ getPath("unknown"); // Type error
246
+
247
+ getBasePath("config"); // OK — type: string
248
+ getBasePath("unknown"); // Type error
249
+ ```
236
250
 
237
- **Parameters:**
238
- - `name` (string) — Application name
239
- - `options.suffix` (string, optional) — Suffix appended to the app name (e.g. `"-nodejs"`)
240
- - `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.
251
+ ## Integration guides
241
252
 
242
- **Returns:** `{ config, data, cache, state, log, temp, runtime }`
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
243
255
 
244
- On Windows, each directory uses a subdirectory structure (e.g. `%LOCALAPPDATA%/my-app/Config`, `%LOCALAPPDATA%/my-app/Data`).
256
+ ## Migration from xdg-basedir
257
+ ## Migration Guides
245
258
 
246
- #### `projectUserDirs(name)`
247
- Returns an object with project-scoped user directories. Each value is the corresponding user directory with the app name appended as a subdirectory.
259
+ Switching from another library? We have you covered:
248
260
 
249
- **Parameters:**
250
- - `name` (string) — Application name
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
251
263
 
252
- **Returns:** `{ desktop, downloads, documents, music, pictures, videos, templates, publicshare }`
264
+ ## Documentation
265
+
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)
269
+
270
+ ## Migration from v2.x
271
+
272
+ ### `getXDGDownloadDir()` removed
273
+
274
+ `getXDGDownloadDir()` was deprecated in v2.3.0 and has been removed in v3.0.0.
275
+
276
+ **Before (v2.x):**
277
+ ```javascript
278
+ const { getXDGDownloadDir } = require("os-user-dirs");
279
+ getXDGDownloadDir(); // reads XDG user-dirs.dirs for download path
280
+ ```
281
+
282
+ **After (v3.x):**
283
+ ```javascript
284
+ const { downloads, getXDGUserDir } = require("os-user-dirs");
285
+ downloads(); // recommended: cross-platform Downloads path
286
+ getXDGUserDir("XDG_DOWNLOAD_DIR"); // direct replacement if you need XDG config parsing
287
+ ```
253
288
 
254
289
  ## License
255
290
 
package/index.d.ts CHANGED
@@ -1,56 +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 local bin directory (~/.local/bin), or null on Windows. */
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
+ */
129
+ export function homeDir(): string;
130
+
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
+ */
31
141
  export function binDir(): string | null;
32
142
 
33
143
  type BaseDirName = "config" | "data" | "cache" | "state" | "log" | "runtime";
34
144
 
35
- /** 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
+ */
36
156
  export function configDir(): string;
37
157
 
38
- /** 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
+ */
39
169
  export function dataDir(): string;
40
170
 
41
- /** 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
+ */
42
182
  export function cacheDir(): string;
43
183
 
44
- /** 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
+ */
45
195
  export function stateDir(): string;
46
196
 
47
- /** 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
+ */
48
208
  export function logDir(): string;
49
209
 
50
- /** 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
+ */
51
221
  export function runtimeDir(): string | null;
52
222
 
53
- /** 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
+ */
54
234
  export function fontsDir(): string;
55
235
 
56
236
  /**
@@ -58,6 +238,14 @@ export function fontsDir(): string;
58
238
  * On Linux, reads `$XDG_CONFIG_DIRS` (default: `["/etc/xdg"]`).
59
239
  * On macOS: `["/Library/Application Support", "/Library/Preferences"]`.
60
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
+ * ```
61
249
  */
62
250
  export function configDirs(): string[];
63
251
 
@@ -66,10 +254,27 @@ export function configDirs(): string[];
66
254
  * On Linux, reads `$XDG_DATA_DIRS` (default: `["/usr/local/share", "/usr/share"]`).
67
255
  * On macOS: `["/Library/Application Support"]`.
68
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
+ * ```
69
265
  */
70
266
  export function dataDirs(): string[];
71
267
 
72
- /** 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
+ */
73
278
  export function getBasePath(name: "config"): string;
74
279
  export function getBasePath(name: "data"): string;
75
280
  export function getBasePath(name: "cache"): string;
@@ -99,6 +304,23 @@ interface ProjectDirsResult {
99
304
  * Returns application-scoped directories for the given app name.
100
305
  * @param name - Application name used to derive directory paths
101
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
+ * ```
102
324
  */
103
325
  export function projectDirs(name: string, options?: ProjectDirsOptions): ProjectDirsResult;
104
326
 
@@ -117,14 +339,46 @@ interface ProjectUserDirsResult {
117
339
  * Returns project-scoped user directories for the given app name.
118
340
  * Each value is the user directory path with the app name appended as a subdirectory.
119
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
+ * ```
120
350
  */
121
351
  export function projectUserDirs(name: string): ProjectUserDirsResult;
122
352
 
353
+ /**
354
+ * Returns the path to the user trash directory, or null on Windows.
355
+ * Linux: `$XDG_DATA_HOME/Trash` (default `~/.local/share/Trash`)
356
+ * macOS: `~/.Trash`
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
+ * ```
366
+ */
367
+ export function trashDir(): string | null;
368
+
123
369
  /**
124
370
  * Returns the path to the user applications directory.
125
371
  * Linux: `$XDG_DATA_HOME/applications` (default `~/.local/share/applications`)
126
372
  * macOS: `~/Applications`
127
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
+ * ```
128
382
  */
129
383
  export function applicationsDir(): string;
130
384
 
@@ -133,13 +387,40 @@ export function applicationsDir(): string;
133
387
  * @param key - XDG key (e.g. "XDG_DOWNLOAD_DIR")
134
388
  * @param configPath - Optional path to user-dirs.dirs config file
135
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
+ * ```
136
396
  */
137
397
  export function getXDGUserDir(key: string, configPath?: string): string | null;
138
398
 
139
399
  /**
140
- * @deprecated Use `getXDGUserDir("XDG_DOWNLOAD_DIR", configPath)` instead.
400
+ * Ensures the specified directory exists, creating it recursively if necessary.
401
+ * @param dirPath - The directory path to ensure exists
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
+ * ```
409
+ */
410
+ export function ensureDirSync(dirPath: string): string;
411
+
412
+ /**
413
+ * Ensures the specified directory exists, creating it recursively if necessary (async version).
414
+ * @param dirPath - The directory path to ensure exists
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
+ * ```
141
422
  */
142
- export function getXDGDownloadDir(configPath?: string): string | null;
423
+ export function ensureDir(dirPath: string): Promise<string>;
143
424
 
144
425
  declare const osUserDirs: typeof downloads & {
145
426
  downloads: typeof downloads;
@@ -163,10 +444,13 @@ declare const osUserDirs: typeof downloads & {
163
444
  configDirs: typeof configDirs;
164
445
  dataDirs: typeof dataDirs;
165
446
  projectDirs: typeof projectDirs;
447
+ trashDir: typeof trashDir;
166
448
  applicationsDir: typeof applicationsDir;
167
449
  projectUserDirs: typeof projectUserDirs;
450
+ homeDir: typeof homeDir;
168
451
  getXDGUserDir: typeof getXDGUserDir;
169
- getXDGDownloadDir: typeof getXDGDownloadDir;
452
+ ensureDirSync: typeof ensureDirSync;
453
+ ensureDir: typeof ensureDir;
170
454
  };
171
455
 
172
456
  export default osUserDirs;
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
- const path = require("path");
2
- const os = require("os");
3
- const fs = require("fs");
1
+ const path = require("node:path");
2
+ const os = require("node:os");
3
+ const fs = require("node:fs");
4
4
 
5
5
  const XDG_KEYS = {
6
6
  desktop: "XDG_DESKTOP_DIR",
@@ -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,45 @@ 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
+ function trashDir() {
327
+ const platform = process.platform;
328
+ const homedir = os.homedir();
329
+
330
+ if (platform === "linux") {
331
+ const envVal = process.env.XDG_DATA_HOME;
332
+ const base = envVal ? path.resolve(envVal) : path.join(homedir, ".local", "share");
333
+ return path.join(base, "Trash");
334
+ }
335
+
336
+ if (platform === "darwin") {
337
+ return path.join(homedir, ".Trash");
338
+ }
339
+
340
+ if (platform === "win32") {
341
+ return null;
342
+ }
343
+
344
+ // Unknown platform: use XDG-style default
345
+ const envVal = process.env.XDG_DATA_HOME;
346
+ const base = envVal ? path.resolve(envVal) : path.join(homedir, ".local", "share");
347
+ return path.join(base, "Trash");
348
+ }
349
+
326
350
  function applicationsDir() {
327
- var homedir = os.homedir();
328
- var platform = process.platform;
351
+ const homedir = os.homedir();
352
+ const platform = process.platform;
329
353
 
330
354
  if (platform === "linux") {
331
- var envVal = process.env.XDG_DATA_HOME;
332
- 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");
333
357
  return path.join(base, "applications");
334
358
  }
335
359
 
@@ -338,7 +362,7 @@ function applicationsDir() {
338
362
  }
339
363
 
340
364
  if (platform === "win32") {
341
- var appdata = process.env.APPDATA || path.join(homedir, "AppData", "Roaming");
365
+ const appdata = process.env.APPDATA || path.join(homedir, "AppData", "Roaming");
342
366
  return path.join(appdata, "Microsoft", "Windows", "Start Menu", "Programs");
343
367
  }
344
368
 
@@ -346,6 +370,27 @@ function applicationsDir() {
346
370
  return path.join(homedir, ".local", "share", "applications");
347
371
  }
348
372
 
373
+ function homeDir() {
374
+ return os.homedir();
375
+ }
376
+
377
+ function ensureDirSync(dirPath) {
378
+ if (!dirPath || typeof dirPath !== "string") {
379
+ throw new Error("ensureDirSync requires a non-empty string path");
380
+ }
381
+ fs.mkdirSync(dirPath, { recursive: true });
382
+ return dirPath;
383
+ }
384
+
385
+ function ensureDir(dirPath) {
386
+ if (!dirPath || typeof dirPath !== "string") {
387
+ return Promise.reject(new Error("ensureDir requires a non-empty string path"));
388
+ }
389
+ return fs.promises.mkdir(dirPath, { recursive: true }).then(function () {
390
+ return dirPath;
391
+ });
392
+ }
393
+
349
394
  // Backward compatibility: require("os-user-dirs")() returns Downloads path
350
395
  module.exports = downloads;
351
396
  module.exports.getPath = getPath;
@@ -371,9 +416,8 @@ module.exports.dataDirs = dataDirs;
371
416
  module.exports.projectDirs = projectDirs;
372
417
  module.exports.applicationsDir = applicationsDir;
373
418
  module.exports.projectUserDirs = projectUserDirs;
419
+ module.exports.homeDir = homeDir;
374
420
  module.exports.getXDGUserDir = getXDGUserDir;
375
-
376
- // Deprecated: kept for backward compatibility
377
- module.exports.getXDGDownloadDir = function (configPath) {
378
- return getXDGUserDir("XDG_DOWNLOAD_DIR", configPath);
379
- };
421
+ module.exports.ensureDirSync = ensureDirSync;
422
+ module.exports.trashDir = trashDir;
423
+ module.exports.ensureDir = ensureDir;
package/index.mjs CHANGED
@@ -24,5 +24,8 @@ export const dataDirs = osUserDirs.dataDirs;
24
24
  export const projectDirs = osUserDirs.projectDirs;
25
25
  export const applicationsDir = osUserDirs.applicationsDir;
26
26
  export const projectUserDirs = osUserDirs.projectUserDirs;
27
+ export const homeDir = osUserDirs.homeDir;
27
28
  export const getXDGUserDir = osUserDirs.getXDGUserDir;
28
- export const getXDGDownloadDir = osUserDirs.getXDGDownloadDir;
29
+ export const trashDir = osUserDirs.trashDir;
30
+ export const ensureDirSync = osUserDirs.ensureDirSync;
31
+ export const ensureDir = osUserDirs.ensureDir;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "os-user-dirs",
3
- "version": "2.7.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,15 +40,57 @@
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",
46
67
  "bugs": {
47
68
  "url": "https://github.com/velocitylabo/os-user-dirs/issues"
48
69
  },
70
+ "engines": {
71
+ "node": ">=20"
72
+ },
49
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
+ },
50
92
  "devDependencies": {
93
+ "c8": "^11.0.0",
51
94
  "mocha": "^11.7.5"
52
95
  }
53
96
  }