Skip to content

Commit

Permalink
fs: expose glob and globSync
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Feb 28, 2024
1 parent a51efa2 commit 04a89f9
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 24 deletions.
59 changes: 59 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,38 @@ including subdirectories and files.
When copying a directory to another directory, globs are not supported and
behavior is similar to `cp dir1/ dir2/`.
### `fsPromises.glob(patten[, options])`
<!-- YAML
added: REPLACEME
-->
> Stability: 1 - Experimental
* `patten` {string|string\[]}
* `options` {Object}
* `cwd` {string} working directory. **Default:** `process.cwd()`
* `exclude` {Function} Function to filter out files/directories. Return
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
* Returns: {AsyncIterator} An AsyncIterator that yields the paths of files
that match the pattern.
```mjs
import { glob } from 'node:fs/promises';

for await (const entry of glob('**/*.js'))
console.log(entry);
```
```cjs
const { glob } = require('node:fs/promises');

(async () => {
for await (const entry of glob('**/*.js'))
console.log(entry);
})();
```
### `fsPromises.lchmod(path, mode)`
<!-- YAML
Expand Down Expand Up @@ -5529,6 +5561,33 @@ changes:
Synchronous version of [`fs.futimes()`][]. Returns `undefined`.
### `fs.globSync(patten[, options])`
<!-- YAML
added: REPLACEME
-->
> Stability: 1 - Experimental
* `patten` {string|string\[]}
* `options` {Object}
* `cwd` {string} working directory. **Default:** `process.cwd()`
* `exclude` {Function} Function to filter out files/directories. Return
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
* Returns: {string\[]} paths of files that match the pattern.
```mjs
import { globSync } from 'node:fs';

console.log(globSync('**/*.js'));
```
```cjs
const { globSync } = require('node:fs');

console.log(globSync('**/*.js'));
```
### `fs.lchmodSync(path, mode)`
<!-- YAML
Expand Down
20 changes: 20 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ const {
const { toPathIfFileURL } = require('internal/url');
const {
customPromisifyArgs: kCustomPromisifyArgsSymbol,
emitExperimentalWarning,
getLazy,
kEmptyObject,
promisify: {
custom: kCustomPromisifiedSymbol,
Expand Down Expand Up @@ -142,6 +144,7 @@ const {
validateInteger,
validateObject,
validateString,
validateStringArray,
kValidateObjectAllowNullable,
} = require('internal/validators');

Expand Down Expand Up @@ -3102,6 +3105,22 @@ function createWriteStream(path, options) {
return new WriteStream(path, options);
}

const lazyGlob = getLazy(() => require('internal/fs/glob').Glob);

function globSync(pattern, options) {
emitExperimentalWarning('globSync');
let patterns;
if (typeof pattern === 'object') {
validateStringArray(pattern, 'patterns');
patterns = pattern;
} else {
validateString(pattern, 'patterns');
patterns = [pattern];
}
const Glob = lazyGlob();
return new Glob(patterns, options).globSync();
}

module.exports = fs = {
appendFile,
appendFileSync,
Expand Down Expand Up @@ -3135,6 +3154,7 @@ module.exports = fs = {
ftruncateSync,
futimes,
futimesSync,
globSync,
lchown,
lchownSync,
lchmod: constants.O_SYMLINK !== undefined ? lchmod : undefined,
Expand Down
Loading

0 comments on commit 04a89f9

Please sign in to comment.