tulz
Version:
A lightweight CLI tool for codebase analysis and aggregation - perfect for slow machines and text editor workflows
61 lines (54 loc) • 2.17 kB
JavaScript
const fs = require('fs');
const path = require('path');
const { shouldIgnore } = require('../utils/fsUtils');
const { formatReportHeader } = require('../utils/formatters'); // <-- IMPORTA O FORMATADOR
function buildTreeObject(dirPath) {
const tree = { name: path.basename(dirPath), type: 'directory', children: [], fileCount: 0 };
let totalFiles = 0;
try {
const items = fs.readdirSync(dirPath).sort();
for (const item of items) {
const fullPath = path.join(dirPath, item);
if (shouldIgnore(fullPath)) continue;
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
const subTree = buildTreeObject(fullPath);
tree.children.push(subTree);
totalFiles += subTree.fileCount;
} else if (stat.isFile()) {
const lineCount = fs.readFileSync(fullPath, 'utf-8').split('\n').length;
tree.children.push({ name: item, type: 'file', lineCount });
totalFiles++;
}
}
} catch (error) { console.error(`Erro ao ler ${dirPath}: ${error.message}`); }
tree.fileCount = totalFiles;
return tree;
}
function formatTreeToString(node, prefix = '') {
let output = '';
const children = node.children || [];
children.forEach((child, index) => {
const isLast = index === children.length - 1;
const connector = isLast ? '└─' : '├─';
const newPrefix = prefix + (isLast ? ' ' : '│ ');
if (child.type === 'directory') {
output += `${prefix}${connector} 📁 ${child.name} (${child.fileCount} arquivos)\n`;
output += formatTreeToString(child, newPrefix);
} else {
output += `${prefix}${connector} 📄 ${child.name} (${child.lineCount} linhas)\n`;
}
});
return output;
}
function generateStructureReport(rootDir) {
const absolutePath = path.resolve(rootDir);
const treeObject = buildTreeObject(absolutePath);
// USA O FORMATADOR!
const reportHeader = formatReportHeader(treeObject.name, [
{ label: 'TOTAL DE ARQUIVOS', value: treeObject.fileCount }
]);
const structureText = formatTreeToString(treeObject);
return reportHeader + '\n' + structureText;
}
module.exports = { generateStructureReport };