os-user-dirs 3.0.0 → 3.1.0

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