/** * @fileoverview Fix syntax errors in endpoints.js * Specifically handles bracket notation in parameter names and missing commas/braces */ import fs from 'fs'; import path from 'path'; /** * Fix all syntax errors in endpoints.js */ function fixSyntaxErrors() { try { console.log('=== FIXING SYNTAX ERRORS IN ENDPOINTS.JS ==='); console.log(''); const endpointsPath = path.join(process.cwd(), 'src/config/endpoints.js'); let content = fs.readFileSync(endpointsPath, 'utf8'); console.log('📁 Reading endpoints.js...'); console.log(`📊 Original file size: ${content.length} characters`); // Create backup const backupPath = path.join(process.cwd(), `endpoints_syntax_backup_${Date.now()}.js`); fs.writeFileSync(backupPath, content); console.log(`💾 Backup created: ${backupPath}`); // Fix bracket notation in parameter names console.log('🔧 Fixing bracket notation in parameter names...'); content = fixBracketNotation(content); // Fix missing commas and braces console.log('🔧 Fixing missing commas and braces...'); content = fixMissingCommasAndBraces(content); // Fix trailing commas console.log('🔧 Fixing trailing commas...'); content = fixTrailingCommas(content); // Write the fixed content fs.writeFileSync(endpointsPath, content); console.log(`📊 Fixed file size: ${content.length} characters`); console.log(''); console.log('✅ Syntax errors fixed successfully!'); return { backupPath: backupPath, success: true }; } catch (error) { console.error('❌ Error fixing syntax errors:', error); throw error; } } /** * Fix bracket notation in parameter names */ function fixBracketNotation(content) { console.log(' Processing bracket notation...'); // Fix parameter names with brackets - need to quote them const bracketPatterns = [ // search[value] -> "search[value]" /(\s+)([a-zA-Z_][a-zA-Z0-9_]*\[[^\]]+\])(\s*:\s*\{)/g, // order[0][column] -> "order[0][column]" /(\s+)([a-zA-Z_][a-zA-Z0-9_]*\[[^\]]+\]\[[^\]]+\])(\s*:\s*\{)/g ]; bracketPatterns.forEach(pattern => { content = content.replace(pattern, (match, indent, paramName, rest) => { return `${indent}"${paramName}"${rest}`; }); }); console.log(' ✅ Bracket notation fixed'); return content; } /** * Fix missing commas and braces */ function fixMissingCommasAndBraces(content) { console.log(' Processing missing commas and braces...'); // Fix missing commas after parameter definitions // Look for patterns like: } description: "..." , content = content.replace(/(\}\s*,?\s*description:\s*"[^"]*"\s*),?\s*\n/g, '$1 },\n'); // Fix missing commas after closing braces in parameter definitions content = content.replace(/(\}\s*description:\s*"[^"]*"\s*)\s*\n(\s+[a-zA-Z_"'])/g, '$1 },\n$2'); // Fix missing closing braces for parameter objects content = content.replace(/(\s+[a-zA-Z_"'][^:]*:\s*\{\s*type:\s*"[^"]*",\s*required:\s*[^,]*,\s*description:\s*"[^"]*"\s*),?\s*\n(\s+[a-zA-Z_"'])/g, '$1 },\n$2'); console.log(' ✅ Missing commas and braces fixed'); return content; } /** * Fix trailing commas */ function fixTrailingCommas(content) { console.log(' Processing trailing commas...'); // Remove trailing commas before closing braces content = content.replace(/,(\s*\})/g, '$1'); // Remove trailing commas before closing brackets content = content.replace(/,(\s*\])/g, '$1'); console.log(' ✅ Trailing commas fixed'); return content; } /** * Validate the fixed file */ async function validateFixedFile() { try { console.log('🔍 Validating fixed endpoints.js...'); const endpointsPath = path.join(process.cwd(), 'src/config/endpoints.js'); // Use Node.js syntax check const { spawn } = await import('child_process'); return new Promise((resolve) => { const child = spawn('node', ['-c', endpointsPath], { stdio: ['pipe', 'pipe', 'pipe'] }); let stderr = ''; child.stderr.on('data', (data) => { stderr += data.toString(); }); child.on('close', (code) => { if (code === 0) { console.log('✅ File syntax is valid'); resolve(true); } else { console.error('❌ Syntax errors still exist:'); console.error(stderr); resolve(false); } }); }); } catch (error) { console.error('❌ Error validating file:', error); return false; } } // Run the fix if (import.meta.url === `file://${process.argv[1]}`) { (async () => { try { const result = fixSyntaxErrors(); console.log(''); console.log('=== VALIDATION ==='); const isValid = await validateFixedFile(); if (isValid) { console.log('🎉 Endpoints.js syntax successfully fixed and validated!'); } else { console.log('⚠️ Some syntax errors may remain. Manual review needed.'); } console.log(`💾 Backup saved: ${result.backupPath}`); } catch (error) { console.error('❌ Failed to fix syntax errors:', error); } })(); } export { fixSyntaxErrors };