@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
68 lines (66 loc) • 2.89 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
let node_path = require("node:path");
let fast_glob = require("fast-glob");
fast_glob = require_runtime.__toESM(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) => (0, node_path.resolve)(dir))));
uniqueDirs.sort((a, b) => a.length - b.length);
return uniqueDirs.reduce((acc, dir) => {
if (!acc.some((parent) => {
const rel = (0, node_path.relative)(parent, dir);
return !rel.startsWith("..") && !(0, node_path.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) => (0, node_path.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) => (0, node_path.normalize)(pattern));
const fileList = getDistinctRootDirs([config.system.baseDir, ...config.content.codeDir]).flatMap((root) => fast_glob.default.sync(patterns, {
cwd: root,
ignore: excludePatterns,
absolute: true,
dot: true
}));
return Array.from(new Set(fileList));
};
//#endregion
exports.buildComponentFilesList = buildComponentFilesList;
//# sourceMappingURL=buildComponentFilesList.cjs.map