Skip to content

Commit 26df356

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

File tree

1 file changed

+172
-9
lines changed

1 file changed

+172
-9
lines changed

doc/api/fs.md

Lines changed: 172 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,22 +1101,171 @@ 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+
1147+
The following examples demonstrate common glob usage patterns.
1148+
1149+
**Basic pattern matching:**
1150+
1151+
Match all `.js` files in the current directory.
1152+
11041153
```mjs
11051154
import { glob } from 'node:fs/promises';
11061155
1107-
for await (const entry of glob('**/*.js'))
1156+
for await (const entry of glob('*.js'))
11081157
console.log(entry);
11091158
```
11101159
11111160
```cjs
11121161
const { glob } = require('node:fs/promises');
11131162
11141163
(async () => {
1115-
for await (const entry of glob('**/*.js'))
1164+
for await (const entry of glob('*.js'))
11161165
console.log(entry);
11171166
})();
11181167
```
11191168
1169+
**Recursive search with brace expansion:**
1170+
1171+
Match all `.js` and `.ts` files in any subdirectory.
1172+
1173+
```mjs
1174+
import { glob } from 'node:fs/promises';
1175+
1176+
for await (const entry of glob('**/*.{js,ts}'))
1177+
console.log(entry);
1178+
```
1179+
1180+
**Using `Array.fromAsync` to collect results:**
1181+
1182+
Convert the async iterator to an array for easier manipulation.
1183+
1184+
```mjs
1185+
import { glob } from 'node:fs/promises';
1186+
1187+
const files = await Array.fromAsync(glob('src/**/*.js'));
1188+
console.log(files);
1189+
```
1190+
1191+
**Brace expansion for multiple directories:**
1192+
1193+
Match `.js` files in `src/components` or `src/utils` directories.
1194+
1195+
```mjs
1196+
import { glob } from 'node:fs/promises';
1197+
1198+
for await (const entry of glob('src/{components,utils}/**/*.js'))
1199+
console.log(entry);
1200+
```
1201+
1202+
**Extended glob patterns:**
1203+
1204+
Match test files ending with `.spec.js` or `.test.js`. Note that the `+(spec|test)`
1205+
pattern also matches files like `foo.spectest.js`.
1206+
1207+
```mjs
1208+
import { glob } from 'node:fs/promises';
1209+
1210+
for await (const entry of glob('test/**/*.+(spec|test).js'))
1211+
console.log(entry);
1212+
```
1213+
1214+
For more precise matching, use brace expansion instead:
1215+
1216+
```mjs
1217+
import { glob } from 'node:fs/promises';
1218+
1219+
for await (const entry of glob('test/**/*.{spec,test}.js'))
1220+
console.log(entry);
1221+
```
1222+
1223+
**Excluding patterns:**
1224+
1225+
Match all `.js` files except those in `node_modules`.
1226+
1227+
```mjs
1228+
import { glob } from 'node:fs/promises';
1229+
1230+
for await (const entry of glob('**/*.js', { exclude: ['node_modules/**'] }))
1231+
console.log(entry);
1232+
```
1233+
1234+
**Multiple patterns:**
1235+
1236+
Match `.js` files in both `src` and `lib` directories.
1237+
1238+
```mjs
1239+
import { glob } from 'node:fs/promises';
1240+
1241+
for await (const entry of glob(['src/**/*.js', 'lib/**/*.js']))
1242+
console.log(entry);
1243+
```
1244+
1245+
#### Platform Considerations
1246+
1247+
* **Case sensitivity**: Glob patterns are case-insensitive on Windows and macOS,
1248+
case-sensitive on other platforms.
1249+
* **Path separators**: Forward slashes (`/`) are used in patterns regardless of
1250+
platform; they are automatically converted to the appropriate separator.
1251+
* **Returned paths**: Results use platform-specific path separators (`\` on Windows,
1252+
`/` on POSIX). Paths are relative to `options.cwd` if provided, otherwise
1253+
relative to the current working directory.
1254+
* **Symlinks**: Symbolic links are followed and their targets are included in results.
1255+
* **Hidden files**: Files starting with `.` are included in results when explicitly
1256+
matched, but not when using `*` or `**` patterns.
1257+
1258+
#### Performance Notes
1259+
1260+
* Results are streamed as an async iterator, allowing for efficient memory usage
1261+
with large result sets.
1262+
* The implementation includes internal caching to avoid duplicate file system operations.
1263+
* For better performance with large directory trees, consider using more specific
1264+
patterns to reduce the search scope.
1265+
1266+
[`path.matchesGlob()`][] supports the same basic glob pattern syntax for
1267+
in-memory path matching, but does not include the `exclude` option.
1268+
11201269
### `fsPromises.lchmod(path, mode)`
11211270
11221271
<!-- YAML
@@ -3149,20 +3298,25 @@ changes:
31493298
description: Add support for `withFileTypes` as an option.
31503299
-->
31513300
3152-
* `pattern` {string|string\[]}
3153-
3301+
* `pattern` {string|string\[]} The glob pattern(s) to match against.
31543302
* `options` {Object}
31553303
* `cwd` {string} current working directory. **Default:** `process.cwd()`
31563304
* `exclude` {Function|string\[]} Function to filter out files/directories or a
31573305
list of glob patterns to be excluded. If a function is provided, return
31583306
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
31593307
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
31603308
`false` otherwise. **Default:** `false`.
3161-
31623309
* `callback` {Function}
31633310
* `err` {Error}
3311+
* `matches` {string\[]|fs.Dirent\[]} An array of paths or Dirent objects that
3312+
match the pattern. String paths use platform-specific separators (`\` on
3313+
Windows, `/` on POSIX) and are relative to `options.cwd` if provided,
3314+
otherwise relative to the current working directory.
3315+
3316+
Retrieves the file paths matching the specified pattern(s).
31643317
3165-
* Retrieves the files matching the specified pattern.
3318+
See `fsPromises.glob()` for detailed information about pattern syntax,
3319+
examples, and behavior.
31663320
31673321
```mjs
31683322
import { glob } from 'node:fs';
@@ -5702,15 +5856,23 @@ changes:
57025856
description: Add support for `withFileTypes` as an option.
57035857
-->
57045858
5705-
* `pattern` {string|string\[]}
5859+
* `pattern` {string|string\[]} The glob pattern(s) to match against.
57065860
* `options` {Object}
57075861
* `cwd` {string} current working directory. **Default:** `process.cwd()`
57085862
* `exclude` {Function|string\[]} Function to filter out files/directories or a
57095863
list of glob patterns to be excluded. If a function is provided, return
57105864
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
57115865
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
57125866
`false` otherwise. **Default:** `false`.
5713-
* Returns: {string\[]} paths of files that match the pattern.
5867+
* Returns: {string\[]|fs.Dirent\[]} An array of paths or Dirent objects that
5868+
match the pattern. String paths use platform-specific separators (`\` on
5869+
Windows, `/` on POSIX) and are relative to `options.cwd` if provided,
5870+
otherwise relative to the current working directory.
5871+
5872+
Retrieves the file paths matching the specified pattern(s).
5873+
5874+
See `fsPromises.glob()` for detailed information about pattern syntax,
5875+
examples, and behavior.
57145876
57155877
```mjs
57165878
import { globSync } from 'node:fs';
@@ -8464,6 +8626,7 @@ the file contents.
84648626
[`fsPromises.utimes()`]: #fspromisesutimespath-atime-mtime
84658627
[`inotify(7)`]: https://man7.org/linux/man-pages/man7/inotify.7.html
84668628
[`kqueue(2)`]: https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
8629+
[`path.matchesGlob()`]: path.md#pathmatchesglobpath-pattern
84678630
[`util.promisify()`]: util.md#utilpromisifyoriginal
84688631
[bigints]: https://tc39.github.io/proposal-bigint
84698632
[caveats]: #caveats

0 commit comments

Comments
 (0)