MCPcopy Create free account
hub / github.com/nodejs/node / #iterateSubpatterns

Method #iterateSubpatterns

lib/internal/fs/glob.js:700–922  ·  view source on GitHub ↗
(path, pattern)

Source from the content-addressed store, hash-verified

698 }
699 }
700 async* #iterateSubpatterns(path, pattern) {
701 const seen = this.#cache.add(path, pattern);
702 if (seen) {
703 return;
704 }
705 const fullpath = resolve(this.#root, path);
706 const stat = await this.#cache.stat(fullpath);
707 const last = pattern.last;
708 const isDirectory = await this.#isDirectory(fullpath, stat, pattern);
709 const isLast = pattern.isLast(isDirectory);
710 const isFirst = pattern.isFirst();
711
712 if (this.#isExcluded(fullpath)) {
713 return;
714 }
715 if (isFirst && isWindows && typeof pattern.at(0) === 'string' && StringPrototypeEndsWith(pattern.at(0), ':')) {
716 // Absolute path, go to root
717 this.#addSubpattern(`${pattern.at(0)}\\`, pattern.child(new SafeSet().add(1)));
718 return;
719 }
720 if (isFirst && pattern.at(0) === '') {
721 // Absolute path, go to root
722 this.#addSubpattern('/', pattern.child(new SafeSet().add(1)));
723 return;
724 }
725 if (isFirst && pattern.at(0) === '..') {
726 // Start with .., go to parent
727 this.#addSubpattern('../', pattern.child(new SafeSet().add(1)));
728 return;
729 }
730 if (isFirst && pattern.at(0) === '.') {
731 // Start with ., proceed
732 this.#addSubpattern('.', pattern.child(new SafeSet().add(1)));
733 return;
734 }
735
736 if (isLast && typeof pattern.at(-1) === 'string') {
737 // Add result if it exists
738 const p = pattern.at(-1);
739 const stat = await this.#cache.stat(join(fullpath, p));
740 if (stat && (p || isDirectory)) {
741 const result = join(path, p);
742 if (!this.#results.has(result)) {
743 if (this.#results.add(result)) {
744 yield this.#withFileTypes ? stat : result;
745 }
746 }
747 }
748 if (pattern.indexes.size === 1 && pattern.indexes.has(last)) {
749 return;
750 }
751 } else if (isLast && pattern.at(-1) === lazyMinimatch().GLOBSTAR &&
752 (path !== '.' || pattern.at(0) === '.' || (last === 0 && stat))) {
753 // If pattern ends with **, add to results
754 // if path is ".", add it only if pattern starts with "." or pattern is exactly "**"
755 if (!this.#results.has(path)) {
756 if (this.#results.add(path)) {
757 yield this.#withFileTypes ? stat : path;

Callers 1

globMethod · 0.95

Calls 15

#isDirectoryMethod · 0.95
#addSubpatternMethod · 0.95
#isCyclicMethod · 0.95
#nextRealpathsMethod · 0.95
lazyMinimatchFunction · 0.85
isLastMethod · 0.80
isFirstMethod · 0.80
childMethod · 0.80
addToStatCacheMethod · 0.80
followStatMethod · 0.80
seenMethod · 0.80
joinFunction · 0.70

Tested by

no test coverage detected