UNPKG

pury

Version:

🛡️ AI-powered security scanner with advanced threat detection, dual reporting system (detailed & summary), and comprehensive code analysis

187 lines 4.06 kB
import { promises as fs } from 'fs'; import { extname, relative, resolve } from 'path'; export async function fileExists(path) { try { await fs.access(path); return true; } catch { return false; } } export async function isDirectory(path) { try { const stats = await fs.stat(path); return stats.isDirectory(); } catch { return false; } } export async function getFileSize(path) { try { const stats = await fs.stat(path); return stats.size; } catch { return 0; } } export async function readFileInfo(filePath, basePath) { const content = await fs.readFile(filePath, 'utf8'); const stats = await fs.stat(filePath); return { path: resolve(filePath), relativePath: relative(basePath, filePath), size: stats.size, extension: extname(filePath), content, encoding: 'utf8' }; } export function getFileExtension(filePath) { return extname(filePath).toLowerCase(); } export function isTextFile(filePath) { const textExtensions = [ '.js', '.ts', '.jsx', '.tsx', '.py', '.java', '.c', '.cpp', '.h', '.hpp', '.cs', '.php', '.rb', '.go', '.rs', '.swift', '.kt', '.scala', '.m', '.mm', '.html', '.htm', '.css', '.scss', '.sass', '.less', '.xml', '.svg', '.json', '.yaml', '.yml', '.toml', '.ini', '.cfg', '.conf', '.md', '.txt', '.rst', '.adoc', '.tex', '.sql', '.sh', '.bash', '.zsh', '.fish', '.ps1', '.bat', '.cmd', '.dockerfile', '.makefile', '.gitignore', '.gitattributes', '.env', '.editorconfig', '.prettierrc', '.eslintrc' ]; const ext = getFileExtension(filePath); return textExtensions.includes(ext) || !ext; // Include files without extension } export function isBinaryFile(filePath) { const binaryExtensions = [ '.exe', '.dll', '.so', '.dylib', '.bin', '.obj', '.o', '.a', '.lib', '.zip', '.tar', '.gz', '.bz2', '.xz', '.7z', '.rar', '.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.ico', '.svg', '.mp3', '.mp4', '.avi', '.mov', '.wmv', '.flv', '.webm', '.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx' ]; const ext = getFileExtension(filePath); return binaryExtensions.includes(ext); } export async function shouldSkipFile(filePath, maxSize) { if (isBinaryFile(filePath)) { return true; } // Skip very large files by default try { const { statSync } = await import('fs'); const stats = statSync(filePath); return stats.size > maxSize; } catch { return true; } } export async function ensureDirectory(dirPath) { try { await fs.mkdir(dirPath, { recursive: true }); } catch (error) { if (error.code !== 'EEXIST') { throw error; } } } export function normalizeLineEndings(content) { return content.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); } export function countLines(content) { return content.split('\n').length; } export function extractLineContext(content, lineNumber, contextLines = 2) { const lines = content.split('\n'); const start = Math.max(0, lineNumber - contextLines - 1); const end = Math.min(lines.length, lineNumber + contextLines); return lines.slice(start, end).join('\n'); } //# sourceMappingURL=file-utils.js.map