-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasync.js
323 lines (263 loc) · 8.03 KB
/
async.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
'use strict';
const fs = require('fs');
const path = require('path');
const utils = require('./utils');
const { realpath, stat } = fs.promises;
const kPath = Symbol('path');
const kResult = Symbol('result');
const kRealPathExists = Symbol('realpath-exists');
const kUpdated = Symbol('updated');
const readdir = async (basedir, options = {}) => {
if (Array.isArray(basedir)) {
return readdirs(basedir, options);
}
const seen = new Set();
const results = [];
const {
absolute,
onDirectory,
onFile,
onEach,
onPush,
onSymbolicLink,
recursive
} = options;
const cwd = absolute ? path.resolve(basedir) : basedir;
let base = options.base || cwd;
if (absolute && base !== cwd) {
base = path.resolve(base);
}
const depth = typeof options.depth === 'number' ? options.depth : null;
const dirs = options.dirs !== false && options.nodir !== true;
const follow = options.follow === true || options.realpath === true || options.symlinks === true;
const objects = options.objects === true || options.withFileTypes === true;
const recurse = recursive === true || (depth !== null && depth > 1);
const sep = options.sep || path.sep;
const symlinks = (options.symlinks !== false && options.follow !== false) || options.realpath === true;
const filter = options.filter ? utils.matcher(options.filter) : () => true;
const isMatch = options.isMatch && utils.matcher(options.isMatch, options);
const isMaxDepth = file => depth !== null && file.depth >= depth;
const updatePaths = file => {
if (!file[kUpdated] && file.path !== file[kPath]) {
file[kUpdated] = true;
file.name = path.basename(file.path);
file.dirname = path.dirname(file.path);
file.relative = path.relative(file.base, file.path);
}
};
const getReturnValue = (file, parent) => {
if (file.ignore === true || file.keep === false) return;
if (filter(file) === false) return;
if (file.keep !== true) {
if (symlinks !== true && follow !== true && file.isSymbolicLink()) return;
if (dirs !== true && file.isDirectory()) return;
if (options.nodir === true && file.isDirectory()) return;
if (options.dot === false && file.name.startsWith('.')) return;
if (isMatch) updatePaths(file);
if (isMatch && isMatch(file, parent) === false) return;
}
if (absolute === true) {
file.path = path.resolve(cwd, file.path);
if (objects !== true) {
return file.path;
}
}
updatePaths(file);
if (objects === true) {
return file;
}
if (options.push !== false) {
return file.relative;
}
};
const push = async (file, parent) => {
const value = getReturnValue(file, parent);
if (value && (options.unique !== true || !seen.has(value))) {
file[kResult] = value;
if (options.unique === true) {
seen.add(value);
}
if (typeof onPush === 'function') {
await onPush(file);
}
results.push(value);
}
};
const shouldStopRecursing = file => {
return file.path !== cwd && (file.recurse === false || (file.recurse !== true && recurse === false));
};
const walk = async (dirent, parent, next) => {
try {
if (onEach) await onEach(dirent, parent);
if (onDirectory) await onDirectory(dirent, parent);
} catch (err) {
next(err);
return;
}
if (dirent.path !== cwd) {
await push(dirent, parent);
}
if (isMaxDepth(dirent)) {
dirent.recurse = false;
}
if (shouldStopRecursing(dirent)) {
next(null, results);
return;
}
fs.readdir(dirent.path, { withFileTypes: true }, async (err, files) => {
if (err) {
if (err.code === 'ENOENT') {
next(null, results);
return;
}
err.file = dirent;
next(err);
return;
}
let len = files.length;
if (len === 0) {
next(null, results);
return;
}
for (const file of files) {
file.depth = dirent.depth + 1;
file.cwd = cwd;
file.base = base;
file.folder = dirent.name;
file.dirname = dirent.path;
file.path = file[kPath] = `${dirent.path}${sep}${file.name}`;
let updatedRelative = false;
// lazily decorate relative path if or when a user supplied function is called
// if no function is passed, we can avoid calling this function
const updateRelative = () => {
if (updatedRelative) return;
updatedRelative = true;
if (file[kPath] === file.path && file.base === file.cwd) {
file.relative = dirent.relative ? `${dirent.relative}${sep}${file.name}` : file.name;
} else {
file.relative = path.relative(file.base, file.path);
}
};
if (absolute !== true || objects === true || isMatch) {
updateRelative();
}
if (onEach) {
try {
updateRelative();
await onEach(file, dirent);
} catch (err) {
next(err);
return;
}
}
if (file.isSymbolicLink()) {
try {
if (typeof onSymbolicLink === 'function') {
updateRelative();
await onSymbolicLink(file, dirent);
}
if (options.realpath) {
file.path = await realpath(file.path);
file.dirname = path.dirname(file.path);
file[kRealPathExists] = true;
}
if (options.symlinks === true || (options.nodir !== true && follow === true) || options.stat === true) {
file.stat = await stat(file.path);
file.isFile = () => file.stat.isFile();
file.isDirectory = () => file.stat.isDirectory();
}
} catch (err) {
if (err.code !== 'ENOENT') {
next(err);
return;
}
file[kRealPathExists] = false;
}
} else if (options.stat === true) {
file.stat = await stat(file[kPath]);
}
if (file.isDirectory()) {
walk(file, dirent, (err, res) => {
if (err) {
err.parent = dirent;
err.file = file;
next(err);
return;
}
if (--len === 0) {
next(null, results);
}
});
continue;
}
if (typeof onFile === 'function') {
try {
updateRelative();
await onFile(file, dirent);
} catch (err) {
next(err);
return;
}
}
await push(file, dirent);
if (--len === 0) {
next(null, results);
}
}
});
};
return new Promise((resolve, reject) => {
const dirent = new fs.Dirent(null, 2);
dirent.depth = 0;
dirent.base = base;
dirent.path = cwd;
dirent.cwd = cwd;
dirent.relative = '';
dirent[kPath] = cwd;
let handled = false;
const handleError = err => {
if (!handled) {
handled = true;
reject(err);
}
};
const promise = walk(dirent, null, (err, results) => {
if (err) {
handleError(err);
return;
}
if (!handled) {
handled = true;
resolve(results);
}
});
promise.catch(handleError);
});
};
const readdirs = (dirs, options = {}) => {
const unique = options.unique === true;
const seen = new Set();
const pending = [];
const files = [];
const onPush = async (file, parent) => {
const result = file[kResult];
if (unique === true) {
if (!seen.has(file.relative)) {
seen.add(file.relative);
files.push(result);
}
} else {
files.push(result);
}
if (options.onPush) {
await options.onPush(file, parent);
}
};
const opts = { ...options, onPush };
for (const dir of [].concat(dirs)) {
pending.push(readdir(dir, opts));
}
return Promise.all(pending).then(() => unique ? [...files] : files);
};
readdir.FILE_RESULT = kResult;
module.exports = readdir;