agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
58 lines (49 loc) • 1.55 kB
JavaScript
/**
* @file File scanner
* @description Recursively scans directories for files with specified extensions
*/
const fs = require('fs');
const path = require('path');
/**
* Get all files recursively with specified extensions
* @param {string} dirPath - Directory path to scan
* @param {Array} extensions - File extensions to include
* @param {Array} excludePatterns - Patterns to exclude
* @returns {Array} Array of file paths
*/
async function getAllFiles(dirPath, extensions, excludePatterns) {
const files = [];
try {
await fs.promises.access(dirPath);
} catch (error) {
console.warn(`Warning: Directory ${dirPath} does not exist`);
return files;
}
async function traverse(currentPath) {
try {
const entries = await fs.promises.readdir(currentPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(currentPath, entry.name);
// Skip excluded patterns
if (excludePatterns.some(pattern => fullPath.includes(pattern))) {
continue;
}
if (entry.isDirectory()) {
await traverse(fullPath);
} else if (entry.isFile()) {
const ext = path.extname(entry.name);
if (extensions.includes(ext)) {
files.push(fullPath);
}
}
}
} catch (error) {
console.warn(`Warning: Could not read directory ${currentPath}: ${error.message}`);
}
}
await traverse(dirPath);
return files;
}
module.exports = {
getAllFiles
};