Skip to content

Commit a7856ab

Browse files
TheAlexLichteraduh95
authored andcommitted
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 23f26ee commit a7856ab

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
@@ -1414,6 +1414,9 @@ behavior is similar to `cp dir1/ dir2/`.
14141414
<!-- YAML
14151415
added: v22.0.0
14161416
changes:
1417+
- version: REPLACEME
1418+
pr-url: https://github.com/nodejs/node/pull/64003
1419+
description: Add support for the `maxDepth` option.
14171420
- version: v26.1.0
14181421
pr-url: https://github.com/nodejs/node/pull/62695
14191422
description: Add support for the `followSymlinks` option.
@@ -1448,6 +1451,8 @@ changes:
14481451
not supported.
14491452
* `followSymlinks` {boolean} When `true`, symbolic links to directories are
14501453
followed while expanding `**` patterns. **Default:** `false`.
1454+
* `maxDepth` {integer} Maximum number of directory levels to traverse.
1455+
The `cwd` directory has a depth of `0`. **Default:** `Infinity`.
14511456
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
14521457
`false` otherwise. **Default:** `false`.
14531458
* Returns: {AsyncIterator} An AsyncIterator that yields the paths of files
@@ -3618,6 +3623,9 @@ descriptor. See [`fs.utimes()`][].
36183623
<!-- YAML
36193624
added: v22.0.0
36203625
changes:
3626+
- version: REPLACEME
3627+
pr-url: https://github.com/nodejs/node/pull/64003
3628+
description: Add support for the `maxDepth` option.
36213629
- version: v26.1.0
36223630
pr-url: https://github.com/nodejs/node/pull/62695
36233631
description: Add support for the `followSymlinks` option.
@@ -3650,6 +3658,8 @@ changes:
36503658
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
36513659
* `followSymlinks` {boolean} When `true`, symbolic links to directories are
36523660
followed while expanding `**` patterns. **Default:** `false`.
3661+
* `maxDepth` {integer} Maximum number of directory levels to traverse.
3662+
The `cwd` directory has a depth of `0`. **Default:** `Infinity`.
36533663
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
36543664
`false` otherwise. **Default:** `false`.
36553665
@@ -6256,6 +6266,9 @@ Synchronous version of [`fs.futimes()`][]. Returns `undefined`.
62566266
<!-- YAML
62576267
added: v22.0.0
62586268
changes:
6269+
- version: REPLACEME
6270+
pr-url: https://github.com/nodejs/node/pull/64003
6271+
description: Add support for the `maxDepth` option.
62596272
- version: v26.1.0
62606273
pr-url: https://github.com/nodejs/node/pull/62695
62616274
description: Add support for the `followSymlinks` option.
@@ -6287,6 +6300,8 @@ changes:
62876300
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
62886301
* `followSymlinks` {boolean} When `true`, symbolic links to directories are
62896302
followed while expanding `**` patterns. **Default:** `false`.
6303+
* `maxDepth` {integer} Maximum number of directory levels to traverse.
6304+
The `cwd` directory has a depth of `0`. **Default:** `Infinity`.
62906305
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
62916306
`false` otherwise. **Default:** `false`.
62926307
* Returns: {string\[]} paths of files that match the pattern.

0 commit comments

Comments
 (0)