@paulohenriquevn/m2js
Version:
Transform TypeScript/JavaScript code into LLM-friendly Markdown summaries + Smart Dead Code Detection + Graph-Deep Diff Analysis. Extract exported functions, classes, and JSDoc comments for better AI context with 60%+ token reduction. Intelligent dead cod
116 lines • 3.68 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.scanDirectory = scanDirectory;
exports.isDirectory = isDirectory;
exports.isFile = isFile;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const SUPPORTED_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx'];
const IGNORED_DIRECTORIES = [
'node_modules',
'dist',
'build',
'.next',
'coverage',
'.git',
'.svn',
'.hg',
'vendor',
'target',
'bin',
'obj',
];
const IGNORED_FILE_PATTERNS = [
'.d.ts', // TypeScript declaration files
'.test.', // Test files
'.spec.', // Spec files
'.min.', // Minified files
'.bundle.', // Bundle files
];
async function scanDirectory(directoryPath) {
const absolutePath = path_1.default.resolve(directoryPath);
const result = {
files: [],
totalFound: 0,
errors: [],
};
try {
// Verify directory exists and is accessible
const stats = await fs_1.promises.stat(absolutePath);
if (!stats.isDirectory()) {
throw new Error(`Path is not a directory: ${absolutePath}`);
}
}
catch (error) {
result.errors.push(`Cannot access directory: ${error.message}`);
return result;
}
try {
const files = await scanDirectoryRecursive(absolutePath);
result.files = files;
result.totalFound = files.length;
}
catch (error) {
result.errors.push(`Error scanning directory: ${error.message}`);
}
return result;
}
async function scanDirectoryRecursive(dirPath) {
const foundFiles = [];
try {
const entries = await fs_1.promises.readdir(dirPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path_1.default.join(dirPath, entry.name);
if (entry.isDirectory()) {
// Skip ignored directories
if (!shouldIgnoreDirectory(entry.name)) {
const subFiles = await scanDirectoryRecursive(fullPath);
foundFiles.push(...subFiles);
}
}
else if (entry.isFile()) {
// Check if file has supported extension and is not ignored
if (isSupportedFile(entry.name) && !shouldIgnoreFile(entry.name)) {
foundFiles.push(fullPath);
}
}
}
}
catch (error) {
// Log error but continue scanning other directories
console.warn(`Warning: Could not scan directory ${dirPath}: ${error.message}`);
}
return foundFiles;
}
function isSupportedFile(fileName) {
const extension = path_1.default.extname(fileName).toLowerCase();
return SUPPORTED_EXTENSIONS.includes(extension);
}
function shouldIgnoreDirectory(dirName) {
return IGNORED_DIRECTORIES.includes(dirName) || dirName.startsWith('.');
}
function shouldIgnoreFile(fileName) {
// Check against ignored patterns
for (const pattern of IGNORED_FILE_PATTERNS) {
if (fileName.includes(pattern)) {
return true;
}
}
return false;
}
function isDirectory(targetPath) {
return fs_1.promises
.stat(path_1.default.resolve(targetPath))
.then(stats => stats.isDirectory())
.catch(() => false);
}
function isFile(targetPath) {
return fs_1.promises
.stat(path_1.default.resolve(targetPath))
.then(stats => stats.isFile())
.catch(() => false);
}
//# sourceMappingURL=file-scanner.js.map