platformdirs 4.3.6 → 4.3.8-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/src/index.js ADDED
@@ -0,0 +1,656 @@
1
+ /**
2
+ * @module
3
+ *
4
+ * platformdirs is a library to determine platform-specific system directories. This includes directories where to place cache files, user data, configuration, etc.
5
+ *
6
+ * The source code and issue tracker are both hosted on [GitHub](https://github.com/jcbhmr/platformdirs.js).
7
+ *
8
+ * Utilities for determining application-specific dirs.
9
+ *
10
+ * See https://github.com/platformdirs/platformdirs for details and usage.
11
+ */
12
+
13
+ import process from "node:process";
14
+ /**
15
+ * @import { Android } from "./android.js";
16
+ * @import { PlatformDirsABC } from "./api.js";
17
+ * @import { MacOS } from "./macos.js";
18
+ * @import { Unix } from "./unix.js";
19
+ * @import { Windows } from "./windows.js";
20
+ */
21
+
22
+ /** @type {typeof Windows | typeof MacOS | typeof Unix} */
23
+ let Result;
24
+ if (process.platform === "win32") {
25
+ ({ Windows: Result } = await import("./windows.js"));
26
+ } else if (process.platform === "darwin") {
27
+ ({ MacOS: Result } = await import("./macos.js"));
28
+ } else {
29
+ ({ Unix: Result } = await import("./unix.js"));
30
+ }
31
+
32
+ /** @return {Promise<typeof PlatformDirsABC>} */
33
+ async function setPlatformDirClass() {
34
+ if (
35
+ process.env.ANDROID_DATA === "/data" &&
36
+ process.env.ANDROID_ROOT === "/system"
37
+ ) {
38
+ if (process.env.SHELL || process.env.PREFIX) {
39
+ return Result;
40
+ }
41
+
42
+ const { _androidFolder } = await import("./android.js");
43
+
44
+ if (_androidFolder() != null) {
45
+ const { Android } = await import("./android.js");
46
+
47
+ return Android;
48
+ }
49
+ }
50
+
51
+ return Result;
52
+ }
53
+
54
+ /** @typedef {Windows | MacOS | Unix | Android} PlatformDirs */
55
+
56
+ /** @type {typeof Windows | typeof MacOS | typeof Unix | typeof Android} */
57
+ export const PlatformDirs = await setPlatformDirClass();
58
+
59
+ export const AppDirs = PlatformDirs;
60
+
61
+ /**
62
+ *
63
+ * @param {string} [appname] See {@link PlatformDirs.appname}
64
+ * @param {string | false} [appauthor] See {@link PlatformDirs.appauthor}
65
+ * @param {string} [version] See {@link PlatformDirs.version}
66
+ * @param {boolean} [roaming=false] See {@link PlatformDirs.roaming}
67
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
68
+ * @returns {string} data directory tied to the user
69
+ */
70
+ export function userDataDir(
71
+ appname,
72
+ appauthor,
73
+ version,
74
+ roaming = false,
75
+ ensureExists = false,
76
+ ) {
77
+ return new PlatformDirs(
78
+ appname,
79
+ appauthor,
80
+ version,
81
+ roaming,
82
+ undefined,
83
+ undefined,
84
+ ensureExists,
85
+ ).userDataDir;
86
+ }
87
+
88
+ /**
89
+ *
90
+ * @param {string} [appname] See {@link PlatformDirs.appname}
91
+ * @param {string | false} [appauthor] See {@link PlatformDirs.appauthor}
92
+ * @param {string} [version] See {@link PlatformDirs.version}
93
+ * @param {boolean} [multipath=false] See {@link PlatformDirs.multipath}
94
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
95
+ * @returns {string} data directory shared by users
96
+ */
97
+ export function siteDataDir(
98
+ appname,
99
+ appauthor,
100
+ version,
101
+ multipath = false,
102
+ ensureExists = false,
103
+ ) {
104
+ return new PlatformDirs(
105
+ appname,
106
+ appauthor,
107
+ version,
108
+ undefined,
109
+ multipath,
110
+ undefined,
111
+ ensureExists,
112
+ ).siteDataDir;
113
+ }
114
+
115
+ /**
116
+ * @param {string} [appname] See {@link PlatformDirs.appname}
117
+ * @param {string | false} [appauthor] See {@link PlatformDirs.appauthor}
118
+ * @param {string} [version] See {@link PlatformDirs.version}
119
+ * @param {boolean} [roaming=false] See {@link PlatformDirs.roaming}
120
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
121
+ * @returns {string} config directory tied to the user
122
+ */
123
+ export function userConfigDir(
124
+ appname,
125
+ appauthor,
126
+ version,
127
+ roaming = false,
128
+ ensureExists = false,
129
+ ) {
130
+ return new PlatformDirs(
131
+ appname,
132
+ appauthor,
133
+ version,
134
+ roaming,
135
+ undefined,
136
+ undefined,
137
+ ensureExists,
138
+ ).userConfigDir;
139
+ }
140
+
141
+ /**
142
+ * @param {string} [appname] See {@link PlatformDirs.appname}
143
+ * @param {string | false} [appauthor] See {@link PlatformDirs.appauthor}
144
+ * @param {string} [version] See {@link PlatformDirs.version}
145
+ * @param {boolean} [multipath=false] See {@link PlatformDirs.multipath}
146
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
147
+ * @returns {string} config directory shared by users
148
+ */
149
+ export function siteConfigDir(
150
+ appname,
151
+ appauthor,
152
+ version,
153
+ multipath = false,
154
+ ensureExists = false,
155
+ ) {
156
+ return new PlatformDirs(
157
+ appname,
158
+ appauthor,
159
+ version,
160
+ undefined,
161
+ multipath,
162
+ undefined,
163
+ ensureExists,
164
+ ).siteConfigDir;
165
+ }
166
+
167
+ /**
168
+ * @param {string} [appname] See {@link PlatformDirs.appname}
169
+ * @param {string | false} [appauthor] See {@link PlatformDirs.appauthor}
170
+ * @param {string} [version] See {@link PlatformDirs.version}
171
+ * @param {boolean} [opinion=true] See {@link PlatformDirs.opinion}
172
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
173
+ * @returns {string} cache directory tied to the user
174
+ */
175
+ export function userCacheDir(
176
+ appname,
177
+ appauthor,
178
+ version,
179
+ opinion = true,
180
+ ensureExists = false,
181
+ ) {
182
+ return new PlatformDirs(
183
+ appname,
184
+ appauthor,
185
+ version,
186
+ undefined,
187
+ undefined,
188
+ opinion,
189
+ ensureExists,
190
+ ).userCacheDir;
191
+ }
192
+
193
+ /**
194
+ * @param {string} [appname] See {@link PlatformDirs.appname}
195
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
196
+ * @param {string} [version] See {@link PlatformDirs.version}
197
+ * @param {boolean} [opinion=true] See {@link PlatformDirs.opinion}
198
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
199
+ * @returns {string} cache directory tied to the user
200
+ */
201
+ export function siteCacheDir(
202
+ appname,
203
+ appauthor,
204
+ version,
205
+ opinion = true,
206
+ ensureExists = false,
207
+ ) {
208
+ return new PlatformDirs(
209
+ appname,
210
+ appauthor,
211
+ version,
212
+ undefined,
213
+ undefined,
214
+ opinion,
215
+ ensureExists,
216
+ ).siteCacheDir;
217
+ }
218
+
219
+ /**
220
+ * @param {string} [appname] See {@link PlatformDirs.appname}
221
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
222
+ * @param {string} [version] See {@link PlatformDirs.version}
223
+ * @param {boolean} [roaming=false] See {@link PlatformDirs.roaming}
224
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
225
+ * @returns {string} state directory tied to the user
226
+ */
227
+ export function userStateDir(
228
+ appname,
229
+ appauthor,
230
+ version,
231
+ roaming = false,
232
+ ensureExists = false,
233
+ ) {
234
+ return new PlatformDirs(
235
+ appname,
236
+ appauthor,
237
+ version,
238
+ roaming,
239
+ undefined,
240
+ undefined,
241
+ ensureExists,
242
+ ).userStateDir;
243
+ }
244
+
245
+ /**
246
+ * @param {string} [appname] See {@link PlatformDirs.appname}
247
+ * @param {string} [appauthor] See {@link PlatformDirs.appauthor}
248
+ * @param {string} [version] See {@link PlatformDirs.version}
249
+ * @param {boolean} [opinion=true] See {@link PlatformDirs.opinion}
250
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
251
+ * @returns {string} log directory tied to the user
252
+ */
253
+ export function userLogDir(
254
+ appname,
255
+ appauthor,
256
+ version,
257
+ opinion = true,
258
+ ensureExists = false,
259
+ ) {
260
+ return new PlatformDirs(
261
+ appname,
262
+ appauthor,
263
+ version,
264
+ undefined,
265
+ undefined,
266
+ opinion,
267
+ ensureExists,
268
+ ).userLogDir;
269
+ }
270
+
271
+ /**
272
+ * @returns {string} documents directory tied to the user
273
+ */
274
+ export function userDocumentsDir() {
275
+ return new PlatformDirs().userDocumentsDir;
276
+ }
277
+
278
+ /**
279
+ * @returns {string} downloads directory tied to the user
280
+ */
281
+ export function userDownloadsDir() {
282
+ return new PlatformDirs().userDownloadsDir;
283
+ }
284
+
285
+ /**
286
+ * @returns {string} pictures directory tied to the user
287
+ */
288
+ export function userPicturesDir() {
289
+ return new PlatformDirs().userPicturesDir;
290
+ }
291
+
292
+ /**
293
+ * @returns {string} videos directory tied to the user
294
+ */
295
+ export function userVideosDir() {
296
+ return new PlatformDirs().userVideosDir;
297
+ }
298
+
299
+ /**
300
+ * @returns {string} music directory tied to the user
301
+ */
302
+ export function userMusicDir() {
303
+ return new PlatformDirs().userMusicDir;
304
+ }
305
+
306
+ /**
307
+ * @returns {string} desktop directory tied to the user
308
+ */
309
+ export function userDesktopDir() {
310
+ return new PlatformDirs().userDesktopDir;
311
+ }
312
+
313
+ /**
314
+ * @param {string} [appname] See {@link PlatformDirs.appname}
315
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
316
+ * @param {string} [version] See {@link PlatformDirs.version}
317
+ * @param {boolean} [opinion=true] See {@link PlatformDirs.opinion}
318
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
319
+ * @returns {string} runtime directory tied to the user
320
+ */
321
+ export function userRuntimeDir(
322
+ appname,
323
+ appauthor,
324
+ version,
325
+ opinion = true,
326
+ ensureExists = false,
327
+ ) {
328
+ return new PlatformDirs(
329
+ appname,
330
+ appauthor,
331
+ version,
332
+ undefined,
333
+ undefined,
334
+ opinion,
335
+ ensureExists,
336
+ ).userRuntimeDir;
337
+ }
338
+
339
+ /**
340
+ * @param {string} [appname] See {@link PlatformDirs.appname}
341
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
342
+ * @param {string} [version] See {@link PlatformDirs.version}
343
+ * @param {boolean} [opinion=true] See {@link PlatformDirs.opinion}
344
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
345
+ * @returns runtime directory shared by users
346
+ */
347
+ export function siteRuntimeDir(
348
+ appname,
349
+ appauthor,
350
+ version,
351
+ opinion = true,
352
+ ensureExists = false,
353
+ ) {
354
+ return new PlatformDirs(
355
+ appname,
356
+ appauthor,
357
+ version,
358
+ undefined,
359
+ undefined,
360
+ opinion,
361
+ ensureExists,
362
+ ).siteRuntimeDir;
363
+ }
364
+
365
+ /**
366
+ * @param {string} [appname] See {@link PlatformDirs.appname}
367
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
368
+ * @param {string} [version] See {@link PlatformDirs.version}
369
+ * @param {boolean} [roaming=false] See {@link PlatformDirs.roaming}
370
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
371
+ * @returns data path tied to the user
372
+ */
373
+ export function userDataPath(
374
+ appname,
375
+ appauthor,
376
+ version,
377
+ roaming = false,
378
+ ensureExists = false,
379
+ ) {
380
+ return new PlatformDirs(
381
+ appname,
382
+ appauthor,
383
+ version,
384
+ roaming,
385
+ undefined,
386
+ undefined,
387
+ ensureExists,
388
+ ).userDataPath;
389
+ }
390
+
391
+ /**
392
+ * @param {string} [appname] See {@link PlatformDirs.appname}
393
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
394
+ * @param {string} [version] See {@link PlatformDirs.version}
395
+ * @param {boolean} [multipath=false] See {@link PlatformDirs.multipath}
396
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
397
+ * @returns data path shared by users
398
+ */
399
+ export function siteDataPath(
400
+ appname,
401
+ appauthor,
402
+ version,
403
+ multipath = false,
404
+ ensureExists = false,
405
+ ) {
406
+ return new PlatformDirs(
407
+ appname,
408
+ appauthor,
409
+ version,
410
+ undefined,
411
+ multipath,
412
+ undefined,
413
+ ensureExists,
414
+ ).siteDataPath;
415
+ }
416
+
417
+ /**
418
+ * @param {string} [appname] See {@link PlatformDirs.appname}
419
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
420
+ * @param {string} [version] See {@link PlatformDirs.version}
421
+ * @param {boolean} [roaming=false] See {@link PlatformDirs.roaming}
422
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
423
+ * @returns {string} config path tied to the user
424
+ */
425
+ export function userConfigPath(
426
+ appname,
427
+ appauthor,
428
+ version,
429
+ roaming = false,
430
+ ensureExists = false,
431
+ ) {
432
+ return new PlatformDirs(
433
+ appname,
434
+ appauthor,
435
+ version,
436
+ roaming,
437
+ undefined,
438
+ undefined,
439
+ ensureExists,
440
+ ).userConfigPath;
441
+ }
442
+
443
+ /**
444
+ * @param {string} [appname] See {@link PlatformDirs.appname}
445
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
446
+ * @param {string} [version] See {@link PlatformDirs.version}
447
+ * @param {boolean} [multipath=false] See {@link PlatformDirs.multipath}
448
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
449
+ * @returns {string} config path shared by the users
450
+ */
451
+ export function siteConfigPath(
452
+ appname,
453
+ appauthor,
454
+ version,
455
+ multipath = false,
456
+ ensureExists = false,
457
+ ) {
458
+ return new PlatformDirs(
459
+ appname,
460
+ appauthor,
461
+ version,
462
+ undefined,
463
+ multipath,
464
+ undefined,
465
+ ensureExists,
466
+ ).siteConfigPath;
467
+ }
468
+
469
+ /**
470
+ * @param {string} [appname] See {@link PlatformDirs.appname}
471
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
472
+ * @param {string} [version] See {@link PlatformDirs.version}
473
+ * @param {boolean} [opinion=true] See {@link PlatformDirs.opinion}
474
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
475
+ * @returns {string} cache path shared by users
476
+ */
477
+ export function siteCachePath(
478
+ appname,
479
+ appauthor,
480
+ version,
481
+ opinion = true,
482
+ ensureExists = false,
483
+ ) {
484
+ return new PlatformDirs(
485
+ appname,
486
+ appauthor,
487
+ version,
488
+ undefined,
489
+ undefined,
490
+ opinion,
491
+ ensureExists,
492
+ ).siteCachePath;
493
+ }
494
+
495
+ /**
496
+ * @param {string} [appname] See {@link PlatformDirs.appname}
497
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
498
+ * @param {string} [version] See {@link PlatformDirs.version}
499
+ * @param {boolean} [opinion=true] See {@link PlatformDirs.opinion}
500
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
501
+ * @returns {string} cache path tied to the user
502
+ */
503
+ export function userCachePath(
504
+ appname,
505
+ appauthor,
506
+ version,
507
+ opinion = true,
508
+ ensureExists = false,
509
+ ) {
510
+ return new PlatformDirs(
511
+ appname,
512
+ appauthor,
513
+ version,
514
+ undefined,
515
+ undefined,
516
+ opinion,
517
+ ensureExists,
518
+ ).userCachePath;
519
+ }
520
+
521
+ /**
522
+ * @param {string} [appname] See {@link PlatformDirs.appname}
523
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
524
+ * @param {string} [version] See {@link PlatformDirs.version}
525
+ * @param {boolean} [roaming=false] See {@link PlatformDirs.roaming}
526
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
527
+ * @returns {string} state path tied to the user
528
+ */
529
+ export function userStatePath(
530
+ appname,
531
+ appauthor,
532
+ version,
533
+ roaming = false,
534
+ ensureExists = false,
535
+ ) {
536
+ return new PlatformDirs(
537
+ appname,
538
+ appauthor,
539
+ version,
540
+ roaming,
541
+ undefined,
542
+ undefined,
543
+ ensureExists,
544
+ ).userStatePath;
545
+ }
546
+
547
+ /**
548
+ * @param {string} [appname] See {@link PlatformDirs.appname}
549
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
550
+ * @param {string} [version] See {@link PlatformDirs.version}
551
+ * @param {boolean} [opinion=true] See {@link PlatformDirs.opinion}
552
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
553
+ * @returns {string} log path tied to the user
554
+ */
555
+ export function userLogPath(
556
+ appname,
557
+ appauthor,
558
+ version,
559
+ opinion = true,
560
+ ensureExists = false,
561
+ ) {
562
+ return new PlatformDirs(
563
+ appname,
564
+ appauthor,
565
+ version,
566
+ undefined,
567
+ undefined,
568
+ opinion,
569
+ ensureExists,
570
+ ).userLogPath;
571
+ }
572
+
573
+ /** @returns {string} documents path tied to the user */
574
+ export function userDocumentsPath() {
575
+ return new PlatformDirs().userDocumentsPath;
576
+ }
577
+
578
+ /** @returns {string} downloads path tied to the user */
579
+ export function userDownloadsPath() {
580
+ return new PlatformDirs().userDownloadsPath;
581
+ }
582
+
583
+ /** @returns {string} pictures path tied to the user */
584
+ export function userPicturesPath() {
585
+ return new PlatformDirs().userPicturesPath;
586
+ }
587
+
588
+ /** @returns {string} videos path tied to the user */
589
+ export function userVideosPath() {
590
+ return new PlatformDirs().userVideosPath;
591
+ }
592
+
593
+ /** @returns {string} music path tied to the user */
594
+ export function userMusicPath() {
595
+ return new PlatformDirs().userMusicPath;
596
+ }
597
+
598
+ /** @returns {string} desktop path tied to the user */
599
+ export function userDesktopPath() {
600
+ return new PlatformDirs().userDesktopPath;
601
+ }
602
+
603
+ /**
604
+ * @param {string} [appname] See {@link PlatformDirs.appname}
605
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
606
+ * @param {string} [version] See {@link PlatformDirs.version}
607
+ * @param {boolean} [opinion=true] See {@link PlatformDirs.opinion}
608
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
609
+ * @returns {string} runtime path tied to the user
610
+ */
611
+ export function userRuntimePath(
612
+ appname,
613
+ appauthor,
614
+ version,
615
+ opinion = true,
616
+ ensureExists = false,
617
+ ) {
618
+ return new PlatformDirs(
619
+ appname,
620
+ appauthor,
621
+ version,
622
+ undefined,
623
+ undefined,
624
+ opinion,
625
+ ensureExists,
626
+ ).userRuntimePath;
627
+ }
628
+
629
+ /**
630
+ * @param {string} [appname] See {@link PlatformDirs.appname}
631
+ * @param {string|false} [appauthor] See {@link PlatformDirs.appauthor}
632
+ * @param {string} [version] See {@link PlatformDirs.version}
633
+ * @param {boolean} [opinion=true] See {@link PlatformDirs.opinion}
634
+ * @param {boolean} [ensureExists=false] See {@link PlatformDirs.ensureExists}
635
+ * @returns {string} runtime path shared by users
636
+ */
637
+ export function siteRuntimePath(
638
+ appname,
639
+ appauthor,
640
+ version,
641
+ opinion = true,
642
+ ensureExists = false,
643
+ ) {
644
+ return new PlatformDirs(
645
+ appname,
646
+ appauthor,
647
+ version,
648
+ undefined,
649
+ undefined,
650
+ opinion,
651
+ ensureExists,
652
+ ).siteRuntimePath;
653
+ }
654
+
655
+ export { PlatformDirsABC } from "./api.js";
656
+ export { version, versionTuple as versionInfo } from "./version.js";