UNPKG

@jirutka/ajv-cli

Version:

CLI for Ajv JSON Schema Validator with human-friendly error messages

41 lines 1.45 kB
import * as FS from 'node:fs'; import { join as joinPath } from 'node:path'; import picomatch from 'picomatch'; export function glob(pattern, options = {}) { const isFullMatch = picomatch(pattern, options); const state = picomatch.scan(pattern, { parts: true, tokens: true }); if (!state.isGlob) { return null; } const globParts = state.parts; const maxDepth = state.maxDepth; const filePaths = []; const traverse = (dirpath, depth) => { let isPrefixMatch = (input) => { const dirGlob = joinPath(...globParts.slice(0, depth)); // Replace itself - this is basically a memorization of the function result. isPrefixMatch = picomatch(dirGlob, options); return isPrefixMatch(input); }; for (const dirent of FS.readdirSync(dirpath || '.', { encoding: 'utf-8', withFileTypes: true, })) { const path = joinPath(dirpath, dirent.name); if (dirent.isDirectory()) { if (!isFinite(maxDepth) || (depth < maxDepth && isPrefixMatch(path))) { traverse(path, depth + 1); } } else if (isFullMatch(path)) { filePaths.push(path); } } }; traverse('', 1); return filePaths; } export function isGlob(input) { return picomatch.scan(input).isGlob; } //# sourceMappingURL=glob.js.map