platformdirs 4.3.6-rc1

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/dist/unix.d.ts ADDED
@@ -0,0 +1,121 @@
1
+ /**
2
+ * @module
3
+ * Unix.
4
+ */
5
+ import { PlatformDirsABC } from "./api.js";
6
+ /**
7
+ * On Unix/Linux, we follow the [XDG Basedir
8
+ * Spec](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html).
9
+ *
10
+ * The spec allows overriding directories via environment variables. The
11
+ * examples shown are the default values, alongside the name of the environment
12
+ * variable that overrides them. Makes use of the
13
+ * {@link PlatformDirsABC.appname}, {@link PlatformDirsABC.version},
14
+ * {@link PlatformDirsABC.multipath}, {@link PlatformDirsABC.opinion},
15
+ * {@link PlatformDirsABC.ensureExists}.
16
+ */
17
+ export declare class Unix extends PlatformDirsABC {
18
+ /**
19
+ * @return data directory tied to the user, e.g. `~/.local/share/$appname/$version` or `$XDG_DATA_HOME/$appname/$version`
20
+ */
21
+ get userDataDir(): string;
22
+ /** @ignore @internal */
23
+ protected get _siteDataDirs(): string[];
24
+ /**
25
+ * @return data directories shared by users (if
26
+ * {@link PlatformDirsABC.multipath} is enabled and `XDG_DATA_DIRS` is and a
27
+ * multi path the response is also a multi path separated by the OS path
28
+ * separator), e.g.
29
+ * `/usr/local/share/$appname/$version:/usr/share/$appname/$version`
30
+ */
31
+ get siteDataDir(): string;
32
+ /**
33
+ * @return config directory tied to the user, e.g. `~/.config/$appname/$version` or `$XDG_CONFIG_HOME/$appname/$version`
34
+ */
35
+ get userConfigDir(): string;
36
+ /** @ignore @internal */
37
+ get _siteConfigDirs(): string[];
38
+ /**
39
+ * @return config directories shared by users (if
40
+ * {@link PlatformDirsABC.multipath} is enabled and `XDG_CONFIG_DIRS` is and
41
+ * a multi path the response is also a multi path separated by the OS path
42
+ * separator), e.g. `/etc/xdg/$appname/$version`
43
+ */
44
+ get siteConfigDir(): string;
45
+ /**
46
+ * @return cache directory tied to the user, e.g. `~/.cache/$appname/$version` or `$XDG_CACHE_HOME/$appname/$version`
47
+ */
48
+ get userCacheDir(): string;
49
+ /**
50
+ * @return cache directory shared by users, e.g. `/var/cache/$appname/$version`
51
+ */
52
+ get siteCacheDir(): string;
53
+ /**
54
+ * @return state directory tied to the user, e.g. `~/.local/state/$appname/$version` or `$XDG_STATE_HOME/$appname/$version`
55
+ */
56
+ get userStateDir(): string;
57
+ /**
58
+ * @return log directory tied to the user, same as `userCacheDir` if not opinionated else `log` in it.
59
+ */
60
+ get userLogDir(): string;
61
+ /**
62
+ * @return documents directory tied to the user, e.g. `~/Documents`
63
+ */
64
+ get userDocumentsDir(): string;
65
+ /**
66
+ * @return downloads directory tied to the user, e.g. `~/Downloads`
67
+ */
68
+ get userDownloadsDir(): string;
69
+ /**
70
+ * @return pictures directory tied to the user, e.g. `~/Pictures`
71
+ */
72
+ get userPicturesDir(): string;
73
+ /**
74
+ * @return videos directory tied to the user, e.g. `~/Videos`
75
+ */
76
+ get userVideosDir(): string;
77
+ /**
78
+ * @return music directory tied to the user, e.g. `~/Music`
79
+ */
80
+ get userMusicDir(): string;
81
+ /**
82
+ * @return desktop directory tied to the user, e.g. `~/Desktop`
83
+ */
84
+ get userDesktopDir(): string;
85
+ /**
86
+ * @return runtime directory tied to the user, e.g. `/run/user/$(id -u)/$appname/$version` or `$XDG_RUNTIME_DIR/$appname/$version`.
87
+ *
88
+ * For FreeBSD/OpenBSD/NetBSD, it would return `/var/run/user/$(id -u)/$appname/$version` if it exists, otherwise `/tmp/runtime-$(id -u)/$appname/$version`, if `XDG_RUNTIME_DIR` is not set.
89
+ */
90
+ get userRuntimeDir(): string;
91
+ /**
92
+ * @return runtime directory shared by users, e.g. `/run/$appname/$version` or `$XDG_RUNTIME_DIR/$appname/$version`.
93
+ *
94
+ * Note that this behaves almost exactly like `userRuntimeDir` if `XDG_RUNTIME_DIR` is set, but will fall back to paths associated to the root user isntead of a regular logged-in user if it's not set.
95
+ *
96
+ * If you wish to ensure that a logged-in user path is returned e.g. `/run/user/0`, use `userRuntimeDir` instead.
97
+ *
98
+ * For FreeBSD/OpenBSD/NetBSD, it would return `/var/run/$appname/$version` if `XDG_RUNTIME_DIR` is not set.
99
+ */
100
+ get siteRuntimeDir(): string;
101
+ /**
102
+ * @return data path shared by users. Only return the first item, even if `multipath` is set to `true`.
103
+ */
104
+ get siteDataPath(): string;
105
+ /**
106
+ * @return config path shared by users. Only return the first item, even if `multipath` is set to `true`.
107
+ */
108
+ get siteConfigPath(): string;
109
+ /**
110
+ * @return cache path shared by users. Only return the first item, even if `multipath` is set to `true`.
111
+ */
112
+ get siteCachePath(): string;
113
+ /**
114
+ * @yields all user and site configuation directories
115
+ */
116
+ iterConfigDirs(): Generator<string>;
117
+ /**
118
+ * @yields all user and site data directories
119
+ */
120
+ iterDataDirs(): Generator<string>;
121
+ }
package/dist/unix.js ADDED
@@ -0,0 +1,277 @@
1
+ /**
2
+ * @module
3
+ * Unix.
4
+ */
5
+ import * as fs from "node:fs";
6
+ import * as os from "node:os";
7
+ import * as path from "node:path";
8
+ import process from "node:process";
9
+ import { PlatformDirsABC } from "./api.js";
10
+ function expanduser(path2) {
11
+ return path2.replace(/^~/, os.homedir());
12
+ }
13
+ function getuid() {
14
+ if (!process.getuid) {
15
+ throw new Error("should only be used on Unix");
16
+ }
17
+ return process.getuid();
18
+ }
19
+ /**
20
+ * On Unix/Linux, we follow the [XDG Basedir
21
+ * Spec](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html).
22
+ *
23
+ * The spec allows overriding directories via environment variables. The
24
+ * examples shown are the default values, alongside the name of the environment
25
+ * variable that overrides them. Makes use of the
26
+ * {@link PlatformDirsABC.appname}, {@link PlatformDirsABC.version},
27
+ * {@link PlatformDirsABC.multipath}, {@link PlatformDirsABC.opinion},
28
+ * {@link PlatformDirsABC.ensureExists}.
29
+ */
30
+ export class Unix extends PlatformDirsABC {
31
+ /**
32
+ * @return data directory tied to the user, e.g. `~/.local/share/$appname/$version` or `$XDG_DATA_HOME/$appname/$version`
33
+ */
34
+ get userDataDir() {
35
+ let path2 = process.env.XDG_DATA_HOME ?? "";
36
+ if (!path2.trim()) {
37
+ path2 = expanduser("~/.local/share");
38
+ }
39
+ return this._appendAppNameAndVersion(path2);
40
+ }
41
+ /** @ignore @internal */
42
+ get _siteDataDirs() {
43
+ let path2 = process.env.XDG_DATA_DIRS ?? "";
44
+ if (!path2.trim()) {
45
+ path2 = `/usr/local/share${path.delimiter}/usr/share`;
46
+ }
47
+ return path2
48
+ .split(path.delimiter)
49
+ .map((p) => this._appendAppNameAndVersion(p));
50
+ }
51
+ /**
52
+ * @return data directories shared by users (if
53
+ * {@link PlatformDirsABC.multipath} is enabled and `XDG_DATA_DIRS` is and a
54
+ * multi path the response is also a multi path separated by the OS path
55
+ * separator), e.g.
56
+ * `/usr/local/share/$appname/$version:/usr/share/$appname/$version`
57
+ */
58
+ get siteDataDir() {
59
+ const dirs = this._siteDataDirs;
60
+ if (!this.multipath) {
61
+ return dirs[0];
62
+ }
63
+ return dirs.join(path.delimiter);
64
+ }
65
+ /**
66
+ * @return config directory tied to the user, e.g. `~/.config/$appname/$version` or `$XDG_CONFIG_HOME/$appname/$version`
67
+ */
68
+ get userConfigDir() {
69
+ let path2 = process.env.XDG_CONFIG_HOME ?? "";
70
+ if (!path2.trim()) {
71
+ path2 = expanduser("~/.config");
72
+ }
73
+ return this._appendAppNameAndVersion(path2);
74
+ }
75
+ /** @ignore @internal */
76
+ get _siteConfigDirs() {
77
+ let path2 = process.env.XDG_CONFIG_DIRS ?? "";
78
+ if (!path2.trim()) {
79
+ path2 = "/etc/xdg";
80
+ }
81
+ return path2
82
+ .split(path.delimiter)
83
+ .map((p) => this._appendAppNameAndVersion(p));
84
+ }
85
+ /**
86
+ * @return config directories shared by users (if
87
+ * {@link PlatformDirsABC.multipath} is enabled and `XDG_CONFIG_DIRS` is and
88
+ * a multi path the response is also a multi path separated by the OS path
89
+ * separator), e.g. `/etc/xdg/$appname/$version`
90
+ */
91
+ get siteConfigDir() {
92
+ const dirs = this._siteConfigDirs;
93
+ if (!this.multipath) {
94
+ return dirs[0];
95
+ }
96
+ return dirs.join(path.delimiter);
97
+ }
98
+ /**
99
+ * @return cache directory tied to the user, e.g. `~/.cache/$appname/$version` or `$XDG_CACHE_HOME/$appname/$version`
100
+ */
101
+ get userCacheDir() {
102
+ let path2 = process.env.XDG_CACHE_HOME ?? "";
103
+ if (!path2.trim()) {
104
+ path2 = expanduser("~/.cache");
105
+ }
106
+ return this._appendAppNameAndVersion(path2);
107
+ }
108
+ /**
109
+ * @return cache directory shared by users, e.g. `/var/cache/$appname/$version`
110
+ */
111
+ get siteCacheDir() {
112
+ return this._appendAppNameAndVersion("/var/cache");
113
+ }
114
+ /**
115
+ * @return state directory tied to the user, e.g. `~/.local/state/$appname/$version` or `$XDG_STATE_HOME/$appname/$version`
116
+ */
117
+ get userStateDir() {
118
+ let path2 = process.env.XDG_STATE_HOME ?? "";
119
+ if (!path2.trim()) {
120
+ path2 = expanduser("~/.local/state");
121
+ }
122
+ return this._appendAppNameAndVersion(path2);
123
+ }
124
+ /**
125
+ * @return log directory tied to the user, same as `userCacheDir` if not opinionated else `log` in it.
126
+ */
127
+ get userLogDir() {
128
+ let path2 = this.userStateDir;
129
+ if (this.opinion) {
130
+ path2 = path.join(path2, "log");
131
+ this._optionallyCreateDirectory(path2);
132
+ }
133
+ return path2;
134
+ }
135
+ /**
136
+ * @return documents directory tied to the user, e.g. `~/Documents`
137
+ */
138
+ get userDocumentsDir() {
139
+ return getUserMediaDir("XDG_DOCUMENTS_DIR", "~/Documents");
140
+ }
141
+ /**
142
+ * @return downloads directory tied to the user, e.g. `~/Downloads`
143
+ */
144
+ get userDownloadsDir() {
145
+ return getUserMediaDir("XDG_DOWNLOAD_DIR", "~/Downloads");
146
+ }
147
+ /**
148
+ * @return pictures directory tied to the user, e.g. `~/Pictures`
149
+ */
150
+ get userPicturesDir() {
151
+ return getUserMediaDir("XDG_PICTURES_DIR", "~/Pictures");
152
+ }
153
+ /**
154
+ * @return videos directory tied to the user, e.g. `~/Videos`
155
+ */
156
+ get userVideosDir() {
157
+ return getUserMediaDir("XDG_VIDEOS_DIR", "~/Videos");
158
+ }
159
+ /**
160
+ * @return music directory tied to the user, e.g. `~/Music`
161
+ */
162
+ get userMusicDir() {
163
+ return getUserMediaDir("XDG_MUSIC_DIR", "~/Music");
164
+ }
165
+ /**
166
+ * @return desktop directory tied to the user, e.g. `~/Desktop`
167
+ */
168
+ get userDesktopDir() {
169
+ return getUserMediaDir("XDG_DESKTOP_DIR", "~/Desktop");
170
+ }
171
+ /**
172
+ * @return runtime directory tied to the user, e.g. `/run/user/$(id -u)/$appname/$version` or `$XDG_RUNTIME_DIR/$appname/$version`.
173
+ *
174
+ * For FreeBSD/OpenBSD/NetBSD, it would return `/var/run/user/$(id -u)/$appname/$version` if it exists, otherwise `/tmp/runtime-$(id -u)/$appname/$version`, if `XDG_RUNTIME_DIR` is not set.
175
+ */
176
+ get userRuntimeDir() {
177
+ let path2 = process.env.XDG_RUNTIME_DIR ?? "";
178
+ if (!path2.trim()) {
179
+ if (process.platform === "freebsd" ||
180
+ process.platform === "netbsd" ||
181
+ process.platform === "openbsd") {
182
+ path2 = `/var/run/user/${getuid()}`;
183
+ if (!fs.existsSync(path2)) {
184
+ path2 = `/tmp/runtime-${getuid()}`;
185
+ }
186
+ }
187
+ else {
188
+ path2 = `/run/user/${getuid()}`;
189
+ }
190
+ }
191
+ return this._appendAppNameAndVersion(path2);
192
+ }
193
+ /**
194
+ * @return runtime directory shared by users, e.g. `/run/$appname/$version` or `$XDG_RUNTIME_DIR/$appname/$version`.
195
+ *
196
+ * Note that this behaves almost exactly like `userRuntimeDir` if `XDG_RUNTIME_DIR` is set, but will fall back to paths associated to the root user isntead of a regular logged-in user if it's not set.
197
+ *
198
+ * If you wish to ensure that a logged-in user path is returned e.g. `/run/user/0`, use `userRuntimeDir` instead.
199
+ *
200
+ * For FreeBSD/OpenBSD/NetBSD, it would return `/var/run/$appname/$version` if `XDG_RUNTIME_DIR` is not set.
201
+ */
202
+ get siteRuntimeDir() {
203
+ let path2 = process.env.XDG_RUNTIME_DIR ?? "";
204
+ if (!path2.trim()) {
205
+ if (process.platform === "freebsd" ||
206
+ process.platform === "netbsd" ||
207
+ process.platform === "openbsd") {
208
+ path2 = "/var/run";
209
+ }
210
+ else {
211
+ path2 = "/run";
212
+ }
213
+ }
214
+ return this._appendAppNameAndVersion(path2);
215
+ }
216
+ /**
217
+ * @return data path shared by users. Only return the first item, even if `multipath` is set to `true`.
218
+ */
219
+ get siteDataPath() {
220
+ return this._firstItemAsPathIfMultipath(this.siteDataDir);
221
+ }
222
+ /**
223
+ * @return config path shared by users. Only return the first item, even if `multipath` is set to `true`.
224
+ */
225
+ get siteConfigPath() {
226
+ return this._firstItemAsPathIfMultipath(this.siteConfigDir);
227
+ }
228
+ /**
229
+ * @return cache path shared by users. Only return the first item, even if `multipath` is set to `true`.
230
+ */
231
+ get siteCachePath() {
232
+ return this._firstItemAsPathIfMultipath(this.siteCacheDir);
233
+ }
234
+ /**
235
+ * @yields all user and site configuation directories
236
+ */
237
+ *iterConfigDirs() {
238
+ yield this.userConfigDir;
239
+ yield* this._siteConfigDirs;
240
+ }
241
+ /**
242
+ * @yields all user and site data directories
243
+ */
244
+ *iterDataDirs() {
245
+ yield this.userDataDir;
246
+ yield* this._siteDataDirs;
247
+ }
248
+ }
249
+ function getUserMediaDir(envVar, fallbackTildePath) {
250
+ let mediaDir = getUserDirsFolder(envVar);
251
+ if (mediaDir === undefined) {
252
+ mediaDir = (process.env[envVar] ?? "").trim();
253
+ if (!mediaDir) {
254
+ mediaDir = expanduser(fallbackTildePath);
255
+ }
256
+ }
257
+ return mediaDir;
258
+ }
259
+ /**
260
+ * Return directory from user-dirs.dirs config file.
261
+ *
262
+ * See https://freedesktop.org/wiki/Software/xdg-user-dirs/.
263
+ */
264
+ function getUserDirsFolder(key) {
265
+ // const userDirsConfigPath = path.join(new Unix().userConfigDir, "user-dirs.dirs");
266
+ // if (fs.existsSync(userDirsConfigPath)) {
267
+ // const parser = new ConfigParser()
268
+ // const read = fs.readFileSync(userDirsConfigPath, "utf-8")
269
+ // parser.readString(`[top]\n${read}`)
270
+ // if (parser.get("top", key) === undefined) {
271
+ // return undefined
272
+ // }
273
+ // const path2 = parser.get("top", key).trim()
274
+ // return path2.replace("$HOME", expanduser("~"))
275
+ // }
276
+ return undefined;
277
+ }
@@ -0,0 +1,2 @@
1
+ export declare const version: string;
2
+ export declare const versionTuple: (string | number)[];
@@ -0,0 +1,24 @@
1
+ import packageJSON from "../package.json" with { type: "json" };
2
+ /**
3
+ * And one with numbered capture groups instead (so cg1 = major, cg2 = minor,
4
+ * cg3 = patch, cg4 = prerelease and cg5 = buildmetadata) that is compatible
5
+ * with ECMA Script (JavaScript), PCRE (Perl Compatible Regular Expressions,
6
+ * i.e. Perl, PHP and R), Python and Go.
7
+ *
8
+ * See: https://regex101.com/r/vkijKf/1/
9
+ *
10
+ * @license CC-BY-3.0
11
+ * @see https://semver.org/
12
+ */
13
+ const semverRegex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
14
+ export const version = packageJSON.version;
15
+ const match = packageJSON.version.match(semverRegex);
16
+ if (!match) {
17
+ throw new Error("unreachable");
18
+ }
19
+ export const versionTuple = [
20
+ +match[0],
21
+ +match[1],
22
+ +match[2],
23
+ ...match.slice(3),
24
+ ];
@@ -0,0 +1,83 @@
1
+ /**
2
+ * @module
3
+ * Windows.
4
+ */
5
+ import { PlatformDirsABC } from "./api.js";
6
+ /**
7
+ * [MSDN on where to store app data
8
+ * files](https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid)
9
+ *
10
+ * Makes use of the {@link PlatformDirsABC.appname},
11
+ * {@link PlatformDirsABC.appauthor}, {@link PlatformDirsABC.version},
12
+ * {@link PlatformDirsABC.roaming}, {@link PlatformDirsABC.opinion},
13
+ * {@link PlatformDirsABC.ensureExists}.
14
+ */
15
+ export declare class Windows extends PlatformDirsABC {
16
+ /**
17
+ * @return data directory tied to the user, e.g. `%USERPROFILE%\AppData\Local\$appauthor\$appname` (not roaming) or `%USERPROFILE%\AppData\Roaming\$appauthor\$appname` (roaming)
18
+ */
19
+ get userDataDir(): string;
20
+ protected _appendParts(path2: string, { opinionValue }?: {
21
+ opinionValue?: string | undefined;
22
+ }): string;
23
+ /**
24
+ * @return data directory shared by users, e.g. `C:\ProgramData\$appauthor\$appname`
25
+ */
26
+ get siteDataDir(): string;
27
+ /**
28
+ * @return config directory tied to the user, same as `userDataDir`
29
+ */
30
+ get userConfigDir(): string;
31
+ /**
32
+ * @return config directory shared by users, same as `siteDataDir`
33
+ */
34
+ get siteConfigDir(): string;
35
+ /**
36
+ * @return cache directory tied to the user (if opinionated with `Cache` folder within `$appname`) e.g. `%USERPROFILE%\AppData\Local\$appauthor\$appname\Cache\$version`
37
+ */
38
+ get userCacheDir(): string;
39
+ /**
40
+ * @return cache directory shared by users, e.g. `C:\ProgramData\$appauthor\$appname\Cache\$version`
41
+ */
42
+ get siteCacheDir(): string;
43
+ /**
44
+ * @return state directory tied to the user, same as `userDataDir`
45
+ */
46
+ get userStateDir(): string;
47
+ /**
48
+ * @return log directory tied to the user, same as `userCacheDir` if not opinionated else `log` in it
49
+ */
50
+ get userLogDir(): string;
51
+ /**
52
+ * @return documents directory tied to the user, e.g. `%USERPROFILE%\Documents`
53
+ */
54
+ get userDocumentsDir(): string;
55
+ /**
56
+ * @return downloads directory tied to the user, e.g. `%USERPROFILE%\Downloads`
57
+ */
58
+ get userDownloadsDir(): string;
59
+ /**
60
+ * @return pictures directory tied to the user, e.g. `%USERPROFILE%\Pictures`
61
+ */
62
+ get userPicturesDir(): string;
63
+ /**
64
+ * @return videos directory tied to the user, e.g. `%USERPROFILE%\Videos`
65
+ */
66
+ get userVideosDir(): string;
67
+ /**
68
+ * @return music directory tied to the user, e.g. `%USERPROFILE%\Music`
69
+ */
70
+ get userMusicDir(): string;
71
+ /**
72
+ * @return desktop directory tied to the user, e.g. `%USERPROFILE%\Desktop`
73
+ */
74
+ get userDesktopDir(): string;
75
+ /**
76
+ * @return runtime directory tied to the user, e.g. `%USERPROFILE%\AppData\Local\Temp\$appauthor\$appname`
77
+ */
78
+ get userRuntimeDir(): string;
79
+ /**
80
+ * @return runtime directory shared by users, same as `userRuntimeDir`
81
+ */
82
+ get siteRuntimeDir(): string;
83
+ }