Skip to content

Commit 151d365

Browse files
authored
fs: expose glob and globSync
PR-URL: #51912 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Nitzan Uziely <linkgoron@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 2a33e95 commit 151d365

File tree

6 files changed

+443
-34
lines changed

6 files changed

+443
-34
lines changed

doc/api/fs.md

+97
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,38 @@ including subdirectories and files.
10691069
When copying a directory to another directory, globs are not supported and
10701070
behavior is similar to `cp dir1/ dir2/`.
10711071
1072+
### `fsPromises.glob(pattern[, options])`
1073+
1074+
<!-- YAML
1075+
added: REPLACEME
1076+
-->
1077+
1078+
> Stability: 1 - Experimental
1079+
1080+
* `pattern` {string|string\[]}
1081+
* `options` {Object}
1082+
* `cwd` {string} current working directory. **Default:** `process.cwd()`
1083+
* `exclude` {Function} Function to filter out files/directories. Return
1084+
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
1085+
* Returns: {AsyncIterator} An AsyncIterator that yields the paths of files
1086+
that match the pattern.
1087+
1088+
```mjs
1089+
import { glob } from 'node:fs/promises';
1090+
1091+
for await (const entry of glob('**/*.js'))
1092+
console.log(entry);
1093+
```
1094+
1095+
```cjs
1096+
const { glob } = require('node:fs/promises');
1097+
1098+
(async () => {
1099+
for await (const entry of glob('**/*.js'))
1100+
console.log(entry);
1101+
})();
1102+
```
1103+
10721104
### `fsPromises.lchmod(path, mode)`
10731105
10741106
<!-- YAML
@@ -3073,6 +3105,44 @@ changes:
30733105
Change the file system timestamps of the object referenced by the supplied file
30743106
descriptor. See [`fs.utimes()`][].
30753107
3108+
### `fs.glob(pattern[, options], callback)`
3109+
3110+
<!-- YAML
3111+
added: REPLACEME
3112+
-->
3113+
3114+
> Stability: 1 - Experimental
3115+
3116+
* `pattern` {string|string\[]}
3117+
3118+
* `options` {Object}
3119+
* `cwd` {string} current working directory. **Default:** `process.cwd()`
3120+
* `exclude` {Function} Function to filter out files/directories. Return
3121+
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
3122+
3123+
* `callback` {Function}
3124+
* `err` {Error}
3125+
3126+
* Retrieves the files matching the specified pattern.
3127+
3128+
```mjs
3129+
import { glob } from 'node:fs';
3130+
3131+
glob('**/*.js', (err, matches) => {
3132+
if (err) throw err;
3133+
console.log(matches);
3134+
});
3135+
```
3136+
3137+
```cjs
3138+
const { glob } = require('node:fs');
3139+
3140+
glob('**/*.js', (err, matches) => {
3141+
if (err) throw err;
3142+
console.log(matches);
3143+
});
3144+
```
3145+
30763146
### `fs.lchmod(path, mode, callback)`
30773147
30783148
<!-- YAML
@@ -5529,6 +5599,33 @@ changes:
55295599
55305600
Synchronous version of [`fs.futimes()`][]. Returns `undefined`.
55315601
5602+
### `fs.globSync(pattern[, options])`
5603+
5604+
<!-- YAML
5605+
added: REPLACEME
5606+
-->
5607+
5608+
> Stability: 1 - Experimental
5609+
5610+
* `pattern` {string|string\[]}
5611+
* `options` {Object}
5612+
* `cwd` {string} current working directory. **Default:** `process.cwd()`
5613+
* `exclude` {Function} Function to filter out files/directories. Return
5614+
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
5615+
* Returns: {string\[]} paths of files that match the pattern.
5616+
5617+
```mjs
5618+
import { globSync } from 'node:fs';
5619+
5620+
console.log(globSync('**/*.js'));
5621+
```
5622+
5623+
```cjs
5624+
const { globSync } = require('node:fs');
5625+
5626+
console.log(globSync('**/*.js'));
5627+
```
5628+
55325629
### `fs.lchmodSync(path, mode)`
55335630
55345631
<!-- YAML

lib/fs.js

+36
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ const {
8686
const { toPathIfFileURL } = require('internal/url');
8787
const {
8888
customPromisifyArgs: kCustomPromisifyArgsSymbol,
89+
emitExperimentalWarning,
90+
getLazy,
8991
kEmptyObject,
9092
promisify: {
9193
custom: kCustomPromisifiedSymbol,
@@ -3102,6 +3104,38 @@ function createWriteStream(path, options) {
31023104
return new WriteStream(path, options);
31033105
}
31043106

3107+
const lazyGlob = getLazy(() => require('internal/fs/glob').Glob);
3108+
3109+
function glob(pattern, options, callback) {
3110+
emitExperimentalWarning('glob');
3111+
if (typeof options === 'function') {
3112+
callback = options;
3113+
options = undefined;
3114+
}
3115+
callback = makeCallback(callback);
3116+
3117+
const Glob = lazyGlob();
3118+
// TODO: Use iterator helpers when available
3119+
(async () => {
3120+
try {
3121+
const res = [];
3122+
for await (const entry of new Glob(pattern, options).glob()) {
3123+
ArrayPrototypePush(res, entry);
3124+
}
3125+
callback(null, res);
3126+
} catch (err) {
3127+
callback(err);
3128+
}
3129+
})();
3130+
}
3131+
3132+
function globSync(pattern, options) {
3133+
emitExperimentalWarning('globSync');
3134+
const Glob = lazyGlob();
3135+
return new Glob(pattern, options).globSync();
3136+
}
3137+
3138+
31053139
module.exports = fs = {
31063140
appendFile,
31073141
appendFileSync,
@@ -3135,6 +3169,8 @@ module.exports = fs = {
31353169
ftruncateSync,
31363170
futimes,
31373171
futimesSync,
3172+
glob,
3173+
globSync,
31383174
lchown,
31393175
lchownSync,
31403176
lchmod: constants.O_SYMLINK !== undefined ? lchmod : undefined,

0 commit comments

Comments
 (0)