Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: expose glob and globSync #51912

Merged
merged 3 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 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(pattern[, options])`
MoLow marked this conversation as resolved.
Show resolved Hide resolved
<!-- YAML
added: REPLACEME
-->
> Stability: 1 - Experimental
* `pattern` {string|string\[]}
* `options` {Object}
* `cwd` {string} current 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 @@ -3073,6 +3105,44 @@ changes:
Change the file system timestamps of the object referenced by the supplied file
descriptor. See [`fs.utimes()`][].
### `fs.glob(pattern[, options], callback)`
<!-- YAML
added: REPLACEME
-->
> Stability: 1 - Experimental
* `pattern` {string|string\[]}
* `options` {Object}
* `cwd` {string} current 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`.
* `callback` {Function}
* `err` {Error}
* Retrieves the files matching the specified pattern.
```mjs
import { glob } from 'node:fs';
glob('**/*.js', (err, matches) => {
if (err) throw err;
console.log(matches);
});
```
```cjs
const { glob } = require('node:fs');
glob('**/*.js', (err, matches) => {
if (err) throw err;
console.log(matches);
});
```
### `fs.lchmod(path, mode, callback)`
<!-- YAML
Expand Down Expand Up @@ -5529,6 +5599,33 @@ changes:
Synchronous version of [`fs.futimes()`][]. Returns `undefined`.
### `fs.globSync(pattern[, options])`
<!-- YAML
added: REPLACEME
-->
> Stability: 1 - Experimental
* `pattern` {string|string\[]}
* `options` {Object}
* `cwd` {string} current 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'));
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to have more details here about what glob patterns are supported, like there is in https://github.com/isaacs/node-glob?tab=readme-ov-file#glob-primer

Since glob implementations can vary and it may not be obvious if a certain pattern is supported (that is not just ** and *)

### `fs.lchmodSync(path, mode)`
<!-- YAML
Expand Down
36 changes: 36 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 @@ -3102,6 +3104,38 @@ function createWriteStream(path, options) {
return new WriteStream(path, options);
}

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

function glob(pattern, options, callback) {
emitExperimentalWarning('glob');
if (typeof options === 'function') {
callback = options;
options = undefined;
}
callback = makeCallback(callback);

const Glob = lazyGlob();
// TODO: Use iterator helpers when available
(async () => {
try {
const res = [];
for await (const entry of new Glob(pattern, options).glob()) {
ArrayPrototypePush(res, entry);
}
callback(null, res);
} catch (err) {
callback(err);
}
})();
}

function globSync(pattern, options) {
emitExperimentalWarning('globSync');
const Glob = lazyGlob();
return new Glob(pattern, options).globSync();
}


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