-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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.glob add option to only include files (not directories) in the results entries #52752
Comments
Maybe just expose the dirent insstead of its name to exclude which would enable: fs.promises.glob("foo/**", { exclude(entry) { return entry.isFile(); }) |
You mean But then we don't have information about filename/path. |
we should probably just add a |
If I want to return strings (because they're relative to the cwd) and ensure they are all files, then is the code for (const dirent of fs.globSync('**', { cwd: path, withFileTypes: true })) {
if (dirent.isFile()) {
const name = path.relative(this.path, path.resolve(dirent.parentPath, dirent.name));
dict[name] = {};
}
} a bit too complicated? |
Or: const files = (await Array.fromAsync(fs.glob(path.join(directory, "**/*.js"), { "withFileTypes": true }))).filter((file) => file.isFile());
console.log(files); |
What is the problem this feature will solve?
Since Node.js v22, with this PR: #51912, it is possible to use
fs.promises.glob
for matching file paths based on specified patterns.However, the results of entries also includes directories, but other famous userland library (e.g: globby) only returns files (not directories).
Example
With a file structure like the following:
$ mkdir -p foo/bar && touch foo/bar.md $ tree foo foo ├── bar └── bar.md 2 directories, 1 file
And the following code:
It prints:
What is the feature you are proposing to solve the problem?
Add 2 options to
fs.glob
:onlyDirectories
, boolean, default tofalse
.onlyFiles
, boolean, default tofalse
.Both default to
false
, to keep same behavior, so no breaking changes is introduced.Options based on the fast-glob library which
globby
uses under the hood.What alternatives have you considered?
The text was updated successfully, but these errors were encountered: