os-user-dirs 2.1.0 → 2.3.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, runtime) with zero dependencies.
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,41 @@ 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, 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
+ runtimeDir();
57
+ //=> '/run/user/1000' (or null)
58
+
59
+ getBasePath("config");
60
+ //=> '/home/user/.config'
61
+ ```
62
+
39
63
  ### CommonJS
40
64
 
41
65
  ```javascript
42
- const { downloads, desktop, documents, music, pictures, videos, getPath } = require("os-user-dirs");
66
+ const { downloads, desktop, documents, music, pictures, videos, templates, publicshare, getPath } = require("os-user-dirs");
43
67
 
44
68
  downloads();
45
69
  //=> '/home/user/Downloads'
@@ -65,9 +89,10 @@ Full type definitions are included. `getPath()` accepts a union type for auto-co
65
89
  ```typescript
66
90
  import { getPath } from "os-user-dirs";
67
91
 
68
- getPath("downloads"); // OK
69
- getPath("desktop"); // OK
70
- getPath("unknown"); // Type error
92
+ getPath("downloads"); // OK
93
+ getPath("desktop"); // OK
94
+ getPath("templates"); // OK
95
+ getPath("unknown"); // Type error
71
96
  ```
72
97
 
73
98
  ## API
@@ -90,8 +115,31 @@ Returns the path to the Pictures directory.
90
115
  ### `videos()`
91
116
  Returns the path to the Videos directory (Movies on macOS).
92
117
 
118
+ ### `templates()`
119
+ Returns the path to the Templates directory.
120
+
121
+ ### `publicshare()`
122
+ Returns the path to the Public Share directory.
123
+
93
124
  ### `getPath(name)`
94
- Returns the path to the specified directory. Valid names: `desktop`, `downloads`, `documents`, `music`, `pictures`, `videos`.
125
+ Returns the path to the specified user directory. Valid names: `desktop`, `downloads`, `documents`, `music`, `pictures`, `videos`, `templates`, `publicshare`.
126
+
127
+ ### Base Directories
128
+
129
+ #### `configDir()`
130
+ Returns the path to the config directory (`~/.config` on Linux, `~/Library/Application Support` on macOS, `%APPDATA%` on Windows).
131
+
132
+ #### `dataDir()`
133
+ Returns the path to the data directory (`~/.local/share` on Linux, `~/Library/Application Support` on macOS, `%LOCALAPPDATA%` on Windows).
134
+
135
+ #### `cacheDir()`
136
+ Returns the path to the cache directory (`~/.cache` on Linux, `~/Library/Caches` on macOS, `%LOCALAPPDATA%` on Windows).
137
+
138
+ #### `runtimeDir()`
139
+ Returns the path to the runtime directory (`$XDG_RUNTIME_DIR` on Linux), or `null` if unavailable.
140
+
141
+ #### `getBasePath(name)`
142
+ Returns the path to the specified base directory. Valid names: `config`, `data`, `cache`, `runtime`.
95
143
 
96
144
  ## License
