tinyglobby

repository·main·Indexed 19 days ago

https://github.com/superchupudev/tinyglobby

A high-performance, minimal globbing library designed as a lightweight alternative to globby and fast-glob. It provides asynchronous glob() and synchronous globSync() functions to match files using glob patterns, supporting negation, custom FileSystemAdapters, and various configuration options via GlobOptions such as cwd, ignore, and absolute paths.

Tokens
1.7K
Snippets
9
Records
9
Agent score
69%

What's inside tinyglobby

  1. Install tinyglobby

    main

    tinyglobby is a fast and minimal alternative to globby and fast-glob. It is designed to behave the same way as those libraries but with significantly fewer dependencies (only two subdependencies compared to 23 for globby and 17 for fast-glob).

    npm install tinyglobby
  2. Use tinyglobby for globbing

    main

    You can use tinyglobby via its asynchronous glob function or its synchronous globSync function. The API is designed to be compatible with globby and fast-glob patterns, supporting arrays of patterns, negation (e.g., !**/*.d.ts), and option objects like cwd or ignore.

    import { glob, globSync } from 'tinyglobby';
    
    // Asynchronous usage
    await glob(['files/*.ts', '!**/*.d.ts'], { cwd: 'src' });
    
    // Synchronous usage
    globSync('src/**/*.ts', { ignore: '**/*.d.ts' });
  3. Configure globbing with GlobOptions

    main

    When using tinyglobby, you can pass a GlobOptions object to control the behavior of the file crawler.

    Key configuration options include:

    • absolute: Whether to return absolute paths. (Default: false)
    • braceExpansion: Enables support for brace expansion syntax like {a,b} or {1..9}. (Default: true)
    • caseSensitiveMatch: Whether to match in case-sensitive mode. (Default: true)
    • cwd: The working directory in which to search. Results are returned relative to this directory unless absolute is set. (Default: process.cwd())
    • debug: Logs debug information for development. (Default: false)
    • deep: Maximum directory depth to crawl. (Default: Infinity)
    • dot: Whether to return entries that start with a dot (e.g., .gitignore). (Default: false)
    • expandDirectories: Whether to automatically expand directory patterns. Note: Disable this if migrating from fast-glob. (Default: true)
    • extglob: Enables support for extglobs like +(pattern). (Default: true)
    • followSymbolicLinks: Whether to traverse and include symbolic links. (Default: true)
    • fs: An object that overrides node:fs functions using a FileSystemAdapter.
    • globstar: Enables support for matching nested directories with **. If false, ** behaves like *. (Default: true)
    • ignore: Glob patterns to exclude from the results. (Default: [])
    • onlyDirectories: If true, only directories are returned and onlyFiles is disabled. (Default: false)
    • onlyFiles: If true, only files are returned. (Default: true)
    • signal: An AbortSignal to abort the crawling process.
    const options: GlobOptions = {
      absolute: true,
      dot: true,
      ignore: ['**/node_modules/**'],
      cwd: './src'
    };
  4. Configure tsdown build settings

    main

    The tsdown.config.ts file is used to define the build configuration for the project using defineConfig from tsdown/config.

    Key configuration options include:

    • format: An array of output formats. Common values are 'esm' (ES Modules) and 'cjs' (CommonJS).
    • nodeProtocol: Defines how Node.js module resolution is handled. In this configuration, it is set to 'strip'.
    import { defineConfig, type UserConfig } from 'tsdown/config';
    
    export default defineConfig({
      format: ['esm', 'cjs'],
      nodeProtocol: 'strip'
    }) as UserConfig;
  5. Define GlobInput types

    main

    The GlobInput type defines what you can pass to the tinyglobby entry point. It accepts:

    • A single string pattern.
    • A readonly string[] of patterns.
    • A GlobOptions object for advanced configuration.
    type GlobInput = string | readonly string[] | GlobOptions;
  6. Provide a custom FileSystemAdapter

    main

    You can provide a custom FileSystemAdapter via the fs option in GlobOptions. This allows you to mock or redirect file system operations (like readdir, stat, etc.) during globbing. This is useful for testing or working with virtual file systems.

    The adapter must provide implementations for:

    • readdir / readdirSync
    • realpath / realpathSync
    • stat / statSync
    import { glob } from 'tinyglobby';
    
    const files = await glob('**/*.txt', {
      fs: {
        // Override specific methods
        readdir: myCustomReaddir,
        stat: myCustomStat
      }
    });
  7. Synchronously match files with globSync()

    main

    Use the globSync function to find files matching glob patterns synchronously. It returns an array of file paths immediately.

    Usage:

    • Pass a string or an array of strings as the first argument.
    • Pass an optional GlobOptions object as the second argument.

    Note: Passing an object containing patterns as the first argument is deprecated. Use the patterns argument instead.

    import { globSync } from 'tinyglobby';
    
    // Single pattern
    const files = globSync('src/**/*.ts');
    
    // Multiple patterns
    const files = globSync(['src/**/*.ts', 'test/**/*.ts'], { onlyFiles: true });
  8. Use FileSystemAdapter to mock or override the file system

    main

    The FileSystemAdapter type allows you to provide a partial implementation of FSLike (from the fdir package). This is useful for overriding node:fs functions, such as when performing tests with a mocked file system.

    import type { FSLike } from 'fdir';
    
    export type FileSystemAdapter = Partial<FSLike>;
  9. Asynchronously match files with glob()

    main

    Use the glob function to asynchronously find files that match one or more glob patterns. It returns a Promise that resolves to an array of file paths.

    Usage:

    • Pass a string or an array of strings as the first argument.
    • Pass an optional GlobOptions object as the second argument.

    Note: Passing an object containing patterns as the first argument is deprecated. Use the patterns argument instead.

    import { glob } from 'tinyglobby';
    
    // Single pattern
    const files = await glob('src/**/*.ts');
    
    // Multiple patterns
    const files = await glob(['src/**/*.ts', 'test/**/*.ts'], { onlyFiles: true });