Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
714 lines (569 loc) · 22 KB
/
Copy pathindex.js
File metadata and controls
714 lines (569 loc) · 22 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
import process from 'node:process';
import fs from 'node:fs';
import nodePath from 'node:path';
import {Readable} from 'node:stream';
import mergeStreams from '@sindresorhus/merge-streams';
import fastGlob from 'fast-glob';
import {toPath} from 'unicorn-magic/node';
import {
GITIGNORE_FILES_PATTERN,
getIgnorePatternsAndPredicate,
getIgnorePatternsAndPredicateSync,
getGlobalGitignoreFile,
getGlobalGitignoreFileAsync,
buildGlobalMatcher,
} from './ignore.js';
import {
bindFsMethod,
promisifyFsMethod,
isNegativePattern,
getStaticAbsolutePathPrefix,
normalizeNegativePattern,
adjustIgnorePatternsForParentDirectories,
convertPatternsForFastGlob,
convertIgnorePatternsForIgnoreFileSearch,
findGitRoot,
findGitRootSync,
} from './utilities.js';
const assertPatternsInput = patterns => {
if (patterns.some(pattern => typeof pattern !== 'string')) {
throw new TypeError('Patterns must be a string or an array of strings');
}
};
const getStatMethod = fsImplementation => {
if (fsImplementation) {
return bindFsMethod(fsImplementation.promises, 'stat')
?? promisifyFsMethod(fsImplementation, 'stat');
}
return bindFsMethod(fs.promises, 'stat');
};
const getStatSyncMethod = fsImplementation =>
bindFsMethod(fsImplementation, 'statSync')
?? bindFsMethod(fs, 'statSync');
const isDirectory = async (path, fsImplementation) => {
try {
const stats = await getStatMethod(fsImplementation)(path);
return stats.isDirectory();
} catch {
return false;
}
};
const isDirectorySync = (path, fsImplementation) => {
try {
const stats = getStatSyncMethod(fsImplementation)(path);
return stats.isDirectory();
} catch {
return false;
}
};
const normalizePathForDirectoryGlob = (filePath, cwd) => {
const path = isNegativePattern(filePath) ? filePath.slice(1) : filePath;
return nodePath.isAbsolute(path) ? path : nodePath.join(cwd, path);
};
const shouldExpandGlobstarDirectory = pattern => {
const match = pattern?.match(/\*\*\/([^/]+)$/);
if (!match) {
return false;
}
const dirname = match[1];
const hasWildcards = /[*?[\]{}]/.test(dirname);
const hasExtension = nodePath.extname(dirname) && !dirname.startsWith('.');
return !hasWildcards && !hasExtension;
};
const getDirectoryGlob = ({directoryPath, files, extensions}) => {
const extensionGlob = extensions?.length > 0 ? `.${extensions.length > 1 ? `{${extensions.join(',')}}` : extensions[0]}` : '';
return files
? files.map(file => nodePath.posix.join(directoryPath, `**/${nodePath.extname(file) ? file : `${file}${extensionGlob}`}`))
: [nodePath.posix.join(directoryPath, `**${extensionGlob ? `/*${extensionGlob}` : ''}`)];
};
const directoryToGlob = async (directoryPaths, {
cwd = process.cwd(),
files,
extensions,
fs: fsImplementation,
} = {}) => {
const globs = await Promise.all(directoryPaths.map(async directoryPath => {
// Check pattern without negative prefix
const checkPattern = isNegativePattern(directoryPath) ? directoryPath.slice(1) : directoryPath;
// Expand globstar directory patterns like **/dirname to **/dirname/**
if (shouldExpandGlobstarDirectory(checkPattern)) {
return getDirectoryGlob({directoryPath, files, extensions});
}
// Original logic for checking actual directories
const pathToCheck = normalizePathForDirectoryGlob(directoryPath, cwd);
return (await isDirectory(pathToCheck, fsImplementation)) ? getDirectoryGlob({directoryPath, files, extensions}) : directoryPath;
}));
return globs.flat();
};
const directoryToGlobSync = (directoryPaths, {
cwd = process.cwd(),
files,
extensions,
fs: fsImplementation,
} = {}) => directoryPaths.flatMap(directoryPath => {
// Check pattern without negative prefix
const checkPattern = isNegativePattern(directoryPath) ? directoryPath.slice(1) : directoryPath;
// Expand globstar directory patterns like **/dirname to **/dirname/**
if (shouldExpandGlobstarDirectory(checkPattern)) {
return getDirectoryGlob({directoryPath, files, extensions});
}
// Original logic for checking actual directories
const pathToCheck = normalizePathForDirectoryGlob(directoryPath, cwd);
return isDirectorySync(pathToCheck, fsImplementation) ? getDirectoryGlob({directoryPath, files, extensions}) : directoryPath;
});
const toPatternsArray = patterns => {
patterns = [...new Set([patterns].flat())];
assertPatternsInput(patterns);
return patterns;
};
const checkCwdOption = (cwd, fsImplementation = fs) => {
if (!cwd || !fsImplementation.statSync) {
return;
}
let stats;
try {
stats = fsImplementation.statSync(cwd);
} catch {
// If stat fails (e.g., path doesn't exist), let fast-glob handle it
return;
}
if (!stats.isDirectory()) {
throw new Error(`The \`cwd\` option must be a path to a directory, got: ${cwd}`);
}
};
const normalizeOptions = (options = {}) => {
// Normalize ignore to an array (fast-glob accepts string but we need array internally)
const ignore = options.ignore
? (Array.isArray(options.ignore) ? options.ignore : [options.ignore])
: [];
options = {
...options,
ignore,
expandDirectories: options.expandDirectories ?? true,
cwd: toPath(options.cwd),
};
checkCwdOption(options.cwd, options.fs);
return options;
};
const normalizeArguments = function_ => async (patterns, options) => function_(toPatternsArray(patterns), normalizeOptions(options));
const normalizeArgumentsSync = function_ => (patterns, options) => function_(toPatternsArray(patterns), normalizeOptions(options));
const getIgnoreFilesPatterns = options => {
const {ignoreFiles, gitignore} = options;
const patterns = ignoreFiles ? toPatternsArray(ignoreFiles) : [];
if (gitignore) {
patterns.push(GITIGNORE_FILES_PATTERN);
}
return patterns;
};
const isPathIgnored = (matcher, globalMatcher, path) => {
const globalResult = globalMatcher ? globalMatcher(path) : undefined;
const result = matcher ? matcher(path) : undefined;
if (result?.unignored) {
return false;
}
return Boolean(result?.ignored || globalResult?.ignored);
};
const hasIgnoredAncestorDirectory = (matcher, globalMatcher, file) => {
let currentPath = file;
while (true) {
const parentDirectory = nodePath.dirname(currentPath);
if (parentDirectory === currentPath) {
return false;
}
if (isPathIgnored(matcher, globalMatcher, `${parentDirectory}${nodePath.sep}`)) {
return true;
}
currentPath = parentDirectory;
}
};
const combinePredicate = (matcher, globalMatcher) => {
if (!matcher && !globalMatcher) {
return false;
}
return file => {
const result = matcher ? matcher(file) : undefined;
// A local negation (e.g. `!file`) re-includes the file, unless
// a parent directory is ignored by either matcher.
if (result?.unignored) {
const globalResult = globalMatcher ? globalMatcher(file) : undefined;
return globalResult?.ignored && hasIgnoredAncestorDirectory(matcher, globalMatcher, file);
}
return isPathIgnored(matcher, globalMatcher, file);
};
};
const buildIgnoreFilterResult = ({options, cwd, ignoreResult: {rules, matcher}, globalMatcher, createFilter}) => {
const finalPredicate = combinePredicate(matcher, globalMatcher);
// Patterns fast-glob can use to skip ignored directories while traversing. The predicate below stays authoritative, so this only ever needs to be safe, never exhaustive. They are returned separately from `options.ignore` so they skip directory expansion and the parent-directory adjustment, which would rebase them outside the scope their ignore files govern.
const pruneIgnorePatterns = convertPatternsForFastGlob(rules, matcher, cwd);
return {
options,
pruneIgnorePatterns,
filter: createFilter(finalPredicate, cwd, options.fs),
};
};
// The ignore files are searched for with the same options as the glob itself, except for `ignore`, which excludes paths from the results and must not decide which ignore files are read. The patterns that cannot hide one are still passed on, so the search keeps skipping the directories they name.
const getIgnoreFileSearchOptions = (options, searchPatterns) => ({
...options,
ignore: convertIgnorePatternsForIgnoreFileSearch(options.ignore, searchPatterns),
});
/**
Apply ignore files to options and return the filter predicate.
The predicate handles every rule, including negations, and is the authoritative filter applied
to fast-glob's results. Rules that provably cannot be affected by negations are additionally
translated into fast-glob `ignore` patterns, so whole ignored directories are skipped during
traversal; see `convertPatternsForFastGlob`.
@returns {Promise<{options: Object, pruneIgnorePatterns: string[], filter: Function}>}
*/
const applyIgnoreFilesAndGetFilter = async options => {
const cwd = options.cwd ?? process.cwd();
const ignoreFilesPatterns = getIgnoreFilesPatterns(options);
const globalIgnoreFile = options.globalGitignore ? await getGlobalGitignoreFileAsync(options) : undefined;
if (ignoreFilesPatterns.length === 0 && !globalIgnoreFile) {
return {
options,
pruneIgnorePatterns: [],
filter: createFilterFunctionAsync(false, cwd, options.fs),
};
}
// Read ignore files once and get both patterns and predicate
// Enable parent .gitignore search when using gitignore option
const includeParentIgnoreFiles = options.gitignore === true;
const ignoreResult = ignoreFilesPatterns.length > 0
? await getIgnorePatternsAndPredicate(ignoreFilesPatterns, getIgnoreFileSearchOptions(options, ignoreFilesPatterns), includeParentIgnoreFiles)
: {rules: [], matcher: false};
const globalGitRoot = globalIgnoreFile ? await findGitRoot(cwd, options.fs) : undefined;
const globalMatcher = globalIgnoreFile ? buildGlobalMatcher(globalIgnoreFile, cwd, globalGitRoot ?? cwd) : undefined;
return buildIgnoreFilterResult({
options,
cwd,
ignoreResult,
globalMatcher,
createFilter: createFilterFunctionAsync,
});
};
/**
Apply ignore files to options and return the filter predicate (sync version).
@returns {{options: Object, pruneIgnorePatterns: string[], filter: Function}}
*/
const applyIgnoreFilesAndGetFilterSync = options => {
const cwd = options.cwd ?? process.cwd();
const ignoreFilesPatterns = getIgnoreFilesPatterns(options);
const globalIgnoreFile = options.globalGitignore ? getGlobalGitignoreFile(options) : undefined;
if (ignoreFilesPatterns.length === 0 && !globalIgnoreFile) {
return {
options,
pruneIgnorePatterns: [],
filter: createFilterFunction(false, cwd, options.fs),
};
}
// Read ignore files once and get both patterns and predicate
// Enable parent .gitignore search when using gitignore option
const includeParentIgnoreFiles = options.gitignore === true;
const ignoreResult = ignoreFilesPatterns.length > 0
? getIgnorePatternsAndPredicateSync(ignoreFilesPatterns, getIgnoreFileSearchOptions(options, ignoreFilesPatterns), includeParentIgnoreFiles)
: {rules: [], matcher: false};
const globalGitRoot = globalIgnoreFile ? findGitRootSync(cwd, options.fs) : undefined;
const globalMatcher = globalIgnoreFile ? buildGlobalMatcher(globalIgnoreFile, cwd, globalGitRoot ?? cwd) : undefined;
return buildIgnoreFilterResult({
options,
cwd,
ignoreResult,
globalMatcher,
createFilter: createFilterFunction,
});
};
const assertGlobalGitignoreSyncSupport = options => {
if (options.globalGitignore && options.fs && !options.fs.statSync) {
throw new Error('The `globalGitignore` option in `globbySync()` requires `fs.statSync` when a custom `fs` is provided.');
}
};
const globalGitignoreAsyncStatErrorMessage = 'The `globalGitignore` option in `globby()` and `globbyStream()` requires `fs.promises.stat` or `fs.stat` when a custom `fs` is provided.';
const assertGlobalGitignoreAsyncSupport = options => {
if (!options.globalGitignore || !options.fs) {
return;
}
if (!options.fs.promises?.stat && !options.fs.stat) {
throw new Error(globalGitignoreAsyncStatErrorMessage);
}
};
const createPathResolver = cwd => {
const basePath = cwd || process.cwd();
const pathCache = new Map();
return pathKey => {
let absolutePath = pathCache.get(pathKey);
if (absolutePath === undefined) {
if (pathCache.size > 10_000) {
pathCache.clear();
}
absolutePath = nodePath.isAbsolute(pathKey) ? pathKey : nodePath.resolve(basePath, pathKey);
pathCache.set(pathKey, absolutePath);
}
return absolutePath;
};
};
const createAsyncDirectoryCheck = fsMethod => {
const directoryCache = new Map();
return async absolutePath => {
let isDirectory = directoryCache.get(absolutePath);
if (isDirectory !== undefined) {
return isDirectory;
}
try {
const stats = await fsMethod?.(absolutePath);
isDirectory = Boolean(stats?.isDirectory());
} catch {
isDirectory = false;
}
if (directoryCache.size > 10_000) {
directoryCache.clear();
}
directoryCache.set(absolutePath, isDirectory);
return isDirectory;
};
};
const createDirectoryCheck = fsMethod => {
const directoryCache = new Map();
return absolutePath => {
let isDirectory = directoryCache.get(absolutePath);
if (isDirectory !== undefined) {
return isDirectory;
}
try {
isDirectory = Boolean(fsMethod?.(absolutePath)?.isDirectory());
} catch {
isDirectory = false;
}
if (directoryCache.size > 10_000) {
directoryCache.clear();
}
directoryCache.set(absolutePath, isDirectory);
return isDirectory;
};
};
const createFilterFunctionAsync = (isIgnored, cwd, fsImplementation) => {
const resolveAbsolutePath = createPathResolver(cwd);
const isDirectoryEntry = createAsyncDirectoryCheck(getStatMethod(fsImplementation));
return async fastGlobResult => {
if (!isIgnored) {
return true;
}
const absolutePath = resolveAbsolutePath(nodePath.normalize(fastGlobResult.path ?? fastGlobResult));
if (isIgnored(absolutePath)) {
return false;
}
return !(await isDirectoryEntry(absolutePath) && isIgnored(`${absolutePath}${nodePath.sep}`));
};
};
const createFilterFunction = (isIgnored, cwd, fsImplementation) => {
const seen = new Set();
const resolveAbsolutePath = createPathResolver(cwd);
const isDirectoryEntry = createDirectoryCheck(getStatSyncMethod(fsImplementation));
return fastGlobResult => {
const pathKey = nodePath.normalize(fastGlobResult.path ?? fastGlobResult);
if (seen.has(pathKey)) {
return false;
}
if (isIgnored) {
const absolutePath = resolveAbsolutePath(pathKey);
if (isIgnored(absolutePath)) {
return false;
}
if (isDirectoryEntry(absolutePath) && isIgnored(`${absolutePath}${nodePath.sep}`)) {
return false;
}
}
seen.add(pathKey);
return true;
};
};
const unionFastGlobResults = (results, filter) => results.flat().filter(fastGlobResult => filter(fastGlobResult));
const unionFastGlobResultsAsync = async (results, filter) => {
results = results.flat();
const matches = await Promise.all(results.map(fastGlobResult => filter(fastGlobResult)));
const seen = new Set();
return results.filter((fastGlobResult, index) => {
if (!matches[index]) {
return false;
}
const pathKey = nodePath.normalize(fastGlobResult.path ?? fastGlobResult);
if (seen.has(pathKey)) {
return false;
}
seen.add(pathKey);
return true;
});
};
const convertNegativePatterns = (patterns, options) => {
// If all patterns are negative and expandNegationOnlyPatterns is enabled (default),
// prepend a positive catch-all pattern to make negation-only patterns work intuitively
// (e.g., '!*.json' matches all files except JSON)
if (patterns.length > 0 && patterns.every(pattern => isNegativePattern(pattern))) {
if (options.expandNegationOnlyPatterns === false) {
return [];
}
patterns = ['**/*', ...patterns];
}
const positiveAbsolutePathPrefixes = [];
let hasRelativePositivePattern = false;
const normalizedPatterns = [];
for (const pattern of patterns) {
if (isNegativePattern(pattern)) {
normalizedPatterns.push(`!${normalizeNegativePattern(pattern.slice(1), positiveAbsolutePathPrefixes, hasRelativePositivePattern)}`);
continue;
}
normalizedPatterns.push(pattern);
const staticAbsolutePathPrefix = getStaticAbsolutePathPrefix(pattern);
if (staticAbsolutePathPrefix === undefined) {
hasRelativePositivePattern = true;
continue;
}
positiveAbsolutePathPrefixes.push(staticAbsolutePathPrefix);
}
patterns = normalizedPatterns;
const tasks = [];
while (patterns.length > 0) {
const index = patterns.findIndex(pattern => isNegativePattern(pattern));
if (index === -1) {
tasks.push({patterns, options});
break;
}
const ignorePattern = patterns[index].slice(1);
for (const task of tasks) {
task.options.ignore.push(ignorePattern);
}
if (index !== 0) {
tasks.push({
patterns: patterns.slice(0, index),
options: {
...options,
ignore: [
...options.ignore,
ignorePattern,
],
},
});
}
patterns = patterns.slice(index + 1);
}
return tasks;
};
const applyParentDirectoryIgnoreAdjustments = tasks => tasks.map(task => ({
patterns: task.patterns,
options: {
...task.options,
ignore: adjustIgnorePatternsForParentDirectories(task.patterns, task.options.ignore),
},
}));
// Prune patterns are appended after directory expansion and the parent-directory adjustment on
// purpose: they are already glob-shaped, and rebasing them onto a `../` prefix would let them
// ignore paths outside the scope of the ignore files they came from.
const appendPruneIgnorePatterns = (tasks, pruneIgnorePatterns) => pruneIgnorePatterns.length === 0
? tasks
: tasks.map(task => ({
patterns: task.patterns,
options: {
...task.options,
ignore: [...task.options.ignore, ...pruneIgnorePatterns],
},
}));
const normalizeExpandDirectoriesOption = (options, cwd) => ({
...(cwd ? {cwd} : {}),
...(Array.isArray(options) ? {files: options} : options),
});
const generateTasks = async (patterns, options, pruneIgnorePatterns = []) => {
const globTasks = convertNegativePatterns(patterns, options);
const {cwd, expandDirectories, fs: fsImplementation} = options;
if (!expandDirectories) {
return appendPruneIgnorePatterns(applyParentDirectoryIgnoreAdjustments(globTasks), pruneIgnorePatterns);
}
const directoryToGlobOptions = {
...normalizeExpandDirectoriesOption(expandDirectories, cwd),
fs: fsImplementation,
};
const tasks = await Promise.all(globTasks.map(async task => {
let {patterns, options} = task;
[
patterns,
options.ignore,
] = await Promise.all([
directoryToGlob(patterns, directoryToGlobOptions),
directoryToGlob(options.ignore, {cwd, fs: fsImplementation}),
]);
// Adjust ignore patterns for parent directory references
options.ignore = adjustIgnorePatternsForParentDirectories(patterns, options.ignore);
return {patterns, options};
}));
return appendPruneIgnorePatterns(tasks, pruneIgnorePatterns);
};
const generateTasksSync = (patterns, options, pruneIgnorePatterns = []) => {
const globTasks = convertNegativePatterns(patterns, options);
const {cwd, expandDirectories, fs: fsImplementation} = options;
if (!expandDirectories) {
return appendPruneIgnorePatterns(applyParentDirectoryIgnoreAdjustments(globTasks), pruneIgnorePatterns);
}
const directoryToGlobSyncOptions = {
...normalizeExpandDirectoriesOption(expandDirectories, cwd),
fs: fsImplementation,
};
const tasks = globTasks.map(task => {
let {patterns, options} = task;
patterns = directoryToGlobSync(patterns, directoryToGlobSyncOptions);
options.ignore = directoryToGlobSync(options.ignore, {cwd, fs: fsImplementation});
// Adjust ignore patterns for parent directory references
options.ignore = adjustIgnorePatternsForParentDirectories(patterns, options.ignore);
return {patterns, options};
});
return appendPruneIgnorePatterns(tasks, pruneIgnorePatterns);
};
export const globby = normalizeArguments(async (patterns, options) => {
assertGlobalGitignoreAsyncSupport(options);
// Apply ignore files and get filter (reads .gitignore files once)
const {options: modifiedOptions, pruneIgnorePatterns, filter} = await applyIgnoreFilesAndGetFilter(options);
// Generate tasks, attaching the prune patterns so fast-glob skips ignored directories
const tasks = await generateTasks(patterns, modifiedOptions, pruneIgnorePatterns);
const results = await Promise.all(tasks.map(task => fastGlob(task.patterns, task.options)));
return unionFastGlobResultsAsync(results, filter);
});
export const globbySync = normalizeArgumentsSync((patterns, options) => {
assertGlobalGitignoreSyncSupport(options);
// Apply ignore files and get filter (reads .gitignore files once)
const {options: modifiedOptions, pruneIgnorePatterns, filter} = applyIgnoreFilesAndGetFilterSync(options);
// Generate tasks, attaching the prune patterns so fast-glob skips ignored directories
const tasks = generateTasksSync(patterns, modifiedOptions, pruneIgnorePatterns);
const results = tasks.map(task => fastGlob.sync(task.patterns, task.options));
return unionFastGlobResults(results, filter);
});
export const globbyStream = normalizeArgumentsSync((patterns, options) => {
assertGlobalGitignoreAsyncSupport(options);
const seen = new Set();
const stream = Readable.from((async function * () {
// Apply ignore files and get filter (reads .gitignore files once)
const {options: modifiedOptions, pruneIgnorePatterns, filter} = await applyIgnoreFilesAndGetFilter(options);
// Generate tasks, attaching the prune patterns so fast-glob skips ignored directories
const tasks = await generateTasks(patterns, modifiedOptions, pruneIgnorePatterns);
if (tasks.length === 0) {
return;
}
const streams = tasks.map(task => fastGlob.stream(task.patterns, task.options));
for await (const fastGlobResult of mergeStreams(streams)) {
const pathKey = nodePath.normalize(fastGlobResult.path ?? fastGlobResult);
if (!seen.has(pathKey) && await filter(fastGlobResult)) {
seen.add(pathKey);
yield fastGlobResult;
}
}
})());
// Returning a web stream will require revisiting once Readable.toWeb integration is viable.
// return Readable.toWeb(stream);
return stream;
});
export const isDynamicPattern = normalizeArgumentsSync((patterns, options) => patterns.some(pattern => fastGlob.isDynamicPattern(pattern, options)));
export const generateGlobTasks = normalizeArguments(generateTasks);
export const generateGlobTasksSync = normalizeArgumentsSync(generateTasksSync);
export {
isGitIgnored,
isGitIgnoredSync,
isIgnoredByIgnoreFiles,
isIgnoredByIgnoreFilesSync,
} from './ignore.js';
export const {convertPathToPattern} = fastGlob;