UNPKG

code-transmute

Version:

Convert any codebase into any language — without changing its brain.

228 lines 7.89 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileUtils = void 0; const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const glob_1 = require("glob"); class FileUtils { static async findFiles(directory, patterns, excludePatterns = []) { const files = []; for (const pattern of patterns) { const matches = await (0, glob_1.glob)(pattern, { cwd: directory, ignore: excludePatterns, absolute: false }); files.push(...matches); } return [...new Set(files)]; // Remove duplicates } static async readFileSafely(filePath) { try { return await fs_extra_1.default.readFile(filePath, 'utf-8'); } catch (error) { console.warn(`Could not read file: ${filePath}`); return null; } } static async writeFileSafely(filePath, content) { try { await fs_extra_1.default.ensureDir(path_1.default.dirname(filePath)); await fs_extra_1.default.writeFile(filePath, content); return true; } catch (error) { console.error(`Could not write file: ${filePath}`, error); return false; } } static async copyDirectory(source, destination) { await fs_extra_1.default.ensureDir(destination); await fs_extra_1.default.copy(source, destination, { overwrite: true, filter: (src, _dest) => { // Skip common ignore patterns const relativePath = path_1.default.relative(source, src); return !relativePath.includes('node_modules') && !relativePath.includes('.git') && !relativePath.includes('dist') && !relativePath.includes('build'); } }); } static async getFileStats(filePath) { try { return await fs_extra_1.default.stat(filePath); } catch (error) { return null; } } static async isDirectory(filePath) { const stats = await this.getFileStats(filePath); return stats ? stats.isDirectory() : false; } static async isFile(filePath) { const stats = await this.getFileStats(filePath); return stats ? stats.isFile() : false; } static async ensureDirectoryExists(dirPath) { await fs_extra_1.default.ensureDir(dirPath); } static async removeDirectory(dirPath) { await fs_extra_1.default.remove(dirPath); } static async cleanDirectory(dirPath) { if (await this.isDirectory(dirPath)) { await fs_extra_1.default.emptyDir(dirPath); } } static getFileExtension(filePath) { return path_1.default.extname(filePath); } static getFileName(filePath) { return path_1.default.basename(filePath); } static getDirectoryName(filePath) { return path_1.default.dirname(filePath); } static joinPaths(...paths) { return path_1.default.join(...paths); } static resolvePath(filePath) { return path_1.default.resolve(filePath); } static async findPackageJson(directory) { const packageJsonPath = path_1.default.join(directory, 'package.json'); if (await this.isFile(packageJsonPath)) { try { return await fs_extra_1.default.readJson(packageJsonPath); } catch (error) { return null; } } return null; } static async findRequirementsTxt(directory) { const requirementsPath = path_1.default.join(directory, 'requirements.txt'); if (await this.isFile(requirementsPath)) { try { const content = await fs_extra_1.default.readFile(requirementsPath, 'utf-8'); return content.split('\n') .map(line => line.trim()) .filter(line => line && !line.startsWith('#')); } catch (error) { return null; } } return null; } static async findGoMod(directory) { const goModPath = path_1.default.join(directory, 'go.mod'); if (await this.isFile(goModPath)) { try { const content = await fs_extra_1.default.readFile(goModPath, 'utf-8'); return this.parseGoMod(content); } catch (error) { return null; } } return null; } static async findCargoToml(directory) { const cargoPath = path_1.default.join(directory, 'Cargo.toml'); if (await this.isFile(cargoPath)) { try { const content = await fs_extra_1.default.readFile(cargoPath, 'utf-8'); return this.parseCargoToml(content); } catch (error) { return null; } } return null; } static parseGoMod(content) { const lines = content.split('\n'); const result = {}; let inRequire = false; lines.forEach(line => { line = line.trim(); if (line === 'require (') { inRequire = true; result.dependencies = []; } else if (inRequire && line === ')') { inRequire = false; } else if (inRequire && line) { const parts = line.split(/\s+/); if (parts.length >= 2) { result.dependencies.push({ module: parts[0], version: parts[1] }); } } else if (line.startsWith('module ')) { result.module = line.replace('module ', ''); } }); return result; } static parseCargoToml(content) { const lines = content.split('\n'); const result = {}; let currentSection = ''; lines.forEach(line => { line = line.trim(); if (line.startsWith('[') && line.endsWith(']')) { currentSection = line.slice(1, -1); if (!result[currentSection]) { result[currentSection] = {}; } } else if (line.includes('=') && currentSection) { const [key, value] = line.split('=').map(s => s.trim()); result[currentSection][key] = value.replace(/['"]/g, ''); } }); return result; } static async getProjectSize(directory) { let totalSize = 0; const files = await (0, glob_1.glob)('**/*', { cwd: directory, ignore: ['node_modules/**', '.git/**', 'dist/**', 'build/**'], absolute: true }); for (const file of files) { try { const stats = await fs_extra_1.default.stat(file); if (stats.isFile()) { totalSize += stats.size; } } catch (error) { // Skip files that can't be read } } return totalSize; } static formatFileSize(bytes) { const sizes = ['Bytes', 'KB', 'MB', 'GB']; if (bytes === 0) return '0 Bytes'; const i = Math.floor(Math.log(bytes) / Math.log(1024)); return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i]; } } exports.FileUtils = FileUtils; //# sourceMappingURL=fileUtils.js.map