97
145
 
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,36 @@ 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" | "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 runtime directory, or null if unavailable. */
42
+ export function runtimeDir(): string | null;
43
+
44
+ /** Returns the path to the specified base directory. */
45
+ export function getBasePath(name: "config"): string;
46
+ export function getBasePath(name: "data"): string;
47
+ export function getBasePath(name: "cache"): string;
48
+ export function getBasePath(name: "runtime"): string | null;
49
+ export function getBasePath(name: BaseDirName): string | null;
50
+
24
51
  /**
25
52
  * Reads an XDG user-dirs.dirs config and returns the directory for the given key.
26
53
  * @param key - XDG key (e.g. "XDG_DOWNLOAD_DIR")
@@ -41,7 +68,14 @@ declare const osUserDirs: typeof downloads & {
41
68
  music: typeof music;
42
69
  pictures: typeof pictures;
43
70
  videos: typeof videos;
71
+ templates: typeof templates;
72
+ publicshare: typeof publicshare;
44
73
  getPath: typeof getPath;
74
+ configDir: typeof configDir;
75
+ dataDir: typeof dataDir;
76
+ cacheDir: typeof cacheDir;
77
+ runtimeDir: typeof runtimeDir;
78
+ getBasePath: typeof getBasePath;
45
79
  getXDGUserDir: typeof getXDGUserDir;
46
80
  getXDGDownloadDir: typeof getXDGDownloadDir;
47
81
  };
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,69 @@ 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
+ runtime: { env: "XDG_RUNTIME_DIR", linux: null, darwin: null, win32: null },
91
+ };
92
+
93
+ function resolveBase(name) {
94
+ const cfg = BASE_DIR_CONFIG[name];
95
+ if (!cfg) {
96
+ throw new Error("Unknown base directory: " + name + ". Valid names: " + Object.keys(BASE_DIR_CONFIG).join(", "));
97
+ }
98
+
99
+ const homedir = os.homedir();
100
+ const platform = process.platform;
101
+
102
+ // On Linux, check the XDG environment variable first
103
+ if (platform === "linux") {
104
+ const envVal = process.env[cfg.env];
105
+ if (envVal) {
106
+ return path.resolve(envVal);
107
+ }
108
+ // Fall back to default suffix, or null for runtime
109
+ return cfg.linux ? path.join(homedir, cfg.linux) : null;
110
+ }
111
+
112
+ if (platform === "darwin") {
113
+ return cfg.darwin ? path.join(homedir, cfg.darwin) : null;
114
+ }
115
+
116
+ // Windows: read from environment variable, with hardcoded fallback
117
+ if (platform === "win32") {
118
+ if (cfg.win32) {
119
+ const winVal = process.env[cfg.win32];
120
+ if (winVal) {
121
+ return path.resolve(winVal);
122
+ }
123
+ // Fallback when env var is missing
124
+ if (cfg.win32 === "APPDATA") {
125
+ return path.join(homedir, "AppData", "Roaming");
126
+ }
127
+ if (cfg.win32 === "LOCALAPPDATA") {
128
+ return path.join(homedir, "AppData", "Local");
129
+ }
130
+ }
131
+ return null;
132
+ }
133
+
134
+ // Unknown platform: use XDG-style defaults (same as Linux without env var)
135
+ return cfg.linux ? path.join(homedir, cfg.linux) : null;
136
+ }
137
+
138
+ function configDir() { return resolveBase("config"); }
139
+ function dataDir() { return resolveBase("data"); }
140
+ function cacheDir() { return resolveBase("cache"); }
141
+ function runtimeDir() { return resolveBase("runtime"); }
142
+
143
+ function getBasePath(name) {
144
+ return resolveBase(name);
145
+ }
77
146
 
78
147
  // Backward compatibility: require("os-user-dirs")() returns Downloads path
79
148
  module.exports = downloads;
@@ -84,6 +153,13 @@ module.exports.documents = documents;
84
153
  module.exports.music = music;
85
154
  module.exports.pictures = pictures;
86
155
  module.exports.videos = videos;
156
+ module.exports.templates = templates;
157
+ module.exports.publicshare = publicshare;
158
+ module.exports.configDir = configDir;
159
+ module.exports.dataDir = dataDir;
160
+ module.exports.cacheDir = cacheDir;
161
+ module.exports.runtimeDir = runtimeDir;
162
+ module.exports.getBasePath = getBasePath;
87
163
  module.exports.getXDGUserDir = getXDGUserDir;
88
164
 
89
165
  // Deprecated: kept for backward compatibility
package/index.mjs CHANGED
@@ -7,6 +7,13 @@ 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 runtimeDir = osUserDirs.runtimeDir;
17
+ export const getBasePath = osUserDirs.getBasePath;
11
18
  export const getXDGUserDir = osUserDirs.getXDGUserDir;
12
19
  export const getXDGDownloadDir = osUserDirs.getXDGDownloadDir;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "os-user-dirs",
3
- "version": "2.1.0",
4
- "description": "Get OS-specific user directories (Downloads, Desktop, Documents, Music, Pictures, Videos) with zero dependencies.",
3
+ "version": "2.3.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"