UNPKG

@synstack/glob

Version:

Glob pattern matching and file filtering utilities

157 lines (154 loc) 5.21 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/glob.index.ts var glob_index_exports = {}; __export(glob_index_exports, { Glob: () => Glob, InvalidGlobException: () => InvalidGlobException, capture: () => capture, cwd: () => cwd, ensureDirTrailingSlash: () => ensureDirTrailingSlash, filterExcludedFactory: () => filterExcludedFactory, filterFactory: () => filterFactory, filterIncludedFactory: () => filterIncludedFactory, glob: () => glob_bundle_exports, matches: () => matches, sort: () => sort }); module.exports = __toCommonJS(glob_index_exports); // src/glob.bundle.ts var glob_bundle_exports = {}; __export(glob_bundle_exports, { capture: () => capture, cwd: () => cwd, ensureDirTrailingSlash: () => ensureDirTrailingSlash, filterExcludedFactory: () => filterExcludedFactory, filterFactory: () => filterFactory, filterIncludedFactory: () => filterIncludedFactory, matches: () => matches, sort: () => sort }); // src/glob.lib.ts var import_glob = require("glob"); var import_minimatch = require("minimatch"); function ensureDirTrailingSlash(glob) { return glob.endsWith("/") ? glob : `${glob}/`; } function capture(glob, filePath) { const baseRegex = import_minimatch.minimatch.makeRe(glob); if (!baseRegex) throw new InvalidGlobException(glob); const capturingRegexString = baseRegex.source.replaceAll("\\(", "(").replaceAll("\\)", ")").replaceAll("\\\\", "\\"); const regex = new RegExp(capturingRegexString, "g"); const matches2 = regex.exec(filePath); if (!matches2) return null; return matches2.slice(1); } function flatten(array) { return Array.isArray(array[0]) ? array[0] : array; } function matches(filePath, ...globs) { const { includes, excludes } = sort(...globs); return includes.some((globPattern) => (0, import_minimatch.minimatch)(filePath, globPattern)) && !excludes.some((glob) => (0, import_minimatch.minimatch)(filePath, glob, { dot: true })); } function sort(...patterns) { const _patterns = flatten(patterns); const includes = _patterns.filter((glob) => !glob.startsWith("!")); const excludes = _patterns.filter((glob) => glob.startsWith("!")).map((glob) => glob.replace("!", "")); return { includes, excludes }; } function filterIncludedFactory(...patterns) { return (path) => flatten(patterns).some((glob) => (0, import_minimatch.minimatch)(path, glob, { dot: true })); } function filterExcludedFactory(...patterns) { return (path) => flatten(patterns).every((glob) => !(0, import_minimatch.minimatch)(path, glob, { dot: true })); } function filterFactory(options) { const _options = options instanceof Array ? sort(...options) : options; const filterIncluded = filterIncludedFactory(_options.includes ?? []); const filterExcluded = filterExcludedFactory(_options.excludes ?? []); return (path) => filterIncluded(path) && filterExcluded(path); } var Glob = class _Glob { static cwd(cwd2) { return new _Glob(cwd2); } _cwd; _options; constructor(cwd2 = ".", options = { nodir: true }) { this._cwd = cwd2; this._options = options; } /** * Set advanced options for the glob search * @param options GlobOptions * @returns A new Glob instance with the updated options */ options(options) { return new _Glob(this._cwd, options); } /** * Executes a glob search and return the matching files */ find(...patterns) { const _patterns = flatten(patterns); const { includes, excludes } = sort(_patterns); return (0, import_glob.glob)(includes, { ignore: excludes, nodir: this._options.nodir ?? true, cwd: this._cwd, ...this._options }); } /** * Synchronously executes a glob search and return the matching files */ findSync(...patterns) { const _patterns = flatten(patterns); const { includes, excludes } = sort(_patterns); return (0, import_glob.globSync)(includes, { ignore: excludes, nodir: this._options.nodir ?? true, cwd: this._cwd, ...this._options }); } }; var InvalidGlobException = class extends Error { constructor(glob) { super(`Invalid glob: ${glob}`); } }; var cwd = Glob.cwd; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Glob, InvalidGlobException, capture, cwd, ensureDirTrailingSlash, filterExcludedFactory, filterFactory, filterIncludedFactory, glob, matches, sort }); //# sourceMappingURL=glob.index.cjs.map