Skip to content

Commit a00cf06

Browse files
fs: add maxDepth option to glob
Signed-off-by: Alexander Lichter <github@lichter.io> PR-URL: #64003 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent a020ac4 commit a00cf06

4 files changed

Lines changed: 667 additions & 32 deletions

File tree

benchmark/fs/bench-glob.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ const configs = {
1616
dir: ['lib'],
1717
pattern: ['**/*', '*.js', '**/**.js'],
1818
mode: ['sync', 'promise', 'callback'],
19+
maxDepth: ['default', '2'],
1920
recursive: ['true', 'false'],
2021
};
2122

2223
const bench = common.createBenchmark(main, configs);
2324

2425
async function main(config) {
2526
const fullPath = path.resolve(benchmarkDirectory, config.dir);
26-
const { pattern, recursive, mode } = config;
27+
const { pattern, recursive, mode, maxDepth } = config;
2728
const options = { cwd: fullPath, recursive };
29+
if (maxDepth !== 'default') {
30+
options.maxDepth = Number(maxDepth);
31+
}
2832
const callback = (resolve, reject) => {
2933
glob(pattern, options, (err, matches) => {
3034
if (err) {
@@ -44,7 +48,10 @@ async function main(config) {
4448
noDead = globSync(pattern, options);
4549
break;
4650
case 'promise':
47-
noDead = await globAsync(pattern, options);
51+
noDead = [];
52+
for await (const match of globAsync(pattern, options)) {
53+
noDead.push(match);
54+
}
4855
break;
4956
case 'callback':
5057
noDead = await new Promise(callback);

doc/api/fs.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,9 @@ behavior is similar to `cp dir1/ dir2/`.
14211421
<!-- YAML
14221422
added: v22.0.0
14231423
changes:
1424+
- version: REPLACEME
1425+
pr-url: https://github.com/nodejs/node/pull/64003
1426+
description: Add support for the `maxDepth` option.
14241427
- version:
14251428
- v26.1.0
14261429
- v24.16.0
@@ -1457,6 +1460,8 @@ changes:
14571460
not supported.
14581461
* `followSymlinks` {boolean} When `true`, symbolic links to directories are
14591462
followed while expanding `**` patterns. **Default:** `false`.
1463+
* `maxDepth` {integer} Maximum number of directory levels to traverse.
1464+
The `cwd` directory has a depth of `0`. **Default:** `Infinity`.
14601465
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
14611466
`false` otherwise. **Default:** `false`.
14621467
* Returns: {AsyncIterator} An AsyncIterator that yields the paths of files
@@ -3629,6 +3634,9 @@ descriptor. See [`fs.utimes()`][].
36293634
<!-- YAML
36303635
added: v22.0.0
36313636
changes:
3637+
- version: REPLACEME
3638+
pr-url: https://github.com/nodejs/node/pull/64003
3639+
description: Add support for the `maxDepth` option.
36323640
- version:
36333641
- v26.1.0
36343642
- v24.16.0
@@ -3663,6 +3671,8 @@ changes:
36633671
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
36643672
* `followSymlinks` {boolean} When `true`, symbolic links to directories are
36653673
followed while expanding `**` patterns. **Default:** `false`.
3674+
* `maxDepth` {integer} Maximum number of directory levels to traverse.
3675+
The `cwd` directory has a depth of `0`. **Default:** `Infinity`.
36663676
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
36673677
`false` otherwise. **Default:** `false`.
36683678
@@ -6273,6 +6283,9 @@ Synchronous version of [`fs.futimes()`][]. Returns `undefined`.
62736283
<!-- YAML
62746284
added: v22.0.0
62756285
changes:
6286+
- version: REPLACEME
6287+
pr-url: https://github.com/nodejs/node/pull/64003
6288+
description: Add support for the `maxDepth` option.
62766289
- version:
62776290
- v26.1.0
62786291
- v24.16.0
@@ -6306,6 +6319,8 @@ changes:
63066319
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
63076320
* `followSymlinks` {boolean} When `true`, symbolic links to directories are
63086321
followed while expanding `**` patterns. **Default:** `false`.
6322+
* `maxDepth` {integer} Maximum number of directory levels to traverse.
6323+
The `cwd` directory has a depth of `0`. **Default:** `Infinity`.
63096324
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
63106325
`false` otherwise. **Default:** `false`.
63116326
* Returns: {string\[]} paths of files that match the pattern.

0 commit comments

Comments
 (0)