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? 💀
228 lines (180 loc) • 7.12 kB
JavaScript
const { Command } = require('commander');
const BrainrotCompiler = require('../src/compiler');
const path = require('path');
const fs = require('fs');
const http = require('http');
const url = require('url');
const program = new Command();
const compiler = new BrainrotCompiler();
program
.name('brainrot-web')
.description('🧠 BrainrotScript Web Development - Create websites with brainrot syntax! 🔥')
.version('1.0.0');
program
.command('dev [port]')
.description('Start development server with auto-compilation')
.action((port = 3000) => {
startDevServer(port);
});
program
.command('build')
.description('Build website for production')
.action(() => {
buildWebsite();
});
program
.command('create <name>')
.description('Create a new BrainrotScript website')
.action((name) => {
createWebsite(name);
});
function startDevServer(port) {
console.log('🧠 Starting BrainrotScript development server...');
// Compile BrainrotScript files
compileBrainrotFiles();
// Start HTTP server
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url);
let pathname = parsedUrl.pathname;
// Default to index.html
if (pathname === '/') {
pathname = '/index.html';
}
const filePath = path.join(__dirname, '..', 'website', pathname);
// Serve files
serveFile(filePath, res);
});
server.listen(port, () => {
console.log(`🚀 BrainrotScript website running at http://localhost:${port}`);
console.log('✨ Your website is absolutely sending! 💀');
console.log('🔄 Auto-compiling .brainrot files...');
// Watch for file changes
watchFiles();
});
}
function compileBrainrotFiles() {
try {
const websiteDir = path.join(__dirname, '..', 'website');
const jsDir = path.join(websiteDir, 'js');
// Create js directory if it doesn't exist
if (!fs.existsSync(jsDir)) {
fs.mkdirSync(jsDir, { recursive: true });
}
// Find and compile .brainrot files
const brainrotFiles = findBrainrotFiles(websiteDir);
brainrotFiles.forEach(file => {
const relativePath = path.relative(websiteDir, file);
const outputPath = path.join(jsDir, relativePath.replace('.brainrot', '.js'));
compiler.compile(file, outputPath);
});
console.log(`✅ Compiled ${brainrotFiles.length} BrainrotScript files`);
} catch (error) {
console.error('❌ Compilation failed:', error.message);
}
}
function serveFile(filePath, res) {
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404);
res.end('File not found');
return;
}
// Set content type
const ext = path.extname(filePath);
const contentTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif'
};
const contentType = contentTypes[ext] || 'text/plain';
res.writeHead(200, { 'Content-Type': contentType });
res.end(data);
});
}
function watchFiles() {
const websiteDir = path.join(__dirname, '..', 'website');
fs.watch(websiteDir, { recursive: true }, (eventType, filename) => {
if (filename && filename.endsWith('.brainrot')) {
console.log(`🔄 Change detected: ${filename}`);
compileBrainrotFiles();
console.log('✅ Auto-compiled!');
}
});
}
function findBrainrotFiles(dir) {
let brainrotFiles = [];
try {
const items = fs.readdirSync(dir);
items.forEach(item => {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory() && item !== 'js') { // Skip js output directory
brainrotFiles = brainrotFiles.concat(findBrainrotFiles(fullPath));
} else if (item.endsWith('.brainrot')) {
brainrotFiles.push(fullPath);
}
});
} catch (error) {
// Directory might not exist yet
}
return brainrotFiles;
}
function buildWebsite() {
console.log('🏗️ Building BrainrotScript website for production...');
const websiteDir = path.join(__dirname, '..', 'website');
const buildDir = path.join(__dirname, '..', 'build');
// Create build directory
if (!fs.existsSync(buildDir)) {
fs.mkdirSync(buildDir, { recursive: true });
}
// Copy HTML and CSS files
copyFiles(websiteDir, buildDir, ['.html', '.css', '.png', '.jpg', '.gif']);
// Compile and copy JavaScript
compileBrainrotFiles();
copyFiles(path.join(websiteDir, 'js'), path.join(buildDir, 'js'), ['.js']);
console.log('✅ Website built successfully!');
console.log(`📁 Output: ${buildDir}`);
}
function copyFiles(sourceDir, targetDir, extensions) {
if (!fs.existsSync(sourceDir)) return;
const items = fs.readdirSync(sourceDir);
items.forEach(item => {
const sourcePath = path.join(sourceDir, item);
const targetPath = path.join(targetDir, item);
const stat = fs.statSync(sourcePath);
if (stat.isDirectory()) {
if (!fs.existsSync(targetPath)) {
fs.mkdirSync(targetPath, { recursive: true });
}
copyFiles(sourcePath, targetPath, extensions);
} else {
const ext = path.extname(item);
if (extensions.includes(ext)) {
if (!fs.existsSync(path.dirname(targetPath))) {
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
}
fs.copyFileSync(sourcePath, targetPath);
}
}
});
}
function createWebsite(name) {
console.log(`🧠 Creating new BrainrotScript website: ${name}`);
const projectDir = path.join(process.cwd(), name);
const templateDir = path.join(__dirname, '..', 'website');
// Create project directory
if (!fs.existsSync(projectDir)) {
fs.mkdirSync(projectDir, { recursive: true });
}
// Copy template files
copyFiles(templateDir, projectDir, ['.html', '.brainrot', '.css']);
console.log(`✅ Website created successfully!`);
console.log(`📁 Location: ${projectDir}`);
console.log(`🚀 To start: cd ${name} && brainrot-web dev`);
}
program.parse();
module.exports = { startDevServer, buildWebsite };