Skip to content

Commit e9f17df

Browse files
fs: add maxDepth option to glob
Signed-off-by: Alexander Lichter <github@lichter.io>
1 parent a1074b8 commit e9f17df

4 files changed

Lines changed: 663 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
@@ -1400,6 +1400,9 @@ behavior is similar to `cp dir1/ dir2/`.
14001400
<!-- YAML
14011401
added: v22.0.0
14021402
changes:
1403+
- version: REPLACEME
1404+
pr-url: https://github.com/nodejs/node/pull/64003
1405+
description: Add support for the `maxDepth` option.
14031406
- version:
14041407
- v26.1.0
14051408
- v24.16.0
@@ -1436,6 +1439,8 @@ changes:
14361439
not supported.
14371440
* `followSymlinks` {boolean} When `true`, symbolic links to directories are
14381441
followed while expanding `**` patterns. **Default:** `false`.
1442+
* `maxDepth` {integer} Maximum number of directory levels to traverse.
1443+
The `cwd` directory has a depth of `0`. **Default:** `Infinity`.
14391444
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
14401445
`false` otherwise. **Default:** `false`.
14411446
* Returns: {AsyncIterator} An AsyncIterator that yields the paths of files
@@ -3556,6 +3561,9 @@ descriptor. See [`fs.utimes()`][].
35563561
<!-- YAML
35573562
added: v22.0.0
35583563
changes:
3564+
- version: REPLACEME
3565+
pr-url: https://github.com/nodejs/node/pull/64003
3566+
description: Add support for the `maxDepth` option.
35593567
- version:
35603568
- v26.1.0
35613569
- v24.16.0
@@ -3590,6 +3598,8 @@ changes:
35903598
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
35913599
* `followSymlinks` {boolean} When `true`, symbolic links to directories are
35923600
followed while expanding `**` patterns. **Default:** `false`.
3601+
* `maxDepth` {integer} Maximum number of directory levels to traverse.
3602+
The `cwd` directory has a depth of `0`. **Default:** `Infinity`.
35933603
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
35943604
`false` otherwise. **Default:** `false`.
35953605
@@ -6187,6 +6197,9 @@ Synchronous version of [`fs.futimes()`][]. Returns `undefined`.
61876197
<!-- YAML
61886198
added: v22.0.0
61896199
changes:
6200+
- version: REPLACEME
6201+
pr-url: https://github.com/nodejs/node/pull/64003
6202+
description: Add support for the `maxDepth` option.
61906203
- version:
61916204
- v26.1.0
61926205
- v24.16.0
@@ -6220,6 +6233,8 @@ changes:
62206233
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
62216234
* `followSymlinks` {boolean} When `true`, symbolic links to directories are
62226235
followed while expanding `**` patterns. **Default:** `false`.
6236+
* `maxDepth` {integer} Maximum number of directory levels to traverse.
6237+
The `cwd` directory has a depth of `0`. **Default:** `Infinity`.
62236238
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
62246239
`false` otherwise. **Default:** `false`.
62256240
* Returns: {string\[]} paths of files that match the pattern.

0 commit comments

Comments
 (0)