UNPKG

@intlayer/chokidar

Version:

Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.

65 lines (63 loc) 2.6 kB
import { isAbsolute, normalize, relative, resolve } from "node:path"; import fg from "fast-glob"; //#region src/utils/buildComponentFilesList.ts /** * Normalizes a pattern value to an array */ const normalizeToArray = (value) => Array.isArray(value) ? value : [value]; /** * Remove directories that are subdirectories of others in the list so files * are never scanned twice. * Example: ['/root', '/root/src'] → ['/root'] */ const getDistinctRootDirs = (dirs) => { const uniqueDirs = Array.from(new Set(dirs.map((dir) => resolve(dir)))); uniqueDirs.sort((a, b) => a.length - b.length); return uniqueDirs.reduce((acc, dir) => { if (!acc.some((parent) => { const rel = relative(parent, dir); return !rel.startsWith("..") && !isAbsolute(rel) && rel !== ""; })) acc.push(dir); return acc; }, []); }; /** * Builds a deduplicated list of absolute file paths matching the given patterns. * * Handles multiple root directories (deduplicates overlapping roots), exclude * patterns, negation patterns embedded in `transformPattern`, and optional * dot-file inclusion. * * @example * // Single root with excludes * const files = buildComponentFilesList({ * transformPattern: 'src/**\/*.{ts,tsx}', * excludePattern: ['**\/node_modules\/**'], * baseDir: '/path/to/project', * }); * * @example * // Multiple roots (e.g. baseDir + codeDir), dot files included * const files = buildComponentFilesList(config, ['**\/node_modules\/**']); */ const buildComponentFilesList = (config, excludePattern) => { const transformPattern = config.build.traversePattern; const compilerTransformPattern = config.compiler.transformPattern; const contentDeclarationPattern = config.content.fileExtensions.map((ext) => `/**/*${ext}`); const patterns = [...transformPattern, ...normalizeToArray(compilerTransformPattern)].filter((pattern) => typeof pattern === "string").filter((pattern) => !pattern.startsWith("!")).map((pattern) => normalize(pattern)); const excludePatterns = [ ...excludePattern ?? [], ...contentDeclarationPattern, ...transformPattern.filter((pattern) => typeof pattern === "string" && pattern.startsWith("!")).map((pattern) => pattern.slice(1)) ].filter((pattern) => typeof pattern === "string").map((pattern) => normalize(pattern)); const fileList = getDistinctRootDirs([config.system.baseDir, ...config.content.codeDir]).flatMap((root) => fg.sync(patterns, { cwd: root, ignore: excludePatterns, absolute: true, dot: true })); return Array.from(new Set(fileList)); }; //#endregion export { buildComponentFilesList }; //# sourceMappingURL=buildComponentFilesList.mjs.map