7
7
PathScurryPosix ,
8
8
PathScurryWin32 ,
9
9
} from 'path-scurry'
10
- import { fileURLToPath } from 'url'
10
+ import { fileURLToPath } from 'url'
11
11
import { Ignore } from './ignore.js'
12
12
import { Pattern } from './pattern.js'
13
13
import { GlobStream , GlobWalker } from './walker.js'
@@ -24,30 +24,163 @@ const defaultPlatform: NodeJS.Platform =
24
24
? process . platform
25
25
: 'linux'
26
26
27
+ /**
28
+ * A `GlobOptions` object may be provided to any of the exported methods, and
29
+ * must be provided to the `Glob` constructor.
30
+ *
31
+ * All options are optional, boolean, and false by default, unless otherwise
32
+ * noted.
33
+ *
34
+ * All resolved options are added to the Glob object as properties.
35
+ *
36
+ * If you are running many `glob` operations, you can pass a Glob object as the
37
+ * `options` argument to a subsequent operation to share the previously loaded
38
+ * cache.
39
+ */
27
40
export interface GlobOptions {
41
+ /**
42
+ * Set to true to always receive absolute paths for
43
+ * matched files. This does _not_ make an extra system call to get
44
+ * the realpath, it only does string path resolution.
45
+ *
46
+ * By default, when this option is not set, absolute paths are
47
+ * returned for patterns that are absolute, and otherwise paths
48
+ * are returned that are relative to the `cwd` setting.
49
+ *
50
+ * Conflicts with {@link withFileTypes}
51
+ */
28
52
absolute ?: boolean
53
+ /**
54
+ * Set to false to enable {@link windowsPathsNoEscape}
55
+ *
56
+ * @deprecated
57
+ */
29
58
allowWindowsEscape ?: boolean
59
+ /**
60
+ * The current working directory in which to search. Defaults to
61
+ * `process.cwd()`.
62
+ *
63
+ * May be eiher a string path or a `file://` URL object or string.
64
+ */
30
65
cwd ?: string | URL
66
+ /**
67
+ * Include `.dot` files in normal matches and `globstar`
68
+ * matches. Note that an explicit dot in a portion of the pattern
69
+ * will always match dot files.
70
+ */
31
71
dot ?: boolean
72
+ /**
73
+ * Follow symlinked directories when expanding `**`
74
+ * patterns. This can result in a lot of duplicate references in
75
+ * the presence of cyclic links, and make performance quite bad.
76
+ *
77
+ * By default, a `**` in a pattern will follow 1 symbolic link if
78
+ * it is not the first item in the pattern, or none if it is the
79
+ * first item in the pattern, following the same behavior as Bash.
80
+ */
32
81
follow ?: boolean
82
+ /**
83
+ * A glob pattern or array of glob patterns to exclude from matches. To
84
+ * ignore all children within a directory, as well as the entry itself,
85
+ * append `/**'` to the ignore pattern.
86
+ */
33
87
ignore ?: string | string [ ] | Ignore
88
+ /**
89
+ * Add a `/` character to directory matches. Note that this requires
90
+ * additional stat calls in some cases.
91
+ */
34
92
mark ?: boolean
93
+ /**
94
+ * Perform a basename-only match if the pattern does not contain any slash
95
+ * characters. That is, `*.js` would be treated as equivalent to
96
+ * `**\/*.js`, matching all js files in all directories.
97
+ */
35
98
matchBase ?: boolean
99
+ /**
100
+ * Do not expand `{a,b}` and `{1..3}` brace sets.
101
+ */
36
102
nobrace ?: boolean
103
+ /**
104
+ * Perform a case-insensitive match. This defaults to `true` on macOS and
105
+ * Windows systems, and `false` on all others.
106
+ *
107
+ * **Note** `nocase` should only be explicitly set when it is
108
+ * known that the filesystem's case sensitivity differs from the
109
+ * platform default. If set `true` on case-sensitive file
110
+ * systems, or `false` on case-insensitive file systems, then the
111
+ * walk may return more or less results than expected.
112
+ */
37
113
nocase ?: boolean
114
+ /**
115
+ * Do not match directories, only files. (Note: to match
116
+ * _only_ directories, put a `/` at the end of the pattern.)
117
+ */
38
118
nodir ?: boolean
119
+ /**
120
+ * Do not match "extglob" patterns such as `+(a|b)`.
121
+ */
39
122
noext ?: boolean
123
+ /**
124
+ * Do not match `**` against multiple filenames. (Ie, treat it as a normal
125
+ * `*` instead.)
126
+ *
127
+ * Conflicts with {@link matchBase}
128
+ */
40
129
noglobstar ?: boolean
130
+ /**
131
+ * Defaults to value of `process.platform` if available, or `'linux'` if
132
+ * not. Setting `platform:'win32'` on non-Windows systems may cause strange
133
+ * behavior.
134
+ */
41
135
platform ?: NodeJS . Platform
136
+ /**
137
+ * Set to true to call `fs.realpath` on all of the
138
+ * results. In the case of an entry that cannot be resolved, the
139
+ * entry is omitted. This incurs a slight performance penalty, of
140
+ * course, because of the added system calls.
141
+ */
42
142
realpath ?: boolean
143
+ /**
144
+ * A [PathScurry](http://npm.im/path-scurry) object used
145
+ * to traverse the file system. If the `nocase` option is set
146
+ * explicitly, then any provided `scurry` object must match this
147
+ * setting.
148
+ */
43
149
scurry ?: PathScurry
150
+ /**
151
+ * An AbortSignal which will cancel the Glob walk when
152
+ * triggered.
153
+ */
44
154
signal ?: AbortSignal
155
+ /**
156
+ * Use `\\` as a path separator _only_, and
157
+ * _never_ as an escape character. If set, all `\\` characters are
158
+ * replaced with `/` in the pattern.
159
+ *
160
+ * Note that this makes it **impossible** to match against paths
161
+ * containing literal glob pattern characters, but allows matching
162
+ * with patterns constructed using `path.join()` and
163
+ * `path.resolve()` on Windows platforms, mimicking the (buggy!)
164
+ * behavior of Glob v7 and before on Windows. Please use with
165
+ * caution, and be mindful of [the caveat below about Windows
166
+ * paths](#windows). (For legacy reasons, this is also set if
167
+ * `allowWindowsEscape` is set to the exact value `false`.)
168
+ */
45
169
windowsPathsNoEscape ?: boolean
170
+ /**
171
+ * Return [PathScurry](http://npm.im/path-scurry)
172
+ * `Path` objects instead of strings. These are similar to a
173
+ * NodeJS `Dirent` object, but with additional methods and
174
+ * properties.
175
+ *
176
+ * Conflicts with {@link absolute}
177
+ */
46
178
withFileTypes ?: boolean
47
179
}
48
180
49
181
export type GlobOptionsWithFileTypesTrue = GlobOptions & {
50
182
withFileTypes : true
183
+ absolute ?: false
51
184
}
52
185
53
186
export type GlobOptionsWithFileTypesFalse = GlobOptions & {
@@ -75,7 +208,10 @@ export type FileTypes<Opts> = Opts extends GlobOptionsWithFileTypesTrue
75
208
? false
76
209
: boolean
77
210
78
- export class Glob < Opts extends GlobOptions > {
211
+ /**
212
+ * An object that can perform glob pattern traversals.
213
+ */
214
+ export class Glob < Opts extends GlobOptions > implements GlobOptions {
79
215
absolute : boolean
80
216
cwd : string
81
217
dot : boolean
@@ -96,9 +232,28 @@ export class Glob<Opts extends GlobOptions> {
96
232
windowsPathsNoEscape : boolean
97
233
withFileTypes : FileTypes < Opts >
98
234
235
+ /**
236
+ * The options provided to the constructor.
237
+ */
99
238
opts : Opts
239
+
240
+ /**
241
+ * An array of parsed immutable {@link Pattern} objects.
242
+ */
100
243
patterns : Pattern [ ]
101
244
245
+ /**
246
+ * All options are stored as properties on the `Glob` object.
247
+ *
248
+ * See {@link GlobOptions} for full options descriptions.
249
+ *
250
+ * Note that a previous `Glob` object can be passed as the
251
+ * `GlobOptions` to another `Glob` instantiation to re-use settings
252
+ * and caches with a new pattern.
253
+ *
254
+ * Traversal functions can be called multiple times to run the walk
255
+ * again.
256
+ */
102
257
constructor ( pattern : string | string [ ] , opts : Opts ) {
103
258
this . withFileTypes = ! ! opts . withFileTypes as FileTypes < Opts >
104
259
this . signal = opts . signal
@@ -198,6 +353,9 @@ export class Glob<Opts extends GlobOptions> {
198
353
} )
199
354
}
200
355
356
+ /**
357
+ * Returns a Promise that resolves to the results array.
358
+ */
201
359
async walk ( ) : Promise < Results < Opts > >
202
360
async walk ( ) : Promise < ( string | Path ) [ ] > {
203
361
// Walkers always return array of Path objects, so we just have to
@@ -213,6 +371,9 @@ export class Glob<Opts extends GlobOptions> {
213
371
]
214
372
}
215
373
374
+ /**
375
+ * synchronous {@link Glob.walk}
376
+ */
216
377
walkSync ( ) : Results < Opts >
217
378
walkSync ( ) : ( string | Path ) [ ] {
218
379
return [
@@ -224,6 +385,9 @@ export class Glob<Opts extends GlobOptions> {
224
385
]
225
386
}
226
387
388
+ /**
389
+ * Stream results asynchronously.
390
+ */
227
391
stream ( ) : Minipass < Result < Opts > , Result < Opts > >
228
392
stream ( ) : Minipass < string | Path , string | Path > {
229
393
return new GlobStream ( this . patterns , this . scurry . cwd , {
@@ -233,6 +397,9 @@ export class Glob<Opts extends GlobOptions> {
233
397
} ) . stream ( )
234
398
}
235
399
400
+ /**
401
+ * Stream results synchronously.
402
+ */
236
403
streamSync ( ) : Minipass < Result < Opts > , Result < Opts > >
237
404
streamSync ( ) : Minipass < string | Path , string | Path > {
238
405
return new GlobStream ( this . patterns , this . scurry . cwd , {
@@ -242,13 +409,21 @@ export class Glob<Opts extends GlobOptions> {
242
409
} ) . streamSync ( )
243
410
}
244
411
412
+ /**
413
+ * Default sync iteration function. Returns a Generator that
414
+ * iterates over the results.
415
+ */
245
416
iterateSync ( ) : Generator < Result < Opts > , void , void > {
246
417
return this . streamSync ( ) [ Symbol . iterator ] ( )
247
418
}
248
419
[ Symbol . iterator ] ( ) {
249
420
return this . iterateSync ( )
250
421
}
251
422
423
+ /**
424
+ * Default async iteration function. Returns an AsyncGenerator that
425
+ * iterates over the results.
426
+ */
252
427
iterate ( ) : AsyncGenerator < Result < Opts > , void , void > {
253
428
return this . stream ( ) [ Symbol . asyncIterator ] ( )
254
429
}
0 commit comments