Skip to content

Commit 92fb0eb

Browse files
committed
doc,fs: improve glob docs per review feedback
1 parent 9148e96 commit 92fb0eb

File tree

1 file changed

+105
-7
lines changed

1 file changed

+105
-7
lines changed

doc/api/fs.md

Lines changed: 105 additions & 7 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,6 +1101,49 @@ changes:
11011101
* Returns: {AsyncIterator} An AsyncIterator that yields the paths of files
11021102
that match the pattern.
11031103
1104+
Retrieves the file paths matching the specified pattern(s).
1105+
1106+
#### Pattern Syntax
1107+
1108+
The glob implementation supports the following pattern syntax:
1109+
1110+
**Basic Wildcards:**
1111+
1112+
| Pattern | Description |
1113+
| -------------------- | -------------------------------------------------------------- |
1114+
| `*` | Matches any number of characters within a single path segment. |
1115+
| `?` | Matches exactly one character. |
1116+
| `[abc]` | Matches any character in the brackets. |
1117+
| `[a-z]` | Matches any character in the range. |
1118+
| `[!abc]` or `[^abc]` | Matches any character not in the brackets. |
1119+
| `[!a-z]` or `[^a-z]` | Matches any character not in the range. |
1120+
1121+
**Globstar:**
1122+
1123+
| Pattern | Description |
1124+
| --------- | --------------------------------------------- |
1125+
| `**` | Matches zero or more directories recursively. |
1126+
| `**/*.js` | Matches all `.js` files in any subdirectory. |
1127+
1128+
**Brace Expansion:**
1129+
1130+
| Pattern | Description |
1131+
| ----------- | ------------------------------------------------ |
1132+
| `{a,b,c}` | Matches any of the comma-separated patterns. |
1133+
| `{1..5}` | Matches any number in the range (1, 2, 3, 4, 5). |
1134+
| `*.{js,ts}` | Matches files with `.js` or `.ts` extensions. |
1135+
1136+
**Extended Glob Patterns:**
1137+
1138+
| Pattern | Description |
1139+
| ------------ | ------------------------------------------------ |
1140+
| `+(pattern)` | Matches one or more occurrences of the pattern. |
1141+
| `*(pattern)` | Matches zero or more occurrences of the pattern. |
1142+
| `?(pattern)` | Matches zero or one occurrence of the pattern. |
1143+
| `!(pattern)` | Matches anything except the pattern. |
1144+
1145+
#### Examples
1146+
11041147
```mjs
11051148
import { glob } from 'node:fs/promises';
11061149
@@ -1117,6 +1160,47 @@ const { glob } = require('node:fs/promises');
11171160
})();
11181161
```
11191162
1163+
To collect all results into an array, use `Array.fromAsync`:
1164+
1165+
```mjs
1166+
const files = await Array.fromAsync(glob('src/**/*.js'));
1167+
```
1168+
1169+
**Common patterns:**
1170+
1171+
| Pattern | Description |
1172+
| --------------------------------------------------- | ------------------------------------------------ |
1173+
| `glob('*.js')` | All `.js` files in the current directory. |
1174+
| `glob('**/*.{js,ts}')` | All `.js` and `.ts` files recursively. |
1175+
| `glob('src/{components,utils}/**/*.js')` | `.js` files in `src/components` or `src/utils`. |
1176+
| `glob('test/**/*.{spec,test}.js')` | Test files ending with `.spec.js` or `.test.js`. |
1177+
| `glob('**/*.js', { exclude: ['node_modules/**'] })` | All `.js` files except those in `node_modules`. |
1178+
| `glob(['src/**/*.js', 'lib/**/*.js'])` | `.js` files in both `src` and `lib` directories. |
1179+
1180+
#### Platform Considerations
1181+
1182+
* **Case sensitivity**: Glob patterns are case-insensitive on Windows and macOS,
1183+
case-sensitive on other platforms.
1184+
* **Path separators**: Forward slashes (`/`) are used in patterns regardless of
1185+
platform; they are automatically converted to the appropriate separator.
1186+
* **Returned paths**: Results use platform-specific path separators (`\` on Windows,
1187+
`/` on POSIX). Paths are relative to `options.cwd` if provided, otherwise
1188+
relative to the current working directory.
1189+
* **Symlinks**: Symbolic links are followed and their targets are included in results.
1190+
* **Hidden files**: Files starting with `.` are included in results when explicitly
1191+
matched, but not when using `*` or `**` patterns.
1192+
1193+
#### Performance Notes
1194+
1195+
* Results are streamed as an async iterator, allowing for efficient memory usage
1196+
with large result sets.
1197+
* The implementation includes internal caching to avoid duplicate file system operations.
1198+
* For better performance with large directory trees, consider using more specific
1199+
patterns to reduce the search scope.
1200+
1201+
[`path.matchesGlob()`][] supports the same basic glob pattern syntax for
1202+
in-memory path matching, but does not include the `exclude` option.
1203+
11201204
### `fsPromises.lchmod(path, mode)`
11211205
11221206
<!-- YAML
@@ -3149,20 +3233,25 @@ changes:
31493233
description: Add support for `withFileTypes` as an option.
31503234
-->
31513235
3152-
* `pattern` {string|string\[]}
3153-
3236+
* `pattern` {string|string\[]} The glob pattern(s) to match against.
31543237
* `options` {Object}
31553238
* `cwd` {string} current working directory. **Default:** `process.cwd()`
31563239
* `exclude` {Function|string\[]} Function to filter out files/directories or a
31573240
list of glob patterns to be excluded. If a function is provided, return
31583241
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
31593242
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
31603243
`false` otherwise. **Default:** `false`.
3161-
31623244
* `callback` {Function}
31633245
* `err` {Error}
3246+
* `matches` {string\[]|fs.Dirent\[]} An array of paths or Dirent objects that
3247+
match the pattern. String paths use platform-specific separators (`\` on
3248+
Windows, `/` on POSIX) and are relative to `options.cwd` if provided,
3249+
otherwise relative to the current working directory.
3250+
3251+
Retrieves the file paths matching the specified pattern(s).
31643252
3165-
* Retrieves the files matching the specified pattern.
3253+
See `fsPromises.glob()` for detailed information about pattern syntax,
3254+
examples, and behavior.
31663255
31673256
```mjs
31683257
import { glob } from 'node:fs';
@@ -5702,15 +5791,23 @@ changes:
57025791
description: Add support for `withFileTypes` as an option.
57035792
-->
57045793
5705-
* `pattern` {string|string\[]}
5794+
* `pattern` {string|string\[]} The glob pattern(s) to match against.
57065795
* `options` {Object}
57075796
* `cwd` {string} current working directory. **Default:** `process.cwd()`
57085797
* `exclude` {Function|string\[]} Function to filter out files/directories or a
57095798
list of glob patterns to be excluded. If a function is provided, return
57105799
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
57115800
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
57125801
`false` otherwise. **Default:** `false`.
5713-
* Returns: {string\[]} paths of files that match the pattern.
5802+
* Returns: {string\[]|fs.Dirent\[]} An array of paths or Dirent objects that
5803+
match the pattern. String paths use platform-specific separators (`\` on
5804+
Windows, `/` on POSIX) and are relative to `options.cwd` if provided,
5805+
otherwise relative to the current working directory.
5806+
5807+
Retrieves the file paths matching the specified pattern(s).
5808+
5809+
See `fsPromises.glob()` for detailed information about pattern syntax,
5810+
examples, and behavior.
57145811
57155812
```mjs
57165813
import { globSync } from 'node:fs';
@@ -8464,6 +8561,7 @@ the file contents.
84648561
[`fsPromises.utimes()`]: #fspromisesutimespath-atime-mtime
84658562
[`inotify(7)`]: https://man7.org/linux/man-pages/man7/inotify.7.html
84668563
[`kqueue(2)`]: https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
8564+
[`path.matchesGlob()`]: path.md#pathmatchesglobpath-pattern
84678565
[`util.promisify()`]: util.md#utilpromisifyoriginal
84688566
[bigints]: https://tc39.github.io/proposal-bigint
84698567
[caveats]: #caveats

0 commit comments

Comments
 (0)