Skip to content

Commit a547a9c

Browse files
committed
more docs
1 parent 42a3ac7 commit a547a9c

File tree

8 files changed

+291
-16
lines changed

8 files changed

+291
-16
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,8 @@ share the previously loaded cache.
200200

201201
- `cwd` String path or `file://` string or URL object. The
202202
current working directory in which to search. Defaults to
203-
`process.cwd()`. This option is always coerced to use
204-
forward-slashes as a path separator, because it is not tested
205-
as a glob pattern, so there is no need to escape anything. See
206-
also: "Windows, CWDs, Drive Letters, and UNC Paths", below.
203+
`process.cwd()`. See also: "Windows, CWDs, Drive Letters, and
204+
UNC Paths", below.
207205

208206
This option may be eiher a string path or a `file://` URL
209207
object or string.
@@ -354,8 +352,8 @@ path separators (ie, `/` on all platforms, and `\` on Windows).
354352
or 0 if it is the first item, unless `follow:true` is set, in
355353
which case it follows all symbolic links.
356354

357-
`[:class:]`, `[=c=]`, and `[.symbol.]` style class patterns are
358-
_not_ supported by this implementation at this time.
355+
`[:class:]` patterns are supported by this implementation, but
356+
`[=c=]` and `[.symbol.]` style class patterns are not.
359357

360358
### Dots
361359

src/glob.ts

Lines changed: 177 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
PathScurryPosix,
88
PathScurryWin32,
99
} from 'path-scurry'
10-
import {fileURLToPath} from 'url'
10+
import { fileURLToPath } from 'url'
1111
import { Ignore } from './ignore.js'
1212
import { Pattern } from './pattern.js'
1313
import { GlobStream, GlobWalker } from './walker.js'
@@ -24,30 +24,163 @@ const defaultPlatform: NodeJS.Platform =
2424
? process.platform
2525
: 'linux'
2626

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+
*/
2740
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+
*/
2852
absolute?: boolean
53+
/**
54+
* Set to false to enable {@link windowsPathsNoEscape}
55+
*
56+
* @deprecated
57+
*/
2958
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+
*/
3065
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+
*/
3171
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+
*/
3281
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+
*/
3387
ignore?: string | string[] | Ignore
88+
/**
89+
* Add a `/` character to directory matches. Note that this requires
90+
* additional stat calls in some cases.
91+
*/
3492
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+
*/
3598
matchBase?: boolean
99+
/**
100+
* Do not expand `{a,b}` and `{1..3}` brace sets.
101+
*/
36102
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+
*/
37113
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+
*/
38118
nodir?: boolean
119+
/**
120+
* Do not match "extglob" patterns such as `+(a|b)`.
121+
*/
39122
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+
*/
40129
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+
*/
41135
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+
*/
42142
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+
*/
43149
scurry?: PathScurry
150+
/**
151+
* An AbortSignal which will cancel the Glob walk when
152+
* triggered.
153+
*/
44154
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+
*/
45169
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+
*/
46178
withFileTypes?: boolean
47179
}
48180

49181
export type GlobOptionsWithFileTypesTrue = GlobOptions & {
50182
withFileTypes: true
183+
absolute?: false
51184
}
52185

53186
export type GlobOptionsWithFileTypesFalse = GlobOptions & {
@@ -75,7 +208,10 @@ export type FileTypes<Opts> = Opts extends GlobOptionsWithFileTypesTrue
75208
? false
76209
: boolean
77210

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 {
79215
absolute: boolean
80216
cwd: string
81217
dot: boolean
@@ -96,9 +232,28 @@ export class Glob<Opts extends GlobOptions> {
96232
windowsPathsNoEscape: boolean
97233
withFileTypes: FileTypes<Opts>
98234

235+
/**
236+
* The options provided to the constructor.
237+
*/
99238
opts: Opts
239+
240+
/**
241+
* An array of parsed immutable {@link Pattern} objects.
242+
*/
100243
patterns: Pattern[]
101244

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+
*/
102257
constructor(pattern: string | string[], opts: Opts) {
103258
this.withFileTypes = !!opts.withFileTypes as FileTypes<Opts>
104259
this.signal = opts.signal
@@ -198,6 +353,9 @@ export class Glob<Opts extends GlobOptions> {
198353
})
199354
}
200355

356+
/**
357+
* Returns a Promise that resolves to the results array.
358+
*/
201359
async walk(): Promise<Results<Opts>>
202360
async walk(): Promise<(string | Path)[]> {
203361
// Walkers always return array of Path objects, so we just have to
@@ -213,6 +371,9 @@ export class Glob<Opts extends GlobOptions> {
213371
]
214372
}
215373

374+
/**
375+
* synchronous {@link Glob.walk}
376+
*/
216377
walkSync(): Results<Opts>
217378
walkSync(): (string | Path)[] {
218379
return [
@@ -224,6 +385,9 @@ export class Glob<Opts extends GlobOptions> {
224385
]
225386
}
226387

388+
/**
389+
* Stream results asynchronously.
390+
*/
227391
stream(): Minipass<Result<Opts>, Result<Opts>>
228392
stream(): Minipass<string | Path, string | Path> {
229393
return new GlobStream(this.patterns, this.scurry.cwd, {
@@ -233,6 +397,9 @@ export class Glob<Opts extends GlobOptions> {
233397
}).stream()
234398
}
235399

400+
/**
401+
* Stream results synchronously.
402+
*/
236403
streamSync(): Minipass<Result<Opts>, Result<Opts>>
237404
streamSync(): Minipass<string | Path, string | Path> {
238405
return new GlobStream(this.patterns, this.scurry.cwd, {
@@ -242,13 +409,21 @@ export class Glob<Opts extends GlobOptions> {
242409
}).streamSync()
243410
}
244411

412+
/**
413+
* Default sync iteration function. Returns a Generator that
414+
* iterates over the results.
415+
*/
245416
iterateSync(): Generator<Result<Opts>, void, void> {
246417
return this.streamSync()[Symbol.iterator]()
247418
}
248419
[Symbol.iterator]() {
249420
return this.iterateSync()
250421
}
251422

423+
/**
424+
* Default async iteration function. Returns an AsyncGenerator that
425+
* iterates over the results.
426+
*/
252427
iterate(): AsyncGenerator<Result<Opts>, void, void> {
253428
return this.stream()[Symbol.asyncIterator]()
254429
}

src/has-magic.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { Glob, GlobOptions } from './glob.js'
22

3+
/**
4+
* Return true if the patterns provided contain any magic
5+
* glob characters.
6+
*/
37
export const hasMagic = (
48
pattern: string | string[],
59
options: GlobOptions = {}

src/ignore.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const defaultPlatform: NodeJS.Platform =
1515
? process.platform
1616
: 'linux'
1717

18+
/**
19+
* Class used to process ignored patterns
20+
*/
1821
export class Ignore {
1922
relative: Minimatch[]
2023
relativeChildren: Minimatch[]

src/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import type {
1515
Result,
1616
} from './walker.js'
1717

18+
/**
19+
* Syncronous form of {@link globStream}. Will read all the matches as fast as
20+
* you consume them, even all in a single tick if you consume them immediately,
21+
* but will still respond to backpressure if they're not consumed immediately.
22+
*/
1823
export function globStreamSync(
1924
pattern: string | string[],
2025
options: GlobOptionsWithFileTypesTrue
@@ -38,6 +43,10 @@ export function globStreamSync(
3843
return new Glob(pattern, options).streamSync()
3944
}
4045

46+
/**
47+
* Return a stream that emits all the strings or `Path` objects and
48+
* then emits `end` when completed.
49+
*/
4150
export function globStream(
4251
pattern: string | string[],
4352
options: GlobOptionsWithFileTypesFalse
@@ -61,6 +70,9 @@ export function globStream(
6170
return new Glob(pattern, options).stream()
6271
}
6372

73+
/**
74+
* Synchronous form of {@link glob}
75+
*/
6476
export function globSync(
6577
pattern: string | string[],
6678
options: GlobOptionsWithFileTypesFalse
@@ -84,6 +96,12 @@ export function globSync(
8496
return new Glob(pattern, options).walkSync()
8597
}
8698

99+
/**
100+
* Perform an asynchronous glob search for the pattern(s) specified. Returns
101+
* [Path](https://isaacs.github.io/path-scurry/classes/PathBase) objects if the
102+
* {@link withFileTypes} option is set to `true`. See {@link GlobOptions} for
103+
* full option descriptions.
104+
*/
87105
export async function glob(
88106
pattern: string | string[],
89107
options?: GlobOptionsWithFileTypesUnset | undefined
@@ -107,6 +125,9 @@ export async function glob(
107125
return new Glob(pattern, options).walk()
108126
}
109127

128+
/**
129+
* Return an async iterator for walking glob pattern matches.
130+
*/
110131
export function globIterate(
111132
pattern: string | string[],
112133
options?: GlobOptionsWithFileTypesUnset | undefined
@@ -130,6 +151,9 @@ export function globIterate(
130151
return new Glob(pattern, options).iterate()
131152
}
132153

154+
/**
155+
* Return a sync iterator for walking glob pattern matches.
156+
*/
133157
export function globIterateSync(
134158
pattern: string | string[],
135159
options?: GlobOptionsWithFileTypesUnset | undefined

0 commit comments

Comments
 (0)