-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
89 lines (84 loc) · 1.76 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import fs from 'node:fs/promises';
import { sep } from 'node:path';
import { cwd, emitWarning } from 'node:process';
import { pathToFileURL } from 'node:url';
// list indices of arguments that should be converted
const processedArgs = {
access: [0],
appendFile: [0],
chmod: [0],
chown: [0],
copyFile: [0, 1],
cp: [0, 1],
lchmod: [0],
lchown: [0],
lutimes: [0],
link: [0, 1],
lstat: [0],
mkdir: [0],
mkdtemp: [0],
open: [0],
opendir: [0],
readdir: [0],
readFile: [0],
readlink: [0],
realpath: [0],
rename: [0, 1],
rmdir: [0],
rm: [0],
stat: [0],
statfs: [0],
symlink: [0, 1],
truncate: [0],
unlink: [0],
utimes: [0],
watch: [0],
writeFile: [0],
constants: null,
};
// in case of this warning, new methods should be added according to https://nodejs.org/api/fs.html#promises-api
const unsupported = Object.keys(fs).filter(_ => processedArgs[_] === undefined).map(_ => `fsURL.${_}`);
if (unsupported.length)
emitWarning(`${unsupported.join(', ')} is not supported`);
// this can be a string literal if cwd does not change
const cwdURL = {
[Symbol.toPrimitive]: () => pathToFileURL(cwd() + sep).href
};
const fsURL = Object.fromEntries(Object.entries(processedArgs).map(([_, $]) => [_, (_ = fs[_]) && ($ = new Set($)).size ? new Proxy(_, {
__proto__: null,
apply: (_, __, ___) => Reflect.apply(_, __, ___.map((_, __) => $.has(__) ? new URL(_, cwdURL) : _)),
}) : _]));
export const {
access,
appendFile,
chmod,
chown,
copyFile,
cp,
lchmod,
lchown,
lutimes,
link,
lstat,
mkdir,
mkdtemp,
open,
opendir,
readdir,
readFile,
readlink,
realpath,
rename,
rmdir,
rm,
stat,
statfs,
symlink,
truncate,
unlink,
utimes,
watch,
writeFile,
constants,
} = fsURL;
export default fsURL;