@synstack/glob
Version: 
Glob pattern matching and file filtering utilities
94 lines (90 loc) • 4.19 kB
text/typescript
import { GlobOptions } from 'glob';
interface Options {
    includes: string[];
    excludes?: string[];
}
/**
 * Ensures a glob pattern has a trailing slash to match directories only
 * @param glob - The glob pattern to ensure has a trailing slash
 * @returns The glob pattern with a trailing slash
 */
declare function ensureDirTrailingSlash(glob: string): string;
/**
 * Allows extracting values from a glob pattern
 * @example **\/path/to/(*)/(*).ts => [string, string]
 * @returns string[] or null if glob does not match
 *
 * _Note: glob capturing only works with single "*" widlcards_
 */
declare function capture(glob: string, filePath: string): string[] | null;
/**
 * @param filePath
 * @param globs list of globs to match against globs prefixed with ! are excluded
 * @returns boolean
 */
declare function matches(filePath: string, ...globs: Array<string> | [Array<string>]): boolean;
/**
 * Split included and excluded globs, removing the "!" prefix along the way
 * @param patterns a list of glob patterns, excluded globs start with "!"
 */
declare function sort(...patterns: Array<string> | [Array<string>]): {
    includes: string[];
    excludes: string[];
};
/**
 * Create a filter function resolving to true if path matches any of the globs
 * @param patterns A list of glob patterns
 */
declare function filterIncludedFactory(...patterns: Array<string> | [Array<string>]): (path: string) => boolean;
/**
 * Create a filter function resolving to true as long as path doesn't match any of the globs
 * @param patterns A list of glob patterns
 */
declare function filterExcludedFactory(...patterns: Array<string> | [Array<string>]): (path: string) => boolean;
/**
 * Creates a filter function based on glob patterns or GlobOptions
 * @param options Array of glob patterns with excluded patterns prefixed with "!" or an object of sorted glob patterns
 * @returns A function that takes a path and returns true if it matches the glob patterns and none of the excluded patterns
 */
declare function filterFactory(globs: Array<string>): (path: string) => boolean;
declare function filterFactory(options: Options): (path: string) => boolean;
declare class Glob {
    static cwd(this: void, cwd: string): Glob;
    private readonly _cwd;
    private readonly _options;
    protected constructor(cwd?: string, options?: GlobOptions);
    /**
     * Set advanced options for the glob search
     * @param options GlobOptions
     * @returns A new Glob instance with the updated options
     */
    options(options: GlobOptions): Glob;
    /**
     * Executes a glob search and return the matching files
     */
    find(...patterns: Array<string> | [Array<string>]): Promise<string[]>;
    /**
     * Synchronously executes a glob search and return the matching files
     */
    findSync(...patterns: Array<string> | [Array<string>]): string[];
}
declare class InvalidGlobException extends Error {
    constructor(glob: string);
}
/**
 * Creates a Glob instance with the provided working directory
 * @param cwd Path to the working directory, defaults to the current working directory
 */
declare const cwd: typeof Glob.cwd;
declare const glob_bundle_capture: typeof capture;
declare const glob_bundle_cwd: typeof cwd;
declare const glob_bundle_ensureDirTrailingSlash: typeof ensureDirTrailingSlash;
declare const glob_bundle_filterExcludedFactory: typeof filterExcludedFactory;
declare const glob_bundle_filterFactory: typeof filterFactory;
declare const glob_bundle_filterIncludedFactory: typeof filterIncludedFactory;
declare const glob_bundle_matches: typeof matches;
declare const glob_bundle_sort: typeof sort;
declare namespace glob_bundle {
  export { glob_bundle_capture as capture, glob_bundle_cwd as cwd, glob_bundle_ensureDirTrailingSlash as ensureDirTrailingSlash, glob_bundle_filterExcludedFactory as filterExcludedFactory, glob_bundle_filterFactory as filterFactory, glob_bundle_filterIncludedFactory as filterIncludedFactory, glob_bundle_matches as matches, glob_bundle_sort as sort };
}
export { Glob, InvalidGlobException, capture, cwd, ensureDirTrailingSlash, filterExcludedFactory, filterFactory, filterIncludedFactory, glob_bundle as glob, matches, sort };