os-user-dirs 2.7.0 → 3.0.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.
- package/README.md +60 -1
- package/index.d.ts +26 -3
- package/index.js +52 -8
- package/index.mjs +4 -1
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -4,6 +4,10 @@ Get OS-specific user directories (Downloads, Desktop, Documents, etc.) and XDG b
|
|
|
4
4
|
|
|
5
5
|
> **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.
|
|
6
6
|
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
- Node.js 18 or later
|
|
10
|
+
|
|
7
11
|
## Supported platforms
|
|
8
12
|
|
|
9
13
|
- Windows
|
|
@@ -21,7 +25,10 @@ $ npm install os-user-dirs
|
|
|
21
25
|
### ESM (recommended)
|
|
22
26
|
|
|
23
27
|
```javascript
|
|
24
|
-
import { downloads, desktop, documents, music, pictures, videos, templates, publicshare, getPath } from "os-user-dirs";
|
|
28
|
+
import { downloads, desktop, documents, music, pictures, videos, templates, publicshare, getPath, homeDir } from "os-user-dirs";
|
|
29
|
+
|
|
30
|
+
homeDir();
|
|
31
|
+
//=> '/home/user'
|
|
25
32
|
|
|
26
33
|
downloads();
|
|
27
34
|
//=> '/home/user/Downloads'
|
|
@@ -186,9 +193,18 @@ Returns the path to the Public Share directory.
|
|
|
186
193
|
### `getPath(name)`
|
|
187
194
|
Returns the path to the specified user directory. Valid names: `desktop`, `downloads`, `documents`, `music`, `pictures`, `videos`, `templates`, `publicshare`.
|
|
188
195
|
|
|
196
|
+
### `homeDir()`
|
|
197
|
+
Returns the path to the user's home directory. Uses `os.homedir()` internally.
|
|
198
|
+
|
|
189
199
|
### `binDir()`
|
|
190
200
|
Returns the path to the user local bin directory (`~/.local/bin` on Linux/macOS), or `null` on Windows.
|
|
191
201
|
|
|
202
|
+
### `trashDir()`
|
|
203
|
+
Returns the path to the user trash directory, or `null` on Windows.
|
|
204
|
+
- Linux: `$XDG_DATA_HOME/Trash` (default `~/.local/share/Trash`) — FreeDesktop Trash spec
|
|
205
|
+
- macOS: `~/.Trash`
|
|
206
|
+
- Windows: `null` (Recycle Bin requires Shell API)
|
|
207
|
+
|
|
192
208
|
### `applicationsDir()`
|
|
193
209
|
Returns the path to the user applications directory.
|
|
194
210
|
- Linux: `$XDG_DATA_HOME/applications` (default `~/.local/share/applications`)
|
|
@@ -251,6 +267,49 @@ Returns an object with project-scoped user directories. Each value is the corres
|
|
|
251
267
|
|
|
252
268
|
**Returns:** `{ desktop, downloads, documents, music, pictures, videos, templates, publicshare }`
|
|
253
269
|
|
|
270
|
+
### Utilities
|
|
271
|
+
|
|
272
|
+
#### `ensureDirSync(dirPath)`
|
|
273
|
+
Ensures the specified directory exists, creating it recursively if necessary. Returns the directory path.
|
|
274
|
+
|
|
275
|
+
#### `ensureDir(dirPath)`
|
|
276
|
+
Async version of `ensureDirSync`. Returns a promise that resolves with the directory path.
|
|
277
|
+
|
|
278
|
+
```javascript
|
|
279
|
+
import { projectDirs, ensureDirSync, ensureDir } from "os-user-dirs";
|
|
280
|
+
|
|
281
|
+
const dirs = projectDirs("my-app");
|
|
282
|
+
|
|
283
|
+
// Sync
|
|
284
|
+
ensureDirSync(dirs.config);
|
|
285
|
+
|
|
286
|
+
// Async
|
|
287
|
+
await ensureDir(dirs.data);
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
## Migration from env-paths
|
|
291
|
+
|
|
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.
|
|
293
|
+
|
|
294
|
+
## Migration from v2.x
|
|
295
|
+
|
|
296
|
+
### `getXDGDownloadDir()` removed
|
|
297
|
+
|
|
298
|
+
`getXDGDownloadDir()` was deprecated in v2.3.0 and has been removed in v3.0.0.
|
|
299
|
+
|
|
300
|
+
**Before (v2.x):**
|
|
301
|
+
```javascript
|
|
302
|
+
const { getXDGDownloadDir } = require("os-user-dirs");
|
|
303
|
+
getXDGDownloadDir(); // reads XDG user-dirs.dirs for download path
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
**After (v3.x):**
|
|
307
|
+
```javascript
|
|
308
|
+
const { downloads, getXDGUserDir } = require("os-user-dirs");
|
|
309
|
+
downloads(); // recommended: cross-platform Downloads path
|
|
310
|
+
getXDGUserDir("XDG_DOWNLOAD_DIR"); // direct replacement if you need XDG config parsing
|
|
311
|
+
```
|
|
312
|
+
|
|
254
313
|
## License
|
|
255
314
|
|
|
256
315
|
MIT
|
package/index.d.ts
CHANGED
|
@@ -27,6 +27,9 @@ export function publicshare(): string;
|
|
|
27
27
|
/** Returns the path to the specified user directory. */
|
|
28
28
|
export function getPath(name: DirName): string;
|
|
29
29
|
|
|
30
|
+
/** Returns the path to the user's home directory. */
|
|
31
|
+
export function homeDir(): string;
|
|
32
|
+
|
|
30
33
|
/** Returns the path to the user local bin directory (~/.local/bin), or null on Windows. */
|
|
31
34
|
export function binDir(): string | null;
|
|
32
35
|
|
|
@@ -120,6 +123,14 @@ interface ProjectUserDirsResult {
|
|
|
120
123
|
*/
|
|
121
124
|
export function projectUserDirs(name: string): ProjectUserDirsResult;
|
|
122
125
|
|
|
126
|
+
/**
|
|
127
|
+
* Returns the path to the user trash directory, or null on Windows.
|
|
128
|
+
* Linux: `$XDG_DATA_HOME/Trash` (default `~/.local/share/Trash`)
|
|
129
|
+
* macOS: `~/.Trash`
|
|
130
|
+
* Windows: `null` (Recycle Bin requires Shell API)
|
|
131
|
+
*/
|
|
132
|
+
export function trashDir(): string | null;
|
|
133
|
+
|
|
123
134
|
/**
|
|
124
135
|
* Returns the path to the user applications directory.
|
|
125
136
|
* Linux: `$XDG_DATA_HOME/applications` (default `~/.local/share/applications`)
|
|
@@ -137,9 +148,18 @@ export function applicationsDir(): string;
|
|
|
137
148
|
export function getXDGUserDir(key: string, configPath?: string): string | null;
|
|
138
149
|
|
|
139
150
|
/**
|
|
140
|
-
*
|
|
151
|
+
* Ensures the specified directory exists, creating it recursively if necessary.
|
|
152
|
+
* @param dirPath - The directory path to ensure exists
|
|
153
|
+
* @returns The directory path
|
|
154
|
+
*/
|
|
155
|
+
export function ensureDirSync(dirPath: string): string;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Ensures the specified directory exists, creating it recursively if necessary (async version).
|
|
159
|
+
* @param dirPath - The directory path to ensure exists
|
|
160
|
+
* @returns A promise that resolves with the directory path
|
|
141
161
|
*/
|
|
142
|
-
export function
|
|
162
|
+
export function ensureDir(dirPath: string): Promise<string>;
|
|
143
163
|
|
|
144
164
|
declare const osUserDirs: typeof downloads & {
|
|
145
165
|
downloads: typeof downloads;
|
|
@@ -163,10 +183,13 @@ declare const osUserDirs: typeof downloads & {
|
|
|
163
183
|
configDirs: typeof configDirs;
|
|
164
184
|
dataDirs: typeof dataDirs;
|
|
165
185
|
projectDirs: typeof projectDirs;
|
|
186
|
+
trashDir: typeof trashDir;
|
|
166
187
|
applicationsDir: typeof applicationsDir;
|
|
167
188
|
projectUserDirs: typeof projectUserDirs;
|
|
189
|
+
homeDir: typeof homeDir;
|
|
168
190
|
getXDGUserDir: typeof getXDGUserDir;
|
|
169
|
-
|
|
191
|
+
ensureDirSync: typeof ensureDirSync;
|
|
192
|
+
ensureDir: typeof ensureDir;
|
|
170
193
|
};
|
|
171
194
|
|
|
172
195
|
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",
|
|
@@ -323,6 +323,30 @@ function projectUserDirs(name) {
|
|
|
323
323
|
return result;
|
|
324
324
|
}
|
|
325
325
|
|
|
326
|
+
function trashDir() {
|
|
327
|
+
var platform = process.platform;
|
|
328
|
+
var homedir = os.homedir();
|
|
329
|
+
|
|
330
|
+
if (platform === "linux") {
|
|
331
|
+
var envVal = process.env.XDG_DATA_HOME;
|
|
332
|
+
var 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
|
+
var envVal = process.env.XDG_DATA_HOME;
|
|
346
|
+
var base = envVal ? path.resolve(envVal) : path.join(homedir, ".local", "share");
|
|
347
|
+
return path.join(base, "Trash");
|
|
348
|
+
}
|
|
349
|
+
|
|
326
350
|
function applicationsDir() {
|
|
327
351
|
var homedir = os.homedir();
|
|
328
352
|
var platform = process.platform;
|
|
@@ -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
|
-
|
|
377
|
-
module.exports.
|
|
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
|
|
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": "
|
|
3
|
+
"version": "3.0.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",
|
|
@@ -46,6 +46,9 @@
|
|
|
46
46
|
"bugs": {
|
|
47
47
|
"url": "https://github.com/velocitylabo/os-user-dirs/issues"
|
|
48
48
|
},
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">=18"
|
|
51
|
+
},
|
|
49
52
|
"homepage": "https://github.com/velocitylabo/os-user-dirs#readme",
|
|
50
53
|
"devDependencies": {
|
|
51
54
|
"mocha": "^11.7.5"
|