intellify
Version:
Detect JavaScript & TypeScript errors and make codes more optimized.
69 lines (51 loc) • 1.48 kB
JavaScript
import fs from 'fs';
import path from 'path';
import util from 'util';
export function formatBytes(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
export function getLineFromLoc(loc) {
if (!loc) return 'unknown';
return loc.start.line;
}
export function safeInspect(obj, depth = 2) {
return util.inspect(obj, {
depth: depth,
colors: false,
maxArrayLength: 10,
breakLength: 80
});
}
export function debugLog(message, data = null) {
}
export function getJsFiles(dir, ignore = ['node_modules', '.git']) {
let results = [];
try {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory() && !ignore.includes(file)) {
results = results.concat(getJsFiles(filePath, ignore));
} else if (stat.isFile() &&
(file.endsWith('.js') || file.endsWith('.ts') ||
file.endsWith('.jsx') || file.endsWith('.tsx'))) {
results.push(filePath);
}
}
} catch (err) {
console.error(`Error reading directory ${dir}:`, err.message);
}
return results;
}
export default {
formatBytes,
getLineFromLoc,
safeInspect,
debugLog,
getJsFiles
};