The Glob class provides an object-oriented interface for glob operations with support for cache reuse and multiple execution methods.
class Glob {
constructor(
pattern: string | string[],
options?: GlobOptions | Glob
)
}| Parameter | Type | Description |
|---|---|---|
pattern |
string | string[] |
A glob pattern or array of patterns |
options |
GlobOptions | Glob |
Options object or another Glob instance for cache reuse |
class Glob {
readonly pattern: string[] // Patterns (always an array)
readonly options: GlobOptions // Resolved options
}Execute the glob and return all matching paths.
walk(): Promise<string[]>
walkSync(): string[]Execute the glob and return a Minipass stream.
stream(): Minipass<string, string>
streamSync(): Minipass<string, string>Execute the glob and return a generator.
iterate(): AsyncGenerator<string, void, void>
iterateSync(): Generator<string, void, void>The Glob class implements both sync and async iterator protocols:
// Built-in methods
[Symbol.asyncIterator](): AsyncGenerator<string, void, void>
[Symbol.iterator](): Generator<string, void, void>import { Glob } from 'globlin'
const g = new Glob('**/*.js', { cwd: '/project' })
// Async iteration
for await (const file of g) {
console.log(file)
}
// Sync iteration
for (const file of g) {
console.log(file)
}import { Glob } from 'globlin'
// Create a Glob instance
const g = new Glob('**/*.js', {
cwd: '/path/to/project',
ignore: ['node_modules/**']
})
// Get all results at once
const files = await g.walk()
// Or synchronously
const filesSync = g.walkSync()import { Glob } from 'globlin'
const g = new Glob(['**/*.ts', '**/*.tsx'], {
cwd: '/project/src',
ignore: ['**/*.test.ts', '**/*.test.tsx']
})
const typeScriptFiles = await g.walk()import { Glob } from 'globlin'
const g = new Glob('**/*.log')
// Async stream
const stream = g.stream()
stream.on('data', (file) => console.log(file))
stream.on('end', () => console.log('Done'))
// Sync stream
const syncStream = g.streamSync()
for (const file of syncStream) {
console.log(file)
}import { Glob } from 'globlin'
const g = new Glob('**/*.js')
// Using iterate() method
for await (const file of g.iterate()) {
console.log(file)
}
// Using Symbol.asyncIterator
for await (const file of g) {
console.log(file)
}
// Sync iteration
for (const file of g.iterateSync()) {
console.log(file)
}
// Using Symbol.iterator
for (const file of g) {
console.log(file)
}Pass a Glob instance as the options parameter to reuse its settings. This is useful when running multiple glob operations with similar configurations.
import { Glob } from 'globlin'
// First glob with specific settings
const g1 = new Glob('**/*.js', {
cwd: '/project',
ignore: ['node_modules/**'],
dot: true,
nocase: true
})
const jsFiles = await g1.walk()
// Reuse g1's settings for a different pattern
// This copies all options: cwd, ignore, dot, nocase, etc.
const g2 = new Glob('**/*.ts', g1)
const tsFiles = await g2.walk()
// Chain multiple globs with shared settings
const g3 = new Glob('**/*.json', g2) // Same settings as g1 and g2When passing a Glob as options, the following are copied:
| Option | Description |
|---|---|
cwd |
Working directory |
root |
Root directory |
dot |
Include dotfiles |
nobrace |
Disable braces |
noglobstar |
Disable globstar |
noext |
Disable extglobs |
nocase |
Case-insensitive |
follow |
Follow symlinks |
maxDepth |
Maximum depth |
matchBase |
Match basename |
absolute |
Return absolute paths |
dotRelative |
Prepend ./ |
mark |
Append / to dirs |
nodir |
Exclude directories |
posix |
POSIX paths |
stat |
Always stat |
realpath |
Resolve symlinks |
ignore |
Ignore patterns |
platform |
Platform |
windowsPathsNoEscape |
Windows escaping |
parallel |
Parallel walking |
cache |
Directory caching |
- The pattern itself (you provide a new pattern)
signal(AbortSignal is not copied)- Internal caches (pattern cache is global anyway)
The Glob class validates options in the constructor:
import { Glob } from 'globlin'
// Error: cannot set absolute and withFileTypes:true
new Glob('**/*', { withFileTypes: true, absolute: true })
// Error: base matching requires globstar
new Glob('*.js', { matchBase: true, noglobstar: true })class Glob {
readonly pattern: string[]
readonly options: GlobOptions
constructor(pattern: string | string[], options?: GlobOptions | Glob)
walk(): Promise<string[]>
walkSync(): string[]
stream(): Minipass<string, string>
streamSync(): Minipass<string, string>
iterate(): AsyncGenerator<string, void, void>
iterateSync(): Generator<string, void, void>
[Symbol.asyncIterator](): AsyncGenerator<string, void, void>
[Symbol.iterator](): Generator<string, void, void>
}| Method | Returns | Async | Memory | Use Case |
|---|---|---|---|---|
walk() |
Promise<string[]> |
Yes | Higher | Need all results as array |
walkSync() |
string[] |
No | Higher | Sync code, need array |
stream() |
Minipass |
Yes | Lower | Piping, large results |
streamSync() |
Minipass |
No | Lower | Sync stream processing |
iterate() |
AsyncGenerator |
Yes | Lower | for await...of syntax |
iterateSync() |
Generator |
No | Lower | for...of syntax |
- The Glob class always returns strings, even when
withFileTypes: trueis set in options - Pattern is always stored as an array internally
- Options are validated in the constructor (fail-fast)
- Fully compatible with glob v13's Glob class
- glob - Standalone async function
- globSync - Standalone sync function
- globStream - Standalone streaming function
- globIterate - Standalone iterator function
- Options - Full options reference