os-user-dirs 2.3.0 → 2.5.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 CHANGED
@@ -1,6 +1,6 @@
1
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)
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,41 @@ getBasePath("config");
60
66
  //=> '/home/user/.config'
61
67
  ```
62
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
+
90
+ const dirs = projectDirs("my-app");
91
+ dirs.config //=> '/home/user/.config/my-app'
92
+ dirs.data //=> '/home/user/.local/share/my-app'
93
+ 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
+
63
104
  ### CommonJS
64
105
 
65
106
  ```javascript
@@ -135,11 +176,38 @@ Returns the path to the data directory (`~/.local/share` on Linux, `~/Library/Ap
135
176
  #### `cacheDir()`
136
177
  Returns the path to the cache directory (`~/.cache` on Linux, `~/Library/Caches` on macOS, `%LOCALAPPDATA%` on Windows).
137
178
 
179
+ #### `stateDir()`
180
+ Returns the path to the state directory (`~/.local/state` on Linux, `~/Library/Application Support` on macOS, `%LOCALAPPDATA%` on Windows).
181
+
182
+ #### `logDir()`
183
+ Returns the path to the log directory (`~/.local/state` on Linux, `~/Library/Logs` on macOS, `%LOCALAPPDATA%` on Windows).
184
+
138
185
  #### `runtimeDir()`
139
186
  Returns the path to the runtime directory (`$XDG_RUNTIME_DIR` on Linux), or `null` if unavailable.
140
187
 
141
188
  #### `getBasePath(name)`
142
- Returns the path to the specified base directory. Valid names: `config`, `data`, `cache`, `runtime`.
189
+ Returns the path to the specified base directory. Valid names: `config`, `data`, `cache`, `state`, `log`, `runtime`.
190
+
191
+ ### System Search Directories
192
+
193
+ #### `configDirs()`
194
+ 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%]`.
195
+
196
+ #### `dataDirs()`
197
+ 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%]`.
198
+
199
+ ### Project Directories
200
+
201
+ #### `projectDirs(name, options?)`
202
+ 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
+
204
+ **Parameters:**
205
+ - `name` (string) — Application name
206
+ - `options.suffix` (string, optional) — Suffix appended to the app name (e.g. `"-nodejs"`)
207
+
208
+ **Returns:** `{ config, data, cache, state, log, temp, runtime }`
209
+
210
+ On Windows, each directory uses a subdirectory structure (e.g. `%LOCALAPPDATA%/my-app/Config`, `%LOCALAPPDATA%/my-app/Data`).
143
211
 
144
212
  ## License
145
213
 
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,16 +38,62 @@ 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
 
50
+ /**
51
+ * Returns the system config directory search path list.
52
+ * On Linux, reads `$XDG_CONFIG_DIRS` (default: `["/etc/xdg"]`).
53
+ * On macOS: `["/Library/Application Support", "/Library/Preferences"]`.
54
+ * On Windows: `[%PROGRAMDATA%]`.
55
+ */
56
+ export function configDirs(): string[];
57
+
58
+ /**
59
+ * Returns the system data directory search path list.
60
+ * On Linux, reads `$XDG_DATA_DIRS` (default: `["/usr/local/share", "/usr/share"]`).
61
+ * On macOS: `["/Library/Application Support"]`.
62
+ * On Windows: `[%PROGRAMDATA%]`.
63
+ */
64
+ export function dataDirs(): string[];
65
+
44
66
  /** Returns the path to the specified base directory. */
45
67
  export function getBasePath(name: "config"): string;
46
68
  export function getBasePath(name: "data"): string;
47
69
  export function getBasePath(name: "cache"): string;
70
+ export function getBasePath(name: "state"): string;
71
+ export function getBasePath(name: "log"): string;
48
72
  export function getBasePath(name: "runtime"): string | null;
49
73
  export function getBasePath(name: BaseDirName): string | null;
50
74
 
75
+ interface ProjectDirsOptions {
76
+ /** Suffix appended to the app name (default: ""). */
77
+ suffix?: string;
78
+ }
79
+
80
+ interface ProjectDirsResult {
81
+ config: string;
82
+ data: string;
83
+ cache: string;
84
+ state: string;
85
+ log: string;
86
+ temp: string;
87
+ runtime: string | null;
88
+ }
89
+
90
+ /**
91
+ * Returns application-scoped directories for the given app name.
92
+ * @param name - Application name used to derive directory paths
93
+ * @param options - Optional settings (e.g. suffix)
94
+ */
95
+ export function projectDirs(name: string, options?: ProjectDirsOptions): ProjectDirsResult;
96
+
51
97
  /**
52
98
  * Reads an XDG user-dirs.dirs config and returns the directory for the given key.
53
99
  * @param key - XDG key (e.g. "XDG_DOWNLOAD_DIR")
@@ -74,8 +120,13 @@ declare const osUserDirs: typeof downloads & {
74
120
  configDir: typeof configDir;
75
121
  dataDir: typeof dataDir;
76
122
  cacheDir: typeof cacheDir;
123
+ stateDir: typeof stateDir;
124
+ logDir: typeof logDir;
77
125
  runtimeDir: typeof runtimeDir;
78
126
  getBasePath: typeof getBasePath;
127
+ configDirs: typeof configDirs;
128
+ dataDirs: typeof dataDirs;
129
+ projectDirs: typeof projectDirs;
79
130
  getXDGUserDir: typeof getXDGUserDir;
80
131
  getXDGDownloadDir: typeof getXDGDownloadDir;
81
132
  };
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", 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
- runtime: { env: "XDG_RUNTIME_DIR", linux: null, darwin: null, win32: null },
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,127 @@ 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
 
147
+ const SEARCH_DIRS_CONFIG = {
148
+ config: {
149
+ env: "XDG_CONFIG_DIRS",
150
+ linux: ["/etc/xdg"],
151
+ darwin: ["/Library/Application Support", "/Library/Preferences"],
152
+ win32: "PROGRAMDATA",
153
+ },
154
+ data: {
155
+ env: "XDG_DATA_DIRS",
156
+ linux: ["/usr/local/share", "/usr/share"],
157
+ darwin: ["/Library/Application Support"],
158
+ win32: "PROGRAMDATA",
159
+ },
160
+ };
161
+
162
+ function resolveSearchDirs(name) {
163
+ const cfg = SEARCH_DIRS_CONFIG[name];
164
+ if (!cfg) {
165
+ throw new Error("Unknown search directory: " + name + ". Valid names: " + Object.keys(SEARCH_DIRS_CONFIG).join(", "));
166
+ }
167
+
168
+ const platform = process.platform;
169
+
170
+ if (platform === "linux") {
171
+ const envVal = process.env[cfg.env];
172
+ if (envVal) {
173
+ const dirs = envVal.split(":").filter(Boolean);
174
+ if (dirs.length > 0) {
175
+ return dirs.map(function (d) { return path.resolve(d); });
176
+ }
177
+ }
178
+ return cfg.linux.slice();
179
+ }
180
+
181
+ if (platform === "darwin") {
182
+ return cfg.darwin.slice();
183
+ }
184
+
185
+ if (platform === "win32") {
186
+ const winVal = process.env[cfg.win32];
187
+ if (winVal) {
188
+ return [path.resolve(winVal)];
189
+ }
190
+ return [path.join(process.env.SYSTEMDRIVE || "C:", "ProgramData")];
191
+ }
192
+
193
+ // Unknown platform: use Linux defaults
194
+ return cfg.linux.slice();
195
+ }
196
+
197
+ function configDirs() { return resolveSearchDirs("config"); }
198
+ function dataDirs() { return resolveSearchDirs("data"); }
199
+
143
200
  function getBasePath(name) {
144
201
  return resolveBase(name);
145
202
  }
146
203
 
204
+ const PROJECT_DIR_WIN32_SUB = {
205
+ config: "Config",
206
+ data: "Data",
207
+ cache: "Cache",
208
+ state: "State",
209
+ log: "Log",
210
+ };
211
+
212
+ function projectDirs(name, options) {
213
+ if (!name || typeof name !== "string") {
214
+ throw new Error("projectDirs requires a non-empty string name");
215
+ }
216
+
217
+ const suffix = (options && options.suffix != null) ? options.suffix : "";
218
+ const appName = name + suffix;
219
+
220
+ const homedir = os.homedir();
221
+ const platform = process.platform;
222
+
223
+ function resolveProject(kind) {
224
+ if (kind === "temp") {
225
+ if (platform === "win32") {
226
+ const localAppData = process.env.LOCALAPPDATA || path.join(homedir, "AppData", "Local");
227
+ return path.join(localAppData, "Temp", appName);
228
+ }
229
+ return path.join(os.tmpdir(), appName);
230
+ }
231
+
232
+ if (kind === "runtime") {
233
+ if (platform === "linux") {
234
+ const envVal = process.env.XDG_RUNTIME_DIR;
235
+ if (envVal) {
236
+ return path.join(path.resolve(envVal), appName);
237
+ }
238
+ }
239
+ return null;
240
+ }
241
+
242
+ const base = resolveBase(kind);
243
+ if (!base) { return null; }
244
+
245
+ const sub = PROJECT_DIR_WIN32_SUB[kind];
246
+ if (platform === "win32" && sub) {
247
+ return path.join(base, appName, sub);
248
+ }
249
+
250
+ return path.join(base, appName);
251
+ }
252
+
253
+ return {
254
+ config: resolveProject("config"),
255
+ data: resolveProject("data"),
256
+ cache: resolveProject("cache"),
257
+ state: resolveProject("state"),
258
+ log: resolveProject("log"),
259
+ temp: resolveProject("temp"),
260
+ runtime: resolveProject("runtime"),
261
+ };
262
+ }
263
+
147
264
  // Backward compatibility: require("os-user-dirs")() returns Downloads path
148
265
  module.exports = downloads;
149
266
  module.exports.getPath = getPath;
@@ -158,8 +275,13 @@ module.exports.publicshare = publicshare;
158
275
  module.exports.configDir = configDir;
159
276
  module.exports.dataDir = dataDir;
160
277
  module.exports.cacheDir = cacheDir;
278
+ module.exports.stateDir = stateDir;
279
+ module.exports.logDir = logDir;
161
280
  module.exports.runtimeDir = runtimeDir;
162
281
  module.exports.getBasePath = getBasePath;
282
+ module.exports.configDirs = configDirs;
283
+ module.exports.dataDirs = dataDirs;
284
+ module.exports.projectDirs = projectDirs;
163
285
  module.exports.getXDGUserDir = getXDGUserDir;
164
286
 
165
287
  // Deprecated: kept for backward compatibility
package/index.mjs CHANGED
@@ -13,7 +13,12 @@ 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 configDirs = osUserDirs.configDirs;
21
+ export const dataDirs = osUserDirs.dataDirs;
22
+ export const projectDirs = osUserDirs.projectDirs;
18
23
  export const getXDGUserDir = osUserDirs.getXDGUserDir;
19
24
  export const getXDGDownloadDir = osUserDirs.getXDGDownloadDir;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "os-user-dirs",
3
- "version": "2.3.0",
3
+ "version": "2.5.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",