GeezerVison is showing github.com — fs: apply exclude function to root path · nodejs/node@5006627
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/glob.js +25-1Lines changed: 25 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | ArrayPrototypePop, | 10 | 10 | ArrayPrototypePush, | 11 | 11 | ArrayPrototypeSome, | | 12 | + Promise, | 12 | 13 | PromisePrototypeThen, | 13 | 14 | SafeMap, | 14 | 15 | SafeSet, | @@ -125,7 +126,8 @@ class Cache { | 125 | 126 | } | 126 | 127 | statSync(path) { | 127 | 128 | const cached = this.#statsCache.get(path); | 128 | | - if (cached) { | | 129 | + // Do not return a promise from a sync function. | | 130 | + if (cached && !(cached instanceof Promise)) { | 129 | 131 | return cached; | 130 | 132 | } | 131 | 133 | const val = getDirentSync(path); | @@ -326,6 +328,28 @@ class Glob { | 326 | 328 | if (this.#isExcluded(path)) { | 327 | 329 | return; | 328 | 330 | } | | 331 | + const fullpath = resolve(this.#root, path); | | 332 | + | | 333 | + // If path is a directory, add trailing slash and test patterns again. | | 334 | + // TODO(Trott): Would running #isExcluded() first and checking isDirectory() only | | 335 | + // if it matches be more performant in the typical use case? #isExcluded() | | 336 | + // is often ()=>false which is about as optimizable as a function gets. | | 337 | + if (this.#cache.statSync(fullpath).isDirectory() && this.#isExcluded(`${fullpath}/`)) { | | 338 | + return; | | 339 | + } | | 340 | + | | 341 | + if (this.#exclude) { | | 342 | + if (this.#withFileTypes) { | | 343 | + const stat = this.#cache.statSync(path); | | 344 | + if (stat !== null) { | | 345 | + if (this.#exclude(stat)) { | | 346 | + return; | | 347 | + } | | 348 | + } | | 349 | + } else if (this.#exclude(path)) { | | 350 | + return; | | 351 | + } | | 352 | + } | 329 | 353 | if (!this.#subpatterns.has(path)) { | 330 | 354 | this.#subpatterns.set(path, [pattern]); | 331 | 355 | } else { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-glob.mjs +8-4Lines changed: 8 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -388,7 +388,7 @@ describe('fsPromises glob - withFileTypes', function() { | 388 | 388 | }); | 389 | 389 |
| 390 | 390 | // [pattern, exclude option, expected result] | 391 | | -const pattern2 = [ | | 391 | +const patterns2 = [ | 392 | 392 | ['a/{b,c}*', ['a/*c'], ['a/b', 'a/cb']], | 393 | 393 | ['a/{a,b,c}*', ['a/*bc*', 'a/cb'], ['a/b', 'a/c']], | 394 | 394 | ['a/**/[cg]', ['**/c'], ['a/abcdef/g', 'a/abcfed/g']], | @@ -427,6 +427,10 @@ const pattern2 = [ | 427 | 427 | [`${absDir}/*{a,q}*`, './a/*{c,b}*/*'], | 428 | 428 | [`${absDir}/foo`, 'a/c', ...(common.isWindows ? [] : ['a/symlink/a/b/c'])], | 429 | 429 | ], | | 430 | + [ 'a/**', () => true, [] ], | | 431 | + [ 'a/**', [ '*' ], [] ], | | 432 | + [ 'a/**', [ '**' ], [] ], | | 433 | + [ 'a/**', [ 'a/**' ], [] ], | 430 | 434 | ]; | 431 | 435 |
| 432 | 436 | describe('globSync - exclude', function() { | @@ -436,7 +440,7 @@ describe('globSync - exclude', function() { | 436 | 440 | assert.strictEqual(actual.length, 0); | 437 | 441 | }); | 438 | 442 | } | 439 | | - for (const [pattern, exclude, expected] of pattern2) { | | 443 | + for (const [pattern, exclude, expected] of patterns2) { | 440 | 444 | test(`${pattern} - exclude: ${exclude}`, () => { | 441 | 445 | const actual = globSync(pattern, { cwd: fixtureDir, exclude }).sort(); | 442 | 446 | const normalized = expected.filter(Boolean).map((item) => item.replaceAll('/', sep)).sort(); | @@ -453,7 +457,7 @@ describe('glob - exclude', function() { | 453 | 457 | assert.strictEqual(actual.length, 0); | 454 | 458 | }); | 455 | 459 | } | 456 | | - for (const [pattern, exclude, expected] of pattern2) { | | 460 | + for (const [pattern, exclude, expected] of patterns2) { | 457 | 461 | test(`${pattern} - exclude: ${exclude}`, async () => { | 458 | 462 | const actual = (await promisified(pattern, { cwd: fixtureDir, exclude })).sort(); | 459 | 463 | const normalized = expected.filter(Boolean).map((item) => item.replaceAll('/', sep)).sort(); | @@ -471,7 +475,7 @@ describe('fsPromises glob - exclude', function() { | 471 | 475 | assert.strictEqual(actual.length, 0); | 472 | 476 | }); | 473 | 477 | } | 474 | | - for (const [pattern, exclude, expected] of pattern2) { | | 478 | + for (const [pattern, exclude, expected] of patterns2) { | 475 | 479 | test(`${pattern} - exclude: ${exclude}`, async () => { | 476 | 480 | const actual = []; | 477 | 481 | for await (const item of asyncGlob(pattern, { cwd: fixtureDir, exclude })) actual.push(item); | |
|