Skip to content

Commit 45a5d32

Browse files
committed
doc(fs): document fs.glob pattern syntax and usage
Improved the documentation for fs.glob, fs.globSync, and fsPromises.glob by adding comprehensive information about the `pattern` parameter. The update includes: - Detailed explanations of glob syntax: wildcards (*, ?), character sets, globstar (**), brace expansion ({a,b,c}), and extended patterns (+(x), !(x)) - Practical usage examples including recursive patterns, exclusions, and multiple patterns - Platform considerations such as case sensitivity, path separators, symlink handling, and hidden file behavior - Performance notes on caching, memory efficiency, and matching strategies - Improved parameter descriptions and consistent formatting across variants - Added a reference to `path.matchesGlob()` which supports the same glob pattern syntax for in-memory path matching Fixes: #58981
1 parent 9148e96 commit 45a5d32

File tree

1 file changed

+91
-9
lines changed

1 file changed

+91
-9
lines changed

doc/api/fs.md

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ changes:
10901090
description: Add support for `withFileTypes` as an option.
10911091
-->
10921092
1093-
* `pattern` {string|string\[]}
1093+
* `pattern` {string|string\[]} The glob pattern(s) to match against.
10941094
* `options` {Object}
10951095
* `cwd` {string} current working directory. **Default:** `process.cwd()`
10961096
* `exclude` {Function|string\[]} Function to filter out files/directories or a
@@ -1101,11 +1101,62 @@ changes:
11011101
* Returns: {AsyncIterator} An AsyncIterator that yields the paths of files
11021102
that match the pattern.
11031103
1104+
Retrieves the files matching the specified pattern(s).
1105+
1106+
#### Pattern Syntax
1107+
1108+
The glob implementation supports the following pattern syntax:
1109+
1110+
**Basic Wildcards:**
1111+
* `*` - Matches any number of characters within a single path segment
1112+
* `?` - Matches exactly one character
1113+
* `[abc]` - Matches any character in the brackets
1114+
* `[a-z]` - Matches any character in the range
1115+
* `[!abc]` or `[^abc]` - Matches any character not in the brackets
1116+
1117+
**Globstar:**
1118+
* `**` - Matches zero or more directories recursively
1119+
* `**/*.js` - Matches all `.js` files in any subdirectory
1120+
1121+
**Brace Expansion:**
1122+
* `{a,b,c}` - Matches any of the comma-separated patterns
1123+
* `{1..5}` - Matches any number in the range (1, 2, 3, 4, 5)
1124+
* `*.{js,ts}` - Matches files with `.js` or `.ts` extensions
1125+
1126+
**Extended Glob Patterns:**
1127+
* `+(pattern)` - Matches one or more occurrences of the pattern
1128+
* `*(pattern)` - Matches zero or more occurrences of the pattern
1129+
* `?(pattern)` - Matches zero or one occurrence of the pattern
1130+
* `!(pattern)` - Matches anything except the pattern
1131+
1132+
#### Examples
1133+
11041134
```mjs
11051135
import { glob } from 'node:fs/promises';
11061136
1107-
for await (const entry of glob('**/*.js'))
1108-
console.log(entry);
1137+
// Basic patterns
1138+
for await (const entry of glob('*.js'))
1139+
console.log(entry); // All .js files in current directory
1140+
1141+
// Recursive search
1142+
for await (const entry of glob('**/*.{js,ts}'))
1143+
console.log(entry); // All .js and .ts files in any subdirectory
1144+
1145+
// Brace expansion
1146+
for await (const entry of glob('src/{components,utils}/**/*.js'))
1147+
console.log(entry); // .js files in src/components or src/utils
1148+
1149+
// Extended glob patterns
1150+
for await (const entry of glob('test/**/*.+(spec|test).js'))
1151+
console.log(entry); // Test files ending with .spec.js or .test.js
1152+
1153+
// Excluding patterns
1154+
for await (const entry of glob('**/*.js', { exclude: ['node_modules/**'] }))
1155+
console.log(entry); // All .js files except those in node_modules
1156+
1157+
// Multiple patterns
1158+
for await (const entry of glob(['src/**/*.js', 'lib/**/*.js']))
1159+
console.log(entry); // .js files in both src and lib directories
11091160
```
11101161
11111162
```cjs
@@ -1117,6 +1168,27 @@ const { glob } = require('node:fs/promises');
11171168
})();
11181169
```
11191170
1171+
#### Platform Considerations
1172+
1173+
* **Case sensitivity**: Glob patterns are case-insensitive on Windows and macOS,
1174+
case-sensitive on other platforms
1175+
* **Path separators**: Forward slashes (`/`) are used in patterns regardless of
1176+
platform; they are automatically converted to the appropriate separator
1177+
* **Symlinks**: Symbolic links are followed and their targets are included in results
1178+
* **Hidden files**: Files starting with `.` are included in results when explicitly
1179+
matched, but not when using `*` or `**` patterns
1180+
1181+
#### Performance Notes
1182+
1183+
* Results are streamed as an async iterator, allowing for efficient memory usage
1184+
with large result sets
1185+
* The implementation includes internal caching to avoid duplicate filesystem operations
1186+
* For better performance with large directory trees, consider using more specific
1187+
patterns to reduce the search scope
1188+
1189+
The same glob pattern syntax is supported by [`path.matchesGlob()`][] for
1190+
matching paths without file system access.
1191+
11201192
### `fsPromises.lchmod(path, mode)`
11211193
11221194
<!-- YAML
@@ -3149,20 +3221,23 @@ changes:
31493221
description: Add support for `withFileTypes` as an option.
31503222
-->
31513223
3152-
* `pattern` {string|string\[]}
3153-
3224+
* `pattern` {string|string\[]} The glob pattern(s) to match against.
31543225
* `options` {Object}
31553226
* `cwd` {string} current working directory. **Default:** `process.cwd()`
31563227
* `exclude` {Function|string\[]} Function to filter out files/directories or a
31573228
list of glob patterns to be excluded. If a function is provided, return
31583229
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
31593230
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
31603231
`false` otherwise. **Default:** `false`.
3161-
31623232
* `callback` {Function}
31633233
* `err` {Error}
3234+
* `matches` {string\[]|Dirent\[]} An array of paths or Dirent objects that
3235+
match the pattern.
3236+
3237+
Retrieves the files matching the specified pattern(s).
31643238
3165-
* Retrieves the files matching the specified pattern.
3239+
See [`fsPromises.glob()`][] for detailed information about pattern syntax,
3240+
examples, and behavior.
31663241
31673242
```mjs
31683243
import { glob } from 'node:fs';
@@ -5702,15 +5777,21 @@ changes:
57025777
description: Add support for `withFileTypes` as an option.
57035778
-->
57045779
5705-
* `pattern` {string|string\[]}
5780+
* `pattern` {string|string\[]} The glob pattern(s) to match against.
57065781
* `options` {Object}
57075782
* `cwd` {string} current working directory. **Default:** `process.cwd()`
57085783
* `exclude` {Function|string\[]} Function to filter out files/directories or a
57095784
list of glob patterns to be excluded. If a function is provided, return
57105785
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
57115786
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
57125787
`false` otherwise. **Default:** `false`.
5713-
* Returns: {string\[]} paths of files that match the pattern.
5788+
* Returns: {string\[]|Dirent\[]} An array of paths or Dirent objects that
5789+
match the pattern.
5790+
5791+
Retrieves the files matching the specified pattern(s).
5792+
5793+
See [`fsPromises.glob()`][] for detailed information about pattern syntax,
5794+
examples, and behavior.
57145795
57155796
```mjs
57165797
import { globSync } from 'node:fs';
@@ -8464,6 +8545,7 @@ the file contents.
84648545
[`fsPromises.utimes()`]: #fspromisesutimespath-atime-mtime
84658546
[`inotify(7)`]: https://man7.org/linux/man-pages/man7/inotify.7.html
84668547
[`kqueue(2)`]: https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
8548+
[`path.matchesGlob()`]: path.md#pathmatchesglobpath-pattern
84678549
[`util.promisify()`]: util.md#utilpromisifyoriginal
84688550
[bigints]: https://tc39.github.io/proposal-bigint
84698551
[caveats]: #caveats

0 commit comments

Comments
 (0)