Glob

Bun

Symbol

Glob

class Glob

Match files using glob patterns.

The supported pattern syntax for is:

  • ? Matches any single character.
  • * Matches zero or more characters, except for path separators ('/' or '').
  • ** Matches zero or more characters, including path separators. Must match a complete path segment, i.e. followed by a path separator or at the end of the pattern.
  • [ab] Matches one of the characters contained in the brackets. Character ranges (e.g. "[a-z]") are also supported. Use "[!ab]" or "[^ab]" to match any character except those contained in the brackets.
  • {a,b} Match one of the patterns contained in the braces. Any of the wildcards listed above can be used in the sub patterns. Braces may be nested up to 10 levels deep.
  • ! Negates the result when at the start of the pattern. Multiple "!" characters negate the pattern multiple times.
  • `` Used to escape any of the special characters above.
const glob = new Glob("*.{ts,tsx}");
const scannedFiles = await Array.fromAsync(glob.scan({ cwd: './src' }))
  • match(str: string): boolean

    Match the glob against a string

    const glob = new Glob("*.{ts,tsx}");
    expect(glob.match('foo.ts')).toBeTrue();
    
  • scan(optionsOrCwd?: string | GlobScanOptions): AsyncIterableIterator<string>

    Scan a root directory recursively for files that match this glob pattern. Returns an async iterator.

    const glob = new Glob("*.{ts,tsx}");
    const scannedFiles = await Array.fromAsync(glob.scan({ cwd: './src' }))
    
  • scanSync(optionsOrCwd?: string | GlobScanOptions): IterableIterator<string>

    Synchronously scan a root directory recursively for files that match this glob pattern. Returns an iterator.

    const glob = new Glob("*.{ts,tsx}");
    const scannedFiles = Array.from(glob.scan({ cwd: './src' }))