os-user-dirs 2.2.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 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, Music, Pictures, Videos) 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
 
@@ -21,7 +21,7 @@ $ npm install os-user-dirs
21
21
  ### ESM (recommended)
22
22
 
23
23
  ```javascript
24
- import { downloads, desktop, documents, music, pictures, videos, getPath } from "os-user-dirs";
24
+ import { downloads, desktop, documents, music, pictures, videos, templates, publicshare, getPath } from "os-user-dirs";
25
25
 
26
26
  downloads();
27
27
  //=> '/home/user/Downloads'
@@ -29,17 +29,66 @@ downloads();
29
29
  desktop();
30
30
  //=> '/home/user/Desktop'
31
31
 
32
- documents();
33
- //=> '/home/user/Documents'
32
+ templates();
33
+ //=> '/home/user/Templates'
34
+
35
+ publicshare();
36
+ //=> '/home/user/Public'
34
37
 
35
38
  getPath("music");
36
39
  //=> '/home/user/Music'
37
40
  ```
38
41
 
42
+ #### Base directories
43
+
44
+ ```javascript
45
+ import { configDir, dataDir, cacheDir, stateDir, logDir, runtimeDir, getBasePath } from "os-user-dirs";
46
+
47
+ configDir();
48
+ //=> '/home/user/.config'
49
+
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
+ #### 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
+
39
88
  ### CommonJS
40
89
 
41
90
  ```javascript
42
- const { downloads, desktop, documents, music, pictures, videos, getPath } = require("os-user-dirs");
91
+ const { downloads, desktop, documents, music, pictures, videos, templates, publicshare, getPath } = require("os-user-dirs");
43
92
 
44
93
  downloads();
45
94
  //=> '/home/user/Downloads'
@@ -65,9 +114,10 @@ Full type definitions are included. `getPath()` accepts a union type for auto-co
65
114
  ```typescript
66
115
  import { getPath } from "os-user-dirs";
67
116
 
68
- getPath("downloads"); // OK
69
- getPath("desktop"); // OK
70
- getPath("unknown"); // Type error
117
+ getPath("downloads"); // OK
118
+ getPath("desktop"); // OK
119
+ getPath("templates"); // OK
120
+ getPath("unknown"); // Type error
71
121
  ```
72
122
 
73
123
  ## API
@@ -90,8 +140,50 @@ Returns the path to the Pictures directory.
90
140
  ### `videos()`
91
141
  Returns the path to the Videos directory (Movies on macOS).
92
142
 
143
+ ### `templates()`
144
+ Returns the path to the Templates directory.
145
+
146
+ ### `publicshare()`
147
+ Returns the path to the Public Share directory.
148
+
93
149
  ### `getPath(name)`
94
- Returns the path to the specified directory. Valid names: `desktop`, `downloads`, `documents`, `music`, `pictures`, `videos`.
150
+ Returns the path to the specified user directory. Valid names: `desktop`, `downloads`, `documents`, `music`, `pictures`, `videos`, `templates`, `publicshare`.
151
+
152
+ ### Base Directories
153
+
154
+ #### `configDir()`
155
+ Returns the path to the config directory (`~/.config` on Linux, `~/Library/Application Support` on macOS, `%APPDATA%` on Windows).
156
+
157
+ #### `dataDir()`
158
+ Returns the path to the data directory (`~/.local/share` on Linux, `~/Library/Application Support` on macOS, `%LOCALAPPDATA%` on Windows).
159
+
160
+ #### `cacheDir()`
161
+ Returns the path to the cache directory (`~/.cache` on Linux, `~/Library/Caches` on macOS, `%LOCALAPPDATA%` on Windows).
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
+
169
+ #### `runtimeDir()`
170
+ Returns the path to the runtime directory (`$XDG_RUNTIME_DIR` on Linux), or `null` if unavailable.
171
+
172
+ #### `getBasePath(name)`
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`).
95
187
 
96
188
  ## License
97
189
 
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- type DirName = "desktop" | "downloads" | "documents" | "music" | "pictures" | "videos";
1
+ type DirName = "desktop" | "downloads" | "documents" | "music" | "pictures" | "videos" | "templates" | "publicshare";
2
2
 
3
3
  /** Returns the path to the Desktop directory. */
4
4
  export function desktop(): string;
@@ -18,9 +18,66 @@ export function pictures(): string;
18
18
  /** Returns the path to the Videos directory (Movies on macOS). */
19
19
  export function videos(): string;
20
20
 
21
+ /** Returns the path to the Templates directory. */
22
+ export function templates(): string;
23
+
24
+ /** Returns the path to the Public Share directory. */
25
+ export function publicshare(): string;
26
+
21
27
  /** Returns the path to the specified user directory. */
22
28
  export function getPath(name: DirName): string;
23
29
 
30
+ type BaseDirName = "config" | "data" | "cache" | "state" | "log" | "runtime";
31
+
32
+ /** Returns the path to the XDG config directory. */
33
+ export function configDir(): string;
34
+
35
+ /** Returns the path to the XDG data directory. */
36
+ export function dataDir(): string;
37
+
38
+ /** Returns the path to the XDG cache directory. */
39
+ export function cacheDir(): string;
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
+
47
+ /** Returns the path to the XDG runtime directory, or null if unavailable. */
48
+ export function runtimeDir(): string | null;
49
+
50
+ /** Returns the path to the specified base directory. */
51
+ export function getBasePath(name: "config"): string;
52
+ export function getBasePath(name: "data"): string;
53
+ export function getBasePath(name: "cache"): string;
54
+ export function getBasePath(name: "state"): string;
55
+ export function getBasePath(name: "log"): string;
56
+ export function getBasePath(name: "runtime"): string | null;
57
+ export function getBasePath(name: BaseDirName): string | null;
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
+
24
81
  /**
25
82
  * Reads an XDG user-dirs.dirs config and returns the directory for the given key.
26
83
  * @param key - XDG key (e.g. "XDG_DOWNLOAD_DIR")
@@ -41,7 +98,17 @@ declare const osUserDirs: typeof downloads & {
41
98
  music: typeof music;
42
99
  pictures: typeof pictures;
43
100
  videos: typeof videos;
101
+ templates: typeof templates;
102
+ publicshare: typeof publicshare;
44
103
  getPath: typeof getPath;
104
+ configDir: typeof configDir;
105
+ dataDir: typeof dataDir;
106
+ cacheDir: typeof cacheDir;
107
+ stateDir: typeof stateDir;
108
+ logDir: typeof logDir;
109
+ runtimeDir: typeof runtimeDir;
110
+ getBasePath: typeof getBasePath;
111
+ projectDirs: typeof projectDirs;
45
112
  getXDGUserDir: typeof getXDGUserDir;
46
113
  getXDGDownloadDir: typeof getXDGDownloadDir;
47
114
  };
package/index.js CHANGED
@@ -9,6 +9,8 @@ const XDG_KEYS = {
9
9
  music: "XDG_MUSIC_DIR",
10
10
  pictures: "XDG_PICTURES_DIR",
11
11
  videos: "XDG_VIDEOS_DIR",
12
+ templates: "XDG_TEMPLATES_DIR",
13
+ publicshare: "XDG_PUBLICSHARE_DIR",
12
14
  };
13
15
 
14
16
  const MACOS_DEFAULTS = {
@@ -18,6 +20,8 @@ const MACOS_DEFAULTS = {
18
20
  music: "Music",
19
21
  pictures: "Pictures",
20
22
  videos: "Movies",
23
+ templates: "Templates",
24
+ publicshare: "Public",
21
25
  };
22
26
 
23
27
  const DEFAULT_DIRS = {
@@ -27,6 +31,8 @@ const DEFAULT_DIRS = {
27
31
  music: "Music",
28
32
  pictures: "Pictures",
29
33
  videos: "Videos",
34
+ templates: "Templates",
35
+ publicshare: "Public",
30
36
  };
31
37
 
32
38
  function getXDGUserDir(key, configPath) {
@@ -74,6 +80,133 @@ function documents() { return resolve("documents"); }
74
80
  function music() { return resolve("music"); }
75
81
  function pictures() { return resolve("pictures"); }
76
82
  function videos() { return resolve("videos"); }
83
+ function templates() { return resolve("templates"); }
84
+ function publicshare() { return resolve("publicshare"); }
85
+
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
+ 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 },
93
+ };
94
+
95
+ function resolveBase(name) {
96
+ const cfg = BASE_DIR_CONFIG[name];
97
+ if (!cfg) {
98
+ throw new Error("Unknown base directory: " + name + ". Valid names: " + Object.keys(BASE_DIR_CONFIG).join(", "));
99
+ }
100
+
101
+ const homedir = os.homedir();
102
+ const platform = process.platform;
103
+
104
+ // On Linux, check the XDG environment variable first
105
+ if (platform === "linux") {
106
+ const envVal = process.env[cfg.env];
107
+ if (envVal) {
108
+ return path.resolve(envVal);
109
+ }
110
+ // Fall back to default suffix, or null for runtime
111
+ return cfg.linux ? path.join(homedir, cfg.linux) : null;
112
+ }
113
+
114
+ if (platform === "darwin") {
115
+ return cfg.darwin ? path.join(homedir, cfg.darwin) : null;
116
+ }
117
+
118
+ // Windows: read from environment variable, with hardcoded fallback
119
+ if (platform === "win32") {
120
+ if (cfg.win32) {
121
+ const winVal = process.env[cfg.win32];
122
+ if (winVal) {
123
+ return path.resolve(winVal);
124
+ }
125
+ // Fallback when env var is missing
126
+ if (cfg.win32 === "APPDATA") {
127
+ return path.join(homedir, "AppData", "Roaming");
128
+ }
129
+ if (cfg.win32 === "LOCALAPPDATA") {
130
+ return path.join(homedir, "AppData", "Local");
131
+ }
132
+ }
133
+ return null;
134
+ }
135
+
136
+ // Unknown platform: use XDG-style defaults (same as Linux without env var)
137
+ return cfg.linux ? path.join(homedir, cfg.linux) : null;
138
+ }
139
+
140
+ function configDir() { return resolveBase("config"); }
141
+ function dataDir() { return resolveBase("data"); }
142
+ function cacheDir() { return resolveBase("cache"); }
143
+ function stateDir() { return resolveBase("state"); }
144
+ function logDir() { return resolveBase("log"); }
145
+ function runtimeDir() { return resolveBase("runtime"); }
146
+
147
+ function getBasePath(name) {
148
+ return resolveBase(name);
149
+ }
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
+ }
77
210
 
78
211
  // Backward compatibility: require("os-user-dirs")() returns Downloads path
79
212
  module.exports = downloads;
@@ -84,6 +217,16 @@ module.exports.documents = documents;
84
217
  module.exports.music = music;
85
218
  module.exports.pictures = pictures;
86
219
  module.exports.videos = videos;
220
+ module.exports.templates = templates;
221
+ module.exports.publicshare = publicshare;
222
+ module.exports.configDir = configDir;
223
+ module.exports.dataDir = dataDir;
224
+ module.exports.cacheDir = cacheDir;
225
+ module.exports.stateDir = stateDir;
226
+ module.exports.logDir = logDir;
227
+ module.exports.runtimeDir = runtimeDir;
228
+ module.exports.getBasePath = getBasePath;
229
+ module.exports.projectDirs = projectDirs;
87
230
  module.exports.getXDGUserDir = getXDGUserDir;
88
231
 
89
232
  // Deprecated: kept for backward compatibility
package/index.mjs CHANGED
@@ -7,6 +7,16 @@ export const documents = osUserDirs.documents;
7
7
  export const music = osUserDirs.music;
8
8
  export const pictures = osUserDirs.pictures;
9
9
  export const videos = osUserDirs.videos;
10
+ export const templates = osUserDirs.templates;
11
+ export const publicshare = osUserDirs.publicshare;
10
12
  export const getPath = osUserDirs.getPath;
13
+ export const configDir = osUserDirs.configDir;
14
+ export const dataDir = osUserDirs.dataDir;
15
+ export const cacheDir = osUserDirs.cacheDir;
16
+ export const stateDir = osUserDirs.stateDir;
17
+ export const logDir = osUserDirs.logDir;
18
+ export const runtimeDir = osUserDirs.runtimeDir;
19
+ export const getBasePath = osUserDirs.getBasePath;
20
+ export const projectDirs = osUserDirs.projectDirs;
11
21
  export const getXDGUserDir = osUserDirs.getXDGUserDir;
12
22
  export const getXDGDownloadDir = osUserDirs.getXDGDownloadDir;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "os-user-dirs",
3
- "version": "2.2.0",
4
- "description": "Get OS-specific user directories (Downloads, Desktop, Documents, Music, Pictures, Videos) with zero dependencies.",
3
+ "version": "2.4.0",
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",
7
7
  "exports": {
@@ -32,6 +32,11 @@
32
32
  "music",
33
33
  "pictures",
34
34
  "videos",
35
+ "templates",
36
+ "public",
37
+ "config",
38
+ "data",
39
+ "cache",
35
40
  "xdg",
36
41
  "user-dirs",
37
42
  "platform-folders"