UNPKG

@synstack/glob

Version:

Glob pattern matching and file filtering utilities

1 lines 8.55 kB
{"version":3,"sources":["../src/glob.index.ts","../src/glob.bundle.ts","../src/glob.lib.ts"],"sourcesContent":["export * as glob from \"./glob.bundle.ts\";\nexport * from \"./glob.lib.ts\";\n","export {\n capture,\n cwd,\n ensureDirTrailingSlash,\n filterExcludedFactory,\n filterFactory,\n filterIncludedFactory,\n matches,\n sort,\n} from \"./glob.lib.ts\";\n","import { glob as globAsync, globSync, type GlobOptions } from \"glob\";\nimport { minimatch } from \"minimatch\";\n\ninterface Options {\n includes: string[];\n excludes?: string[];\n}\n\n/**\n * Ensures a glob pattern has a trailing slash to match directories only\n * @param glob - The glob pattern to ensure has a trailing slash\n * @returns The glob pattern with a trailing slash\n */\nexport function ensureDirTrailingSlash(glob: string) {\n return glob.endsWith(\"/\") ? glob : `${glob}/`;\n}\n\n/**\n * Allows extracting values from a glob pattern\n * @example **\\/path/to/(*)/(*).ts => [string, string]\n * @returns string[] or null if glob does not match\n *\n * _Note: glob capturing only works with single \"*\" widlcards_\n */\nexport function capture(glob: string, filePath: string) {\n const baseRegex = minimatch.makeRe(glob);\n if (!baseRegex) throw new InvalidGlobException(glob);\n const capturingRegexString = baseRegex.source\n .replaceAll(\"\\\\(\", \"(\")\n .replaceAll(\"\\\\)\", \")\")\n .replaceAll(\"\\\\\\\\\", \"\\\\\");\n const regex = new RegExp(capturingRegexString, \"g\");\n const matches = regex.exec(filePath);\n if (!matches) return null;\n return matches.slice(1);\n}\n\nfunction flatten(array: Array<string> | [Array<string>]): Array<string> {\n return Array.isArray(array[0]) ? array[0] : (array as Array<string>);\n}\n\n/**\n * @param filePath\n * @param globs list of globs to match against globs prefixed with ! are excluded\n * @returns boolean\n */\nexport function matches(\n filePath: string,\n ...globs: Array<string> | [Array<string>]\n): boolean {\n const { includes, excludes } = sort(...globs);\n return (\n includes.some((globPattern) => minimatch(filePath, globPattern)) &&\n !excludes.some((glob) => minimatch(filePath, glob, { dot: true }))\n );\n}\n\n/**\n * Split included and excluded globs, removing the \"!\" prefix along the way\n * @param patterns a list of glob patterns, excluded globs start with \"!\"\n */\nexport function sort(...patterns: Array<string> | [Array<string>]) {\n const _patterns = flatten(patterns);\n const includes = _patterns.filter((glob) => !glob.startsWith(\"!\"));\n const excludes = _patterns\n .filter((glob) => glob.startsWith(\"!\"))\n .map((glob) => glob.replace(\"!\", \"\"));\n return { includes, excludes };\n}\n\n/**\n * Create a filter function resolving to true if path matches any of the globs\n * @param patterns A list of glob patterns\n */\nexport function filterIncludedFactory(\n ...patterns: Array<string> | [Array<string>]\n) {\n return (path: string) =>\n flatten(patterns).some((glob) => minimatch(path, glob, { dot: true }));\n}\n\n/**\n * Create a filter function resolving to true as long as path doesn't match any of the globs\n * @param patterns A list of glob patterns\n */\nexport function filterExcludedFactory(\n ...patterns: Array<string> | [Array<string>]\n) {\n return (path: string) =>\n flatten(patterns).every((glob) => !minimatch(path, glob, { dot: true }));\n}\n\n/**\n * Creates a filter function based on glob patterns or GlobOptions\n * @param options Array of glob patterns with excluded patterns prefixed with \"!\" or an object of sorted glob patterns\n * @returns A function that takes a path and returns true if it matches the glob patterns and none of the excluded patterns\n */\nexport function filterFactory(globs: Array<string>): (path: string) => boolean;\nexport function filterFactory(options: Options): (path: string) => boolean;\nexport function filterFactory(options: Options | Array<string>) {\n const _options = options instanceof Array ? sort(...options) : options;\n const filterIncluded = filterIncludedFactory(_options.includes ?? []);\n const filterExcluded = filterExcludedFactory(_options.excludes ?? []);\n return (path: string) => filterIncluded(path) && filterExcluded(path);\n}\n\nexport class Glob {\n public static cwd(this: void, cwd: string) {\n return new Glob(cwd);\n }\n\n private readonly _cwd: string;\n private readonly _options: GlobOptions;\n\n private constructor(\n cwd: string = \".\",\n options: GlobOptions = {\n nodir: true,\n },\n ) {\n this._cwd = cwd;\n this._options = options;\n }\n\n /**\n * Set advanced options for the glob search\n * @param options GlobOptions\n * @returns A new Glob instance with the updated options\n */\n public options(options: GlobOptions) {\n return new Glob(this._cwd, options);\n }\n\n /**\n * Executes a glob search and return the matching files\n */\n public find(...patterns: Array<string> | [Array<string>]) {\n const _patterns = flatten(patterns);\n const { includes, excludes } = sort(_patterns);\n return globAsync(includes, {\n ignore: excludes,\n nodir: this._options.nodir ?? true,\n cwd: this._cwd,\n ...this._options,\n }) as Promise<string[]>;\n }\n\n /**\n * Synchronously executes a glob search and return the matching files\n */\n public findSync(...patterns: Array<string> | [Array<string>]) {\n const _patterns = flatten(patterns);\n const { includes, excludes } = sort(_patterns);\n return globSync(includes, {\n ignore: excludes,\n nodir: this._options.nodir ?? true,\n cwd: this._cwd,\n ...this._options,\n }) as string[];\n }\n}\n\nexport class InvalidGlobException extends Error {\n constructor(glob: string) {\n super(`Invalid glob: ${glob}`);\n }\n}\n\n/**\n * Creates a Glob instance with the provided working directory\n * @param cwd Path to the working directory, defaults to the current working directory\n */\nexport const cwd = Glob.cwd;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAA8D;AAC9D,uBAA0B;AAYnB,SAAS,uBAAuB,MAAc;AACnD,SAAO,KAAK,SAAS,GAAG,IAAI,OAAO,GAAG,IAAI;AAC5C;AASO,SAAS,QAAQ,MAAc,UAAkB;AACtD,QAAM,YAAY,2BAAU,OAAO,IAAI;AACvC,MAAI,CAAC,UAAW,OAAM,IAAI,qBAAqB,IAAI;AACnD,QAAM,uBAAuB,UAAU,OACpC,WAAW,OAAO,GAAG,EACrB,WAAW,OAAO,GAAG,EACrB,WAAW,QAAQ,IAAI;AAC1B,QAAM,QAAQ,IAAI,OAAO,sBAAsB,GAAG;AAClD,QAAMA,WAAU,MAAM,KAAK,QAAQ;AACnC,MAAI,CAACA,SAAS,QAAO;AACrB,SAAOA,SAAQ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