os-user-dirs 2.3.0 → 2.4.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 +47 -3
- package/index.d.ts +34 -1
- package/index.js +71 -4
- package/index.mjs +3 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# os-user-dirs [](https://github.com/velocitylabo/os-user-dirs/actions/workflows/ci.yml)
|
|
2
2
|
|
|
3
|
-
Get OS-specific user directories (Downloads, Desktop, Documents, etc.) and XDG base directories (config, data, cache, runtime) with zero dependencies.
|
|
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`).
|
|
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
|
|
|
@@ -42,7 +42,7 @@ getPath("music");
|
|
|
42
42
|
#### Base directories
|
|
43
43
|
|
|
44
44
|
```javascript
|
|
45
|
-
import { configDir, dataDir, cacheDir, runtimeDir, getBasePath } from "os-user-dirs";
|
|
45
|
+
import { configDir, dataDir, cacheDir, stateDir, logDir, runtimeDir, getBasePath } from "os-user-dirs";
|
|
46
46
|
|
|
47
47
|
configDir();
|
|
48
48
|
//=> '/home/user/.config'
|
|
@@ -53,6 +53,12 @@ dataDir();
|
|
|
53
53
|
cacheDir();
|
|
54
54
|
//=> '/home/user/.cache'
|
|
55
55
|
|
|
56
|
+
stateDir();
|
|
57
|
+
//=> '/home/user/.local/state'
|
|
58
|
+
|
|
59
|
+
logDir();
|
|
60
|
+
//=> '/home/user/.local/state' (Linux), '~/Library/Logs' (macOS)
|
|
61
|
+
|
|
56
62
|
runtimeDir();
|
|
57
63
|
//=> '/run/user/1000' (or null)
|
|
58
64
|
|
|
@@ -60,6 +66,25 @@ getBasePath("config");
|
|
|
60
66
|
//=> '/home/user/.config'
|
|
61
67
|
```
|
|
62
68
|
|
|
69
|
+
#### Project directories
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
import { projectDirs } from "os-user-dirs";
|
|
73
|
+
|
|
74
|
+
const dirs = projectDirs("my-app");
|
|
75
|
+
dirs.config //=> '/home/user/.config/my-app'
|
|
76
|
+
dirs.data //=> '/home/user/.local/share/my-app'
|
|
77
|
+
dirs.cache //=> '/home/user/.cache/my-app'
|
|
78
|
+
dirs.state //=> '/home/user/.local/state/my-app'
|
|
79
|
+
dirs.log //=> '/home/user/.local/state/my-app'
|
|
80
|
+
dirs.temp //=> '/tmp/my-app'
|
|
81
|
+
dirs.runtime //=> '/run/user/1000/my-app' (or null)
|
|
82
|
+
|
|
83
|
+
// With suffix option
|
|
84
|
+
const dirs2 = projectDirs("my-app", { suffix: "-nodejs" });
|
|
85
|
+
dirs2.config //=> '/home/user/.config/my-app-nodejs'
|
|
86
|
+
```
|
|
87
|
+
|
|
63
88
|
### CommonJS
|
|
64
89
|
|
|
65
90
|
```javascript
|
|
@@ -135,11 +160,30 @@ Returns the path to the data directory (`~/.local/share` on Linux, `~/Library/Ap
|
|
|
135
160
|
#### `cacheDir()`
|
|
136
161
|
Returns the path to the cache directory (`~/.cache` on Linux, `~/Library/Caches` on macOS, `%LOCALAPPDATA%` on Windows).
|
|
137
162
|
|
|
163
|
+
#### `stateDir()`
|
|
164
|
+
Returns the path to the state directory (`~/.local/state` on Linux, `~/Library/Application Support` on macOS, `%LOCALAPPDATA%` on Windows).
|
|
165
|
+
|
|
166
|
+
#### `logDir()`
|
|
167
|
+
Returns the path to the log directory (`~/.local/state` on Linux, `~/Library/Logs` on macOS, `%LOCALAPPDATA%` on Windows).
|
|
168
|
+
|
|
138
169
|
#### `runtimeDir()`
|
|
139
170
|
Returns the path to the runtime directory (`$XDG_RUNTIME_DIR` on Linux), or `null` if unavailable.
|
|
140
171
|
|
|
141
172
|
#### `getBasePath(name)`
|
|
142
|
-
Returns the path to the specified base directory. Valid names: `config`, `data`, `cache`, `runtime`.
|
|
173
|
+
Returns the path to the specified base directory. Valid names: `config`, `data`, `cache`, `state`, `log`, `runtime`.
|
|
174
|
+
|
|
175
|
+
### Project Directories
|
|
176
|
+
|
|
177
|
+
#### `projectDirs(name, options?)`
|
|
178
|
+
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).
|
|
179
|
+
|
|
180
|
+
**Parameters:**
|
|
181
|
+
- `name` (string) — Application name
|
|
182
|
+
- `options.suffix` (string, optional) — Suffix appended to the app name (e.g. `"-nodejs"`)
|
|
183
|
+
|
|
184
|
+
**Returns:** `{ config, data, cache, state, log, temp, runtime }`
|
|
185
|
+
|
|
186
|
+
On Windows, each directory uses a subdirectory structure (e.g. `%LOCALAPPDATA%/my-app/Config`, `%LOCALAPPDATA%/my-app/Data`).
|
|
143
187
|
|
|
144
188
|
## License
|
|
145
189
|
|
package/index.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ 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
|
-
type BaseDirName = "config" | "data" | "cache" | "runtime";
|
|
30
|
+
type BaseDirName = "config" | "data" | "cache" | "state" | "log" | "runtime";
|
|
31
31
|
|
|
32
32
|
/** Returns the path to the XDG config directory. */
|
|
33
33
|
export function configDir(): string;
|
|
@@ -38,6 +38,12 @@ export function dataDir(): string;
|
|
|
38
38
|
/** Returns the path to the XDG cache directory. */
|
|
39
39
|
export function cacheDir(): string;
|
|
40
40
|
|
|
41
|
+
/** Returns the path to the XDG state directory. */
|
|
42
|
+
export function stateDir(): string;
|
|
43
|
+
|
|
44
|
+
/** Returns the path to the log directory. */
|
|
45
|
+
export function logDir(): string;
|
|
46
|
+
|
|
41
47
|
/** Returns the path to the XDG runtime directory, or null if unavailable. */
|
|
42
48
|
export function runtimeDir(): string | null;
|
|
43
49
|
|
|
@@ -45,9 +51,33 @@ export function runtimeDir(): string | null;
|
|
|
45
51
|
export function getBasePath(name: "config"): string;
|
|
46
52
|
export function getBasePath(name: "data"): string;
|
|
47
53
|
export function getBasePath(name: "cache"): string;
|
|
54
|
+
export function getBasePath(name: "state"): string;
|
|
55
|
+
export function getBasePath(name: "log"): string;
|
|
48
56
|
export function getBasePath(name: "runtime"): string | null;
|
|
49
57
|
export function getBasePath(name: BaseDirName): string | null;
|
|
50
58
|
|
|
59
|
+
interface ProjectDirsOptions {
|
|
60
|
+
/** Suffix appended to the app name (default: ""). */
|
|
61
|
+
suffix?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface ProjectDirsResult {
|
|
65
|
+
config: string;
|
|
66
|
+
data: string;
|
|
67
|
+
cache: string;
|
|
68
|
+
state: string;
|
|
69
|
+
log: string;
|
|
70
|
+
temp: string;
|
|
71
|
+
runtime: string | null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Returns application-scoped directories for the given app name.
|
|
76
|
+
* @param name - Application name used to derive directory paths
|
|
77
|
+
* @param options - Optional settings (e.g. suffix)
|
|
78
|
+
*/
|
|
79
|
+
export function projectDirs(name: string, options?: ProjectDirsOptions): ProjectDirsResult;
|
|
80
|
+
|
|
51
81
|
/**
|
|
52
82
|
* Reads an XDG user-dirs.dirs config and returns the directory for the given key.
|
|
53
83
|
* @param key - XDG key (e.g. "XDG_DOWNLOAD_DIR")
|
|
@@ -74,8 +104,11 @@ declare const osUserDirs: typeof downloads & {
|
|
|
74
104
|
configDir: typeof configDir;
|
|
75
105
|
dataDir: typeof dataDir;
|
|
76
106
|
cacheDir: typeof cacheDir;
|
|
107
|
+
stateDir: typeof stateDir;
|
|
108
|
+
logDir: typeof logDir;
|
|
77
109
|
runtimeDir: typeof runtimeDir;
|
|
78
110
|
getBasePath: typeof getBasePath;
|
|
111
|
+
projectDirs: typeof projectDirs;
|
|
79
112
|
getXDGUserDir: typeof getXDGUserDir;
|
|
80
113
|
getXDGDownloadDir: typeof getXDGDownloadDir;
|
|
81
114
|
};
|
package/index.js
CHANGED
|
@@ -84,10 +84,12 @@ function templates() { return resolve("templates"); }
|
|
|
84
84
|
function publicshare() { return resolve("publicshare"); }
|
|
85
85
|
|
|
86
86
|
const BASE_DIR_CONFIG = {
|
|
87
|
-
config: { env: "XDG_CONFIG_HOME", linux: ".config",
|
|
88
|
-
data: { env: "XDG_DATA_HOME", linux: ".local/share",
|
|
89
|
-
cache: { env: "XDG_CACHE_HOME", linux: ".cache",
|
|
90
|
-
|
|
87
|
+
config: { env: "XDG_CONFIG_HOME", linux: ".config", darwin: "Library/Application Support", win32: "APPDATA" },
|
|
88
|
+
data: { env: "XDG_DATA_HOME", linux: ".local/share", darwin: "Library/Application Support", win32: "LOCALAPPDATA" },
|
|
89
|
+
cache: { env: "XDG_CACHE_HOME", linux: ".cache", darwin: "Library/Caches", win32: "LOCALAPPDATA" },
|
|
90
|
+
state: { env: "XDG_STATE_HOME", linux: ".local/state", darwin: "Library/Application Support", win32: "LOCALAPPDATA" },
|
|
91
|
+
log: { env: "XDG_STATE_HOME", linux: ".local/state", darwin: "Library/Logs", win32: "LOCALAPPDATA" },
|
|
92
|
+
runtime: { env: "XDG_RUNTIME_DIR", linux: null, darwin: null, win32: null },
|
|
91
93
|
};
|
|
92
94
|
|
|
93
95
|
function resolveBase(name) {
|
|
@@ -138,12 +140,74 @@ function resolveBase(name) {
|
|
|
138
140
|
function configDir() { return resolveBase("config"); }
|
|
139
141
|
function dataDir() { return resolveBase("data"); }
|
|
140
142
|
function cacheDir() { return resolveBase("cache"); }
|
|
143
|
+
function stateDir() { return resolveBase("state"); }
|
|
144
|
+
function logDir() { return resolveBase("log"); }
|
|
141
145
|
function runtimeDir() { return resolveBase("runtime"); }
|
|
142
146
|
|
|
143
147
|
function getBasePath(name) {
|
|
144
148
|
return resolveBase(name);
|
|
145
149
|
}
|
|
146
150
|
|
|
151
|
+
const PROJECT_DIR_WIN32_SUB = {
|
|
152
|
+
config: "Config",
|
|
153
|
+
data: "Data",
|
|
154
|
+
cache: "Cache",
|
|
155
|
+
state: "State",
|
|
156
|
+
log: "Log",
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
function projectDirs(name, options) {
|
|
160
|
+
if (!name || typeof name !== "string") {
|
|
161
|
+
throw new Error("projectDirs requires a non-empty string name");
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const suffix = (options && options.suffix != null) ? options.suffix : "";
|
|
165
|
+
const appName = name + suffix;
|
|
166
|
+
|
|
167
|
+
const homedir = os.homedir();
|
|
168
|
+
const platform = process.platform;
|
|
169
|
+
|
|
170
|
+
function resolveProject(kind) {
|
|
171
|
+
if (kind === "temp") {
|
|
172
|
+
if (platform === "win32") {
|
|
173
|
+
const localAppData = process.env.LOCALAPPDATA || path.join(homedir, "AppData", "Local");
|
|
174
|
+
return path.join(localAppData, "Temp", appName);
|
|
175
|
+
}
|
|
176
|
+
return path.join(os.tmpdir(), appName);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (kind === "runtime") {
|
|
180
|
+
if (platform === "linux") {
|
|
181
|
+
const envVal = process.env.XDG_RUNTIME_DIR;
|
|
182
|
+
if (envVal) {
|
|
183
|
+
return path.join(path.resolve(envVal), appName);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const base = resolveBase(kind);
|
|
190
|
+
if (!base) { return null; }
|
|
191
|
+
|
|
192
|
+
const sub = PROJECT_DIR_WIN32_SUB[kind];
|
|
193
|
+
if (platform === "win32" && sub) {
|
|
194
|
+
return path.join(base, appName, sub);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return path.join(base, appName);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
config: resolveProject("config"),
|
|
202
|
+
data: resolveProject("data"),
|
|
203
|
+
cache: resolveProject("cache"),
|
|
204
|
+
state: resolveProject("state"),
|
|
205
|
+
log: resolveProject("log"),
|
|
206
|
+
temp: resolveProject("temp"),
|
|
207
|
+
runtime: resolveProject("runtime"),
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
147
211
|
// Backward compatibility: require("os-user-dirs")() returns Downloads path
|
|
148
212
|
module.exports = downloads;
|
|
149
213
|
module.exports.getPath = getPath;
|
|
@@ -158,8 +222,11 @@ module.exports.publicshare = publicshare;
|
|
|
158
222
|
module.exports.configDir = configDir;
|
|
159
223
|
module.exports.dataDir = dataDir;
|
|
160
224
|
module.exports.cacheDir = cacheDir;
|
|
225
|
+
module.exports.stateDir = stateDir;
|
|
226
|
+
module.exports.logDir = logDir;
|
|
161
227
|
module.exports.runtimeDir = runtimeDir;
|
|
162
228
|
module.exports.getBasePath = getBasePath;
|
|
229
|
+
module.exports.projectDirs = projectDirs;
|
|
163
230
|
module.exports.getXDGUserDir = getXDGUserDir;
|
|
164
231
|
|
|
165
232
|
// Deprecated: kept for backward compatibility
|
package/index.mjs
CHANGED
|
@@ -13,7 +13,10 @@ export const getPath = osUserDirs.getPath;
|
|
|
13
13
|
export const configDir = osUserDirs.configDir;
|
|
14
14
|
export const dataDir = osUserDirs.dataDir;
|
|
15
15
|
export const cacheDir = osUserDirs.cacheDir;
|
|
16
|
+
export const stateDir = osUserDirs.stateDir;
|
|
17
|
+
export const logDir = osUserDirs.logDir;
|
|
16
18
|
export const runtimeDir = osUserDirs.runtimeDir;
|
|
17
19
|
export const getBasePath = osUserDirs.getBasePath;
|
|
20
|
+
export const projectDirs = osUserDirs.projectDirs;
|
|
18
21
|
export const getXDGUserDir = osUserDirs.getXDGUserDir;
|
|
19
22
|
export const getXDGDownloadDir = osUserDirs.getXDGDownloadDir;
|
package/package.json
CHANGED