@synstack/glob
Version:
Glob pattern matching and file filtering utilities
129 lines (127 loc) • 4.05 kB
JavaScript
var __defProp = Object.defineProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
// 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
import { glob as globAsync, globSync } from "glob";
import { minimatch } from "minimatch";
import { normalizeSeparators } from "@synstack/path";
function ensureDirTrailingSlash(glob) {
return glob.endsWith("/") ? glob : `${glob}/`;
}
function capture(glob, filePath) {
const normalizedPath = normalizeSeparators(filePath);
const baseRegex = 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(normalizedPath);
if (!matches2) return null;
return matches2.slice(1);
}
function flatten(array) {
return Array.isArray(array[0]) ? array[0] : array;
}
function matches(filePath, ...globs) {
const normalizedPath = normalizeSeparators(filePath);
const { includes, excludes } = sort(...globs);
return includes.some((globPattern) => minimatch(normalizedPath, globPattern)) && !excludes.some((glob) => minimatch(normalizedPath, 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) => minimatch(path, glob, { dot: true }));
}
function filterExcludedFactory(...patterns) {
return (path) => flatten(patterns).every((glob) => !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 globAsync(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 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;
export {
Glob,
InvalidGlobException,
capture,
cwd,
ensureDirTrailingSlash,
filterExcludedFactory,
filterFactory,
filterIncludedFactory,
glob_bundle_exports as glob,
matches,
sort
};
//# sourceMappingURL=glob.index.js.map