UNPKG

@synstack/glob

Version:

Glob pattern matching and file filtering utilities

80 lines (76 loc) 3.52 kB
import { RelativePath } from '@synstack/path'; interface GlobOptions { includes: string[]; excludes?: 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: GlobOptions): (path: string) => boolean; declare class Glob { static cwd(this: void, cwd: string): Glob; private readonly cwd; private constructor(); /** * Executes a glob search and return the matching files */ find(...patterns: Array<string> | [Array<string>]): Promise<RelativePath[]>; /** * Synchronously executes a glob search and return the matching files */ findSync(...patterns: Array<string> | [Array<string>]): RelativePath[]; } 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_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_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, filterExcludedFactory, filterFactory, filterIncludedFactory, glob_bundle as glob, matches, sort };