Skip to content

Commit 2972d97

Browse files
fs: add maxDepth option to glob
1 parent a1074b8 commit 2972d97

4 files changed

Lines changed: 654 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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,8 @@ changes:
14361436
not supported.
14371437
* `followSymlinks` {boolean} When `true`, symbolic links to directories are
14381438
followed while expanding `**` patterns. **Default:** `false`.
1439+
* `maxDepth` {integer} Maximum number of directory levels to traverse.
1440+
The `cwd` directory has a depth of `0`. **Default:** `Infinity`.
14391441
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
14401442
`false` otherwise. **Default:** `false`.
14411443
* Returns: {AsyncIterator} An AsyncIterator that yields the paths of files
@@ -3590,6 +3592,8 @@ changes:
35903592
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
35913593
* `followSymlinks` {boolean} When `true`, symbolic links to directories are
35923594
followed while expanding `**` patterns. **Default:** `false`.
3595+
* `maxDepth` {integer} Maximum number of directory levels to traverse.
3596+
The `cwd` directory has a depth of `0`. **Default:** `Infinity`.
35933597
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
35943598
`false` otherwise. **Default:** `false`.
35953599
@@ -6220,6 +6224,8 @@ changes:
62206224
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
62216225
* `followSymlinks` {boolean} When `true`, symbolic links to directories are
62226226
followed while expanding `**` patterns. **Default:** `false`.
6227+
* `maxDepth` {integer} Maximum number of directory levels to traverse.
6228+
The `cwd` directory has a depth of `0`. **Default:** `Infinity`.
62236229
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
62246230
`false` otherwise. **Default:** `false`.
62256231
* Returns: {string\[]} paths of files that match the pattern.

0 commit comments

Comments
 (0)