PRIX / lib /src /file-discoverer.js
Rachit-Tw's picture
Upload 94 files
440cddf verified
Raw
History Blame Contribute Delete
4.17 kB
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileDiscoverer = void 0;
exports.createFileDiscoverer = createFileDiscoverer;
exports.discoverFiles = discoverFiles;
const fs_1 = require("fs");
const path_1 = require("path");
const DEFAULT_EXCLUDES = new Set([
'node_modules',
'.git',
'.svn',
'.hg',
'dist',
'build',
'coverage',
'.nyc_output',
'.next',
'.nuxt',
'__pycache__',
'.pytest_cache',
'target',
'bin',
'obj',
'vendor',
'.venv',
'env'
]);
const PLATFORM_FILE_PROTOCOL = typeof window !== 'undefined';
class FileDiscoverer {
excludePatterns;
includeExtensions;
constructor(excludePatterns = [], includeExtensions = [
'.ts',
'.tsx',
'.js',
'.jsx',
'.go',
'.py',
'.rs',
'.cpp',
'.c',
'.h',
'.hpp',
'.java'
]) {
this.excludePatterns = new Set([...DEFAULT_EXCLUDES, ...excludePatterns]);
this.includeExtensions = new Set(includeExtensions.map(e => e.toLowerCase()));
}
discover(rootPath, filter) {
if (!(0, fs_1.existsSync)(rootPath)) {
console.warn(`Path does not exist: ${rootPath}`);
return [];
}
const results = [];
this.discoverRecursive(rootPath, rootPath, results, 0, filter?.maxDepth || 50);
return results;
}
discoverRecursive(rootPath, currentPath, results, depth, maxDepth) {
if (depth > maxDepth)
return;
let entries = [];
try {
entries = (0, fs_1.readdirSync)(currentPath);
}
catch (e) {
return;
}
for (const entry of entries) {
if (entry.startsWith('.') && entry !== '.gitignore')
continue;
if (this.excludePatterns.has(entry))
continue;
const fullPath = (0, path_1.join)(currentPath, entry);
let stat = null;
try {
stat = (0, fs_1.statSync)(fullPath);
}
catch {
continue;
}
if (stat.isDirectory()) {
this.discoverRecursive(rootPath, fullPath, results, depth + 1, maxDepth);
}
else if (stat.isFile()) {
const ext = (0, path_1.extname)(entry).toLowerCase();
if (this.includeExtensions.has(ext)) {
const relativePath = this.getRelativePath(rootPath, fullPath);
results.push({
path: fullPath,
relativePath,
extension: ext,
size: stat.size,
mtime: stat.mtimeMs
});
}
}
}
}
getRelativePath(root, full) {
const normalize = (p) => p.replace(/\\/g, '/');
const rootNorm = normalize(root);
const fullNorm = normalize(full);
if (fullNorm.startsWith(rootNorm)) {
const relative = fullNorm.substring(rootNorm.length);
return relative.startsWith('/') ? relative.substring(1) : relative;
}
return fullNorm;
}
filterByExtensions(files, extensions) {
const extSet = new Set(extensions.map(e => e.toLowerCase()));
return files.filter(f => extSet.has(f.extension));
}
groupByExtension(files) {
const groups = new Map();
for (const file of files) {
if (!groups.has(file.extension)) {
groups.set(file.extension, []);
}
groups.get(file.extension).push(file);
}
return groups;
}
}
exports.FileDiscoverer = FileDiscoverer;
function createFileDiscoverer(excludePatterns, includeExtensions) {
return new FileDiscoverer(excludePatterns, includeExtensions);
}
function discoverFiles(rootPath, options) {
const discoverer = new FileDiscoverer(options?.exclude, options?.include);
return discoverer.discover(rootPath, { maxDepth: options?.maxDepth });
}
//# sourceMappingURL=file-discoverer.js.map