@codewithmehmet/paul-cli
Version:
Intelligent project file scanner and Git change tracker with interactive interface
75 lines • 2.36 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { IGNORED_PATTERNS, BINARY_EXTENSIONS, SKIP_CONTENT_FILES, ALLOWED_HIDDEN_FILES } from '../core/patterns.js';
export function shouldReadContent(filePath) {
const filename = path.basename(filePath);
const ext = path.extname(filePath).toLowerCase();
return !SKIP_CONTENT_FILES.includes(filename) && !BINARY_EXTENSIONS.includes(ext);
}
export function getFileContent(filePath, maxSizeKB = 1000) {
try {
const stats = fs.statSync(filePath);
if (stats.size > maxSizeKB * 1024) {
return `[File too large: ${(stats.size / 1024).toFixed(1)}KB]`;
}
if (!shouldReadContent(filePath)) {
return '[Binary or ignored content]';
}
return fs.readFileSync(filePath, 'utf8');
} catch (error) {
return `[Read error: ${error.message}]`;
}
}
export function getFileExtension(filePath) {
const ext = path.extname(filePath).slice(1);
return ext || 'txt';
}
export function shouldIgnoreFile(fileName) {
const ext = path.extname(fileName).toLowerCase();
if (BINARY_EXTENSIONS.includes(ext)) return true;
for (const pattern of IGNORED_PATTERNS) {
if (pattern.startsWith('*')) {
if (fileName.endsWith(pattern.slice(1))) return true;
} else if (fileName === pattern) {
return true;
}
}
return false;
}
export function isAllowedHiddenFile(fileName) {
if (!fileName.startsWith('.')) return true;
return ALLOWED_HIDDEN_FILES.includes(fileName);
}
export async function getDirectoryStats(dirPath) {
let totalSize = 0;
let fileCount = 0;
try {
const items = await fs.promises.readdir(dirPath, {
withFileTypes: true
});
for (const item of items) {
if (shouldIgnoreFile(item.name)) continue;
if (!isAllowedHiddenFile(item.name)) continue;
const fullPath = path.join(dirPath, item.name);
try {
const stats = await fs.promises.stat(fullPath);
if (stats.isFile()) {
totalSize += stats.size;
fileCount++;
} else if (stats.isDirectory()) {
const subStats = await getDirectoryStats(fullPath);
totalSize += subStats.size;
fileCount += subStats.count;
}
} catch {
// Ignore permission errors
}
}
} catch {
// Ignore permission errors
}
return {
size: totalSize,
count: fileCount
};
}