From 04a89f959c885a86d82ca07f759977d2164349e7 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Wed, 28 Feb 2024 10:37:29 +0200 Subject: [PATCH] fs: expose glob and globSync --- doc/api/fs.md | 59 +++++ lib/fs.js | 20 ++ lib/internal/fs/glob.js | 244 +++++++++++++++++- lib/internal/fs/promises.js | 20 +- test/parallel/test-fs-glob.mjs | 35 ++- test/parallel/test-permission-fs-supported.js | 1 + 6 files changed, 355 insertions(+), 24 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 4bd7da6de39030..64e106bb12c48b 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -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])` + + + +> 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)` + +> 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)`