Skip to content

Commit 04a89f9

Browse files
committed
fs: expose glob and globSync
1 parent a51efa2 commit 04a89f9

File tree

6 files changed

+355
-24
lines changed

6 files changed

+355
-24
lines changed

doc/api/fs.md

Lines changed: 59 additions & 0 deletions
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(patten[, options])`
1073+
1074+
<!-- YAML
1075+
added: REPLACEME
1076+
-->
1077+
1078+
> Stability: 1 - Experimental
1079+
1080+
* `patten` {string|string\[]}
1081+
* `options` {Object}
1082+
* `cwd` {string} 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
@@ -5529,6 +5561,33 @@ changes:
55295561
55305562
Synchronous version of [`fs.futimes()`][]. Returns `undefined`.
55315563
5564+
### `fs.globSync(patten[, options])`
5565+
5566+
<!-- YAML
5567+
added: REPLACEME
5568+
-->
5569+
5570+
> Stability: 1 - Experimental
5571+
5572+
* `patten` {string|string\[]}
5573+
* `options` {Object}
5574+
* `cwd` {string} working directory. **Default:** `process.cwd()`
5575+
* `exclude` {Function} Function to filter out files/directories. Return
5576+
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
5577+
* Returns: {string\[]} paths of files that match the pattern.
5578+
5579+
```mjs
5580+
import { globSync } from 'node:fs';
5581+
5582+
console.log(globSync('**/*.js'));
5583+
```
5584+
5585+
```cjs
5586+
const { globSync } = require('node:fs');
5587+
5588+
console.log(globSync('**/*.js'));
5589+
```
5590+
55325591
### `fs.lchmodSync(path, mode)`
55335592
55345593
<!-- YAML

lib/fs.js

Lines changed: 20 additions & 0 deletions
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,
@@ -142,6 +144,7 @@ const {
142144
validateInteger,
143145
validateObject,
144146
validateString,
147+
validateStringArray,
145148
kValidateObjectAllowNullable,
146149
} = require('internal/validators');
147150

@@ -3102,6 +3105,22 @@ function createWriteStream(path, options) {
31023105
return new WriteStream(path, options);
31033106
}
31043107

3108+
const lazyGlob = getLazy(() => require('internal/fs/glob').Glob);
3109+
3110+
function globSync(pattern, options) {
3111+
emitExperimentalWarning('globSync');
3112+
let patterns;
3113+
if (typeof pattern === 'object') {
3114+
validateStringArray(pattern, 'patterns');
3115+
patterns = pattern;
3116+
} else {
3117+
validateString(pattern, 'patterns');
3118+
patterns = [pattern];
3119+
}
3120+
const Glob = lazyGlob();
3121+
return new Glob(patterns, options).globSync();
3122+
}
3123+
31053124
module.exports = fs = {
31063125
appendFile,
31073126
appendFileSync,
@@ -3135,6 +3154,7 @@ module.exports = fs = {
31353154
ftruncateSync,
31363155
futimes,
31373156
futimesSync,
3157+
globSync,
31383158
lchown,
31393159
lchownSync,
31403160
lchmod: constants.O_SYMLINK !== undefined ? lchmod : undefined,

0 commit comments

Comments
 (0)