rrdir 13.1.1 → 13.1.2
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/index.js +175 -125
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,164 +1,214 @@
|
|
|
1
|
-
import { readdir
|
|
2
|
-
import { readdirSync
|
|
3
|
-
import { sep
|
|
4
|
-
import
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { readdir, stat, lstat } from "node:fs/promises";
|
|
2
|
+
import { readdirSync, statSync, lstatSync } from "node:fs";
|
|
3
|
+
import { sep, resolve } from "node:path";
|
|
4
|
+
import picomatch from "picomatch";
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
const toUint8Array = encoder.encode.bind(encoder);
|
|
7
|
+
const decoder = new TextDecoder();
|
|
8
|
+
const toString = decoder.decode.bind(decoder);
|
|
9
|
+
const sepUint8Array = toUint8Array(sep);
|
|
10
|
+
const getEncoding = (dir) => dir instanceof Uint8Array ? "buffer" : "utf8";
|
|
11
|
+
const defaultOpts = {
|
|
12
|
+
strict: false,
|
|
13
|
+
stats: false,
|
|
14
|
+
followSymlinks: false,
|
|
9
15
|
exclude: void 0,
|
|
10
16
|
include: void 0,
|
|
11
|
-
insensitive:
|
|
17
|
+
insensitive: false
|
|
12
18
|
};
|
|
13
|
-
function
|
|
14
|
-
|
|
19
|
+
function makePath({ name }, dir, encoding) {
|
|
20
|
+
if (encoding === "buffer") {
|
|
21
|
+
return dir === "." ? name : Uint8Array.from([...dir, ...sepUint8Array, ...name]);
|
|
22
|
+
} else {
|
|
23
|
+
return dir === "." ? name : `${dir}${sep}${name}`;
|
|
24
|
+
}
|
|
15
25
|
}
|
|
16
|
-
function
|
|
26
|
+
function build(dirent, path, stats, opts) {
|
|
17
27
|
return {
|
|
18
|
-
path
|
|
19
|
-
directory: (
|
|
20
|
-
symlink: (
|
|
21
|
-
...
|
|
28
|
+
path,
|
|
29
|
+
directory: (stats || dirent).isDirectory(),
|
|
30
|
+
symlink: (stats || dirent).isSymbolicLink(),
|
|
31
|
+
...opts.stats ? { stats } : {}
|
|
22
32
|
};
|
|
23
33
|
}
|
|
24
|
-
function
|
|
25
|
-
const
|
|
26
|
-
dot:
|
|
27
|
-
flags:
|
|
34
|
+
function makeMatchers({ include, exclude, insensitive }) {
|
|
35
|
+
const opts = {
|
|
36
|
+
dot: true,
|
|
37
|
+
flags: insensitive ? "i" : void 0
|
|
28
38
|
};
|
|
29
39
|
return {
|
|
30
|
-
includeMatcher:
|
|
31
|
-
excludeMatcher:
|
|
40
|
+
includeMatcher: include?.length ? (path) => picomatch(include, opts)(resolve(path)) : null,
|
|
41
|
+
excludeMatcher: exclude?.length ? (path) => picomatch(exclude, opts)(resolve(path)) : null
|
|
32
42
|
};
|
|
33
43
|
}
|
|
34
|
-
async function*
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
async function* rrdir(dir, opts = {}, { includeMatcher, excludeMatcher, encoding } = {}) {
|
|
45
|
+
if (includeMatcher === void 0) {
|
|
46
|
+
opts = { ...defaultOpts, ...opts };
|
|
47
|
+
({ includeMatcher, excludeMatcher } = makeMatchers(opts));
|
|
48
|
+
if (typeof dir === "string" && /[/\\]$/.test(dir))
|
|
49
|
+
dir = dir.substring(0, dir.length - 1);
|
|
50
|
+
encoding = getEncoding(dir);
|
|
51
|
+
}
|
|
52
|
+
let dirents = [];
|
|
37
53
|
try {
|
|
38
|
-
|
|
39
|
-
} catch (
|
|
40
|
-
if (
|
|
41
|
-
throw
|
|
42
|
-
yield { path:
|
|
54
|
+
dirents = await readdir(dir, { encoding, withFileTypes: true });
|
|
55
|
+
} catch (err) {
|
|
56
|
+
if (opts.strict)
|
|
57
|
+
throw err;
|
|
58
|
+
yield { path: dir, err };
|
|
43
59
|
}
|
|
44
|
-
if (
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
if (!dirents.length)
|
|
61
|
+
return;
|
|
62
|
+
for (const dirent of dirents) {
|
|
63
|
+
const path = makePath(dirent, dir, encoding);
|
|
64
|
+
if (excludeMatcher?.(encoding === "buffer" ? toString(path) : path))
|
|
65
|
+
continue;
|
|
66
|
+
const isSymbolicLink = opts.followSymlinks && dirent.isSymbolicLink();
|
|
67
|
+
const encodedPath = encoding === "buffer" ? toString(path) : path;
|
|
68
|
+
const isIncluded = !includeMatcher || includeMatcher(encodedPath);
|
|
69
|
+
let stats;
|
|
70
|
+
if (isIncluded) {
|
|
71
|
+
if (opts.stats || isSymbolicLink) {
|
|
72
|
+
try {
|
|
73
|
+
stats = await (opts.followSymlinks ? stat : lstat)(path);
|
|
74
|
+
} catch (err) {
|
|
75
|
+
if (opts.strict)
|
|
76
|
+
throw err;
|
|
77
|
+
yield { path, err };
|
|
78
|
+
}
|
|
61
79
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
80
|
+
yield build(dirent, path, stats, opts);
|
|
81
|
+
}
|
|
82
|
+
let recurse = false;
|
|
83
|
+
if (isSymbolicLink) {
|
|
84
|
+
if (!stats)
|
|
85
|
+
try {
|
|
86
|
+
stats = await stat(path);
|
|
87
|
+
} catch {
|
|
88
|
+
}
|
|
89
|
+
if (stats && stats.isDirectory())
|
|
90
|
+
recurse = true;
|
|
91
|
+
} else if (dirent.isDirectory()) {
|
|
92
|
+
recurse = true;
|
|
73
93
|
}
|
|
94
|
+
if (recurse)
|
|
95
|
+
yield* rrdir(path, opts, { includeMatcher, excludeMatcher, encoding });
|
|
96
|
+
}
|
|
74
97
|
}
|
|
75
|
-
async function
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
98
|
+
async function rrdirAsync(dir, opts = {}, { includeMatcher, excludeMatcher, encoding } = {}) {
|
|
99
|
+
if (includeMatcher === void 0) {
|
|
100
|
+
opts = { ...defaultOpts, ...opts };
|
|
101
|
+
({ includeMatcher, excludeMatcher } = makeMatchers(opts));
|
|
102
|
+
if (typeof dir === "string" && /[/\\]$/.test(dir))
|
|
103
|
+
dir = dir.substring(0, dir.length - 1);
|
|
104
|
+
encoding = getEncoding(dir);
|
|
105
|
+
}
|
|
106
|
+
const results = [];
|
|
107
|
+
let dirents = [];
|
|
79
108
|
try {
|
|
80
|
-
|
|
81
|
-
} catch (
|
|
82
|
-
if (
|
|
83
|
-
throw
|
|
84
|
-
|
|
109
|
+
dirents = await readdir(dir, { encoding, withFileTypes: true });
|
|
110
|
+
} catch (err) {
|
|
111
|
+
if (opts.strict)
|
|
112
|
+
throw err;
|
|
113
|
+
results.push({ path: dir, err });
|
|
85
114
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
115
|
+
if (!dirents.length)
|
|
116
|
+
return results;
|
|
117
|
+
await Promise.all(dirents.map(async (dirent) => {
|
|
118
|
+
const path = makePath(dirent, dir, encoding);
|
|
119
|
+
if (excludeMatcher?.(encoding === "buffer" ? toString(path) : path))
|
|
89
120
|
return;
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
121
|
+
const isSymbolicLink = opts.followSymlinks && dirent.isSymbolicLink();
|
|
122
|
+
const encodedPath = encoding === "buffer" ? toString(path) : path;
|
|
123
|
+
const isIncluded = !includeMatcher || includeMatcher(encodedPath);
|
|
124
|
+
let stats;
|
|
125
|
+
if (isIncluded) {
|
|
126
|
+
if (opts.stats || isSymbolicLink) {
|
|
94
127
|
try {
|
|
95
|
-
|
|
96
|
-
} catch (
|
|
97
|
-
if (
|
|
98
|
-
throw
|
|
99
|
-
|
|
128
|
+
stats = await (opts.followSymlinks ? stat : lstat)(path);
|
|
129
|
+
} catch (err) {
|
|
130
|
+
if (opts.strict)
|
|
131
|
+
throw err;
|
|
132
|
+
results.push({ path, err });
|
|
100
133
|
}
|
|
101
|
-
|
|
134
|
+
}
|
|
135
|
+
results.push(build(dirent, path, stats, opts));
|
|
102
136
|
}
|
|
103
|
-
let
|
|
104
|
-
if (
|
|
105
|
-
if (!
|
|
137
|
+
let recurse = false;
|
|
138
|
+
if (isSymbolicLink) {
|
|
139
|
+
if (!stats)
|
|
106
140
|
try {
|
|
107
|
-
|
|
141
|
+
stats = await stat(path);
|
|
108
142
|
} catch {
|
|
109
143
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
144
|
+
if (stats && stats.isDirectory())
|
|
145
|
+
recurse = true;
|
|
146
|
+
} else if (dirent.isDirectory()) {
|
|
147
|
+
recurse = true;
|
|
148
|
+
}
|
|
149
|
+
if (recurse)
|
|
150
|
+
results.push(...await rrdirAsync(path, opts, { includeMatcher, excludeMatcher, encoding }));
|
|
151
|
+
}));
|
|
152
|
+
return results;
|
|
115
153
|
}
|
|
116
|
-
function
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
154
|
+
function rrdirSync(dir, opts = {}, { includeMatcher, excludeMatcher, encoding } = {}) {
|
|
155
|
+
if (includeMatcher === void 0) {
|
|
156
|
+
opts = { ...defaultOpts, ...opts };
|
|
157
|
+
({ includeMatcher, excludeMatcher } = makeMatchers(opts));
|
|
158
|
+
if (typeof dir === "string" && /[/\\]$/.test(dir))
|
|
159
|
+
dir = dir.substring(0, dir.length - 1);
|
|
160
|
+
encoding = getEncoding(dir);
|
|
161
|
+
}
|
|
162
|
+
const results = [];
|
|
163
|
+
let dirents = [];
|
|
120
164
|
try {
|
|
121
|
-
|
|
122
|
-
} catch (
|
|
123
|
-
if (
|
|
124
|
-
throw
|
|
125
|
-
|
|
165
|
+
dirents = readdirSync(dir, { encoding, withFileTypes: true });
|
|
166
|
+
} catch (err) {
|
|
167
|
+
if (opts.strict)
|
|
168
|
+
throw err;
|
|
169
|
+
results.push({ path: dir, err });
|
|
126
170
|
}
|
|
127
|
-
if (!
|
|
128
|
-
return
|
|
129
|
-
for (const
|
|
130
|
-
const
|
|
131
|
-
if (
|
|
171
|
+
if (!dirents.length)
|
|
172
|
+
return results;
|
|
173
|
+
for (const dirent of dirents) {
|
|
174
|
+
const path = makePath(dirent, dir, encoding);
|
|
175
|
+
if (excludeMatcher?.(encoding === "buffer" ? toString(path) : path))
|
|
132
176
|
continue;
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
177
|
+
const isSymbolicLink = opts.followSymlinks && dirent.isSymbolicLink();
|
|
178
|
+
const encodedPath = encoding === "buffer" ? toString(path) : path;
|
|
179
|
+
const isIncluded = !includeMatcher || includeMatcher(encodedPath);
|
|
180
|
+
let stats;
|
|
181
|
+
if (isIncluded) {
|
|
182
|
+
if (opts.stats || isSymbolicLink) {
|
|
137
183
|
try {
|
|
138
|
-
|
|
139
|
-
} catch (
|
|
140
|
-
if (
|
|
141
|
-
throw
|
|
142
|
-
|
|
184
|
+
stats = (opts.followSymlinks ? statSync : lstatSync)(path);
|
|
185
|
+
} catch (err) {
|
|
186
|
+
if (opts.strict)
|
|
187
|
+
throw err;
|
|
188
|
+
results.push({ path, err });
|
|
143
189
|
}
|
|
144
|
-
|
|
190
|
+
}
|
|
191
|
+
results.push(build(dirent, path, stats, opts));
|
|
145
192
|
}
|
|
146
|
-
let
|
|
147
|
-
if (
|
|
148
|
-
if (!
|
|
193
|
+
let recurse = false;
|
|
194
|
+
if (isSymbolicLink) {
|
|
195
|
+
if (!stats)
|
|
149
196
|
try {
|
|
150
|
-
|
|
197
|
+
stats = statSync(path);
|
|
151
198
|
} catch {
|
|
152
199
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
200
|
+
if (stats && stats.isDirectory())
|
|
201
|
+
recurse = true;
|
|
202
|
+
} else if (dirent.isDirectory()) {
|
|
203
|
+
recurse = true;
|
|
204
|
+
}
|
|
205
|
+
if (recurse)
|
|
206
|
+
results.push(...rrdirSync(path, opts, { includeMatcher, excludeMatcher, encoding }));
|
|
157
207
|
}
|
|
158
|
-
return
|
|
208
|
+
return results;
|
|
159
209
|
}
|
|
160
210
|
export {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
211
|
+
rrdir,
|
|
212
|
+
rrdirAsync,
|
|
213
|
+
rrdirSync
|
|
164
214
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rrdir",
|
|
3
|
-
"version": "13.1.
|
|
3
|
+
"version": "13.1.2",
|
|
4
4
|
"description": "Recursive directory reader with a delightful API",
|
|
5
5
|
"author": "silverwind <me@silverwind.io>",
|
|
6
6
|
"repository": "silverwind/rrdir",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"updates": "16.0.1",
|
|
30
30
|
"versions": "12.0.1",
|
|
31
31
|
"vite": "5.2.11",
|
|
32
|
-
"vite-config-silverwind": "2.0
|
|
32
|
+
"vite-config-silverwind": "2.1.0",
|
|
33
33
|
"vitest": "1.5.0",
|
|
34
34
|
"vitest-config-silverwind": "8.0.4"
|
|
35
35
|
}
|