UNPKG

brainrotscript

Version:

🧠 A brainrot programming language that compiles to JavaScript - because why write normal code when you can write code that's absolutely sending you? 💀

150 lines (122 loc) 5.79 kB
const fs = require('fs'); const path = require('path'); const BrainrotCompiler = require('./compiler'); class ReactBrainrotCompiler extends BrainrotCompiler { constructor() { super(); } /** * Compiles a .brainrot React component to .jsx * @param {string} inputPath - Path to the .brainrot file * @param {string} outputPath - Path where the compiled .jsx file should be saved */ compileToReact(inputPath, outputPath) { try { // Read the brainrot source code const brainrotCode = fs.readFileSync(inputPath, 'utf8'); // Translate to JavaScript/JSX let jsxCode = this.translateToJavaScript(brainrotCode); // Additional React-specific transformations jsxCode = this.handleReactSpecifics(jsxCode); // Write the compiled JSX const jsxOutputPath = outputPath || inputPath.replace('.brainrot', '.jsx'); fs.writeFileSync(jsxOutputPath, jsxCode); console.log(`✅ Successfully compiled React component: ${inputPath}${jsxOutputPath}`); return jsxCode; } catch (error) { console.error(`❌ React compilation failed: ${error.message}`); throw error; } } /** * Handle React-specific syntax transformations * @param {string} jsxCode - The JavaScript/JSX code * @returns {string} - Enhanced JSX code */ handleReactSpecifics(jsxCode) { // Handle export default -> export default jsxCode = jsxCode.replace(/drop mid/g, 'export default'); // Handle import statements (yoink -> import already handled by base compiler) // Ensure proper JSX returns jsxCode = jsxCode.replace(/its_giving \(/g, 'return ('); // Add React import if not present and JSX is detected if (jsxCode.includes('<') && jsxCode.includes('>') && !jsxCode.includes('import React')) { jsxCode = `import React from 'react';\n${jsxCode}`; } return jsxCode; } /** * Compiles all .brainrot files in a directory to .jsx * @param {string} sourceDir - Source directory with .brainrot files * @param {string} outputDir - Output directory for .jsx files */ compileReactProject(sourceDir, outputDir) { try { // Create output directory if it doesn't exist if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir, { recursive: true }); } // Find all .brainrot files const brainrotFiles = this.findBrainrotFiles(sourceDir); console.log(`🧠 Found ${brainrotFiles.length} BrainrotScript files to compile...`); // Compile each file brainrotFiles.forEach(file => { const relativePath = path.relative(sourceDir, file); const outputPath = path.join(outputDir, relativePath.replace('.brainrot', '.jsx')); // Create subdirectories if needed const outputSubdir = path.dirname(outputPath); if (!fs.existsSync(outputSubdir)) { fs.mkdirSync(outputSubdir, { recursive: true }); } this.compileToReact(file, outputPath); }); console.log(`🚀 React project compilation complete! Output: ${outputDir}`); } catch (error) { console.error(`❌ React project compilation failed: ${error.message}`); throw error; } } /** * Recursively find all .brainrot files in a directory * @param {string} dir - Directory to search * @returns {string[]} - Array of .brainrot file paths */ findBrainrotFiles(dir) { let brainrotFiles = []; const items = fs.readdirSync(dir); items.forEach(item => { const fullPath = path.join(dir, item); const stat = fs.statSync(fullPath); if (stat.isDirectory()) { brainrotFiles = brainrotFiles.concat(this.findBrainrotFiles(fullPath)); } else if (item.endsWith('.brainrot')) { brainrotFiles.push(fullPath); } }); return brainrotFiles; } /** * Watch for changes in .brainrot files and auto-compile * @param {string} sourceDir - Source directory to watch * @param {string} outputDir - Output directory for compiled files */ watchAndCompile(sourceDir, outputDir) { console.log(`👁️ Watching ${sourceDir} for BrainrotScript changes...`); fs.watch(sourceDir, { recursive: true }, (eventType, filename) => { if (filename && filename.endsWith('.brainrot')) { console.log(`🔄 Change detected: ${filename}`); const fullPath = path.join(sourceDir, filename); if (fs.existsSync(fullPath)) { const outputPath = path.join(outputDir, filename.replace('.brainrot', '.jsx')); try { this.compileToReact(fullPath, outputPath); console.log(`✅ Auto-compiled: ${filename}`); } catch (error) { console.error(`❌ Auto-compilation failed for ${filename}: ${error.message}`); } } } }); } } module.exports = ReactBrainrotCompiler;