269 lines
8.6 KiB
JavaScript
269 lines
8.6 KiB
JavaScript
/**
|
|
* @fileoverview Comprehensive syntax fix for endpoints.js
|
|
* Handles all structural issues including duplicates, malformed objects, and syntax errors
|
|
*/
|
|
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
/**
|
|
* Comprehensive fix for all syntax issues
|
|
*/
|
|
function comprehensiveSyntaxFix() {
|
|
try {
|
|
console.log('=== COMPREHENSIVE SYNTAX FIX FOR 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_comprehensive_backup_${Date.now()}.js`);
|
|
fs.writeFileSync(backupPath, content);
|
|
console.log(`💾 Backup created: ${backupPath}`);
|
|
|
|
// Step 1: Fix parameter structure issues
|
|
console.log('🔧 Step 1: Fixing parameter structure...');
|
|
content = fixParameterStructure(content);
|
|
|
|
// Step 2: Remove duplicate parameters
|
|
console.log('🔧 Step 2: Removing duplicate parameters...');
|
|
content = removeDuplicateParameters(content);
|
|
|
|
// Step 3: Fix bracket notation
|
|
console.log('🔧 Step 3: Fixing bracket notation...');
|
|
content = fixBracketNotation(content);
|
|
|
|
// Step 4: Fix missing commas and braces
|
|
console.log('🔧 Step 4: Fixing missing commas and braces...');
|
|
content = fixMissingCommasAndBraces(content);
|
|
|
|
// Step 5: Clean up malformed objects
|
|
console.log('🔧 Step 5: Cleaning up malformed objects...');
|
|
content = cleanupMalformedObjects(content);
|
|
|
|
// Write the fixed content
|
|
fs.writeFileSync(endpointsPath, content);
|
|
|
|
console.log(`📊 Fixed file size: ${content.length} characters`);
|
|
console.log('');
|
|
console.log('✅ Comprehensive syntax fix completed!');
|
|
|
|
return {
|
|
backupPath: backupPath,
|
|
success: true
|
|
};
|
|
|
|
} catch (error) {
|
|
console.error('❌ Error in comprehensive syntax fix:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fix parameter structure issues
|
|
*/
|
|
function fixParameterStructure(content) {
|
|
console.log(' Processing parameter structure...');
|
|
|
|
// Fix malformed parameter definitions like:
|
|
// username: { type: "string", required: true, description: "Username" },
|
|
// username: { type: "string", required: true, description: "username parameter" ,
|
|
// password: { type: "string", required: true, description: "password parameter" }}},
|
|
|
|
// First, fix incomplete parameter definitions
|
|
content = content.replace(
|
|
/(\w+):\s*\{\s*type:\s*"[^"]*",\s*required:\s*[^,]*,\s*description:\s*"[^"]*"\s*,\s*\n\s*(\w+):\s*\{/g,
|
|
'$1: { type: "string", required: true, description: "Parameter" },\n $2: {'
|
|
);
|
|
|
|
// Fix parameter definitions with trailing commas and missing closing braces
|
|
content = content.replace(
|
|
/(\w+):\s*\{\s*type:\s*"([^"]*)",\s*required:\s*([^,]*),\s*description:\s*"([^"]*)"\s*,\s*$/gm,
|
|
'$1: { type: "$2", required: $3, description: "$4" }'
|
|
);
|
|
|
|
console.log(' ✅ Parameter structure fixed');
|
|
return content;
|
|
}
|
|
|
|
/**
|
|
* Remove duplicate parameters within the same parameter block
|
|
*/
|
|
function removeDuplicateParameters(content) {
|
|
console.log(' Processing duplicate parameters...');
|
|
|
|
// Find parameter blocks and clean them
|
|
content = content.replace(/parameters:\s*\{([^}]+)\}/g, (match, paramBlock) => {
|
|
const lines = paramBlock.split('\n');
|
|
const cleanedLines = [];
|
|
const seenParams = new Set();
|
|
|
|
for (const line of lines) {
|
|
const paramMatch = line.match(/^\s*(\w+):\s*\{/);
|
|
if (paramMatch) {
|
|
const paramName = paramMatch[1];
|
|
if (!seenParams.has(paramName)) {
|
|
seenParams.add(paramName);
|
|
cleanedLines.push(line);
|
|
}
|
|
} else {
|
|
cleanedLines.push(line);
|
|
}
|
|
}
|
|
|
|
return `parameters: {${cleanedLines.join('\n')}}`;
|
|
});
|
|
|
|
console.log(' ✅ Duplicate parameters removed');
|
|
return content;
|
|
}
|
|
|
|
/**
|
|
* Fix bracket notation in parameter names
|
|
*/
|
|
function fixBracketNotation(content) {
|
|
console.log(' Processing bracket notation...');
|
|
|
|
// Fix parameter names with brackets - need to quote them
|
|
content = content.replace(
|
|
/(\s+)([a-zA-Z_][a-zA-Z0-9_]*\[[^\]]+\])(\s*:\s*\{)/g,
|
|
'$1"$2"$3'
|
|
);
|
|
|
|
// Fix nested bracket notation like order[0][column]
|
|
content = content.replace(
|
|
/(\s+)([a-zA-Z_][a-zA-Z0-9_]*\[[^\]]+\]\[[^\]]+\])(\s*:\s*\{)/g,
|
|
'$1"$2"$3'
|
|
);
|
|
|
|
console.log(' ✅ Bracket notation fixed');
|
|
return content;
|
|
}
|
|
|
|
/**
|
|
* Fix missing commas and braces
|
|
*/
|
|
function fixMissingCommasAndBraces(content) {
|
|
console.log(' Processing missing commas and braces...');
|
|
|
|
// Fix parameter definitions that are missing closing braces
|
|
content = content.replace(
|
|
/(\w+):\s*\{\s*type:\s*"([^"]*)",\s*required:\s*([^,]*),\s*description:\s*"([^"]*)"\s*\n/g,
|
|
'$1: { type: "$2", required: $3, description: "$4" },\n'
|
|
);
|
|
|
|
// Fix missing commas between parameters
|
|
content = content.replace(
|
|
/(\}\s*)\n(\s+\w+:\s*\{)/g,
|
|
'$1,\n$2'
|
|
);
|
|
|
|
console.log(' ✅ Missing commas and braces fixed');
|
|
return content;
|
|
}
|
|
|
|
/**
|
|
* Clean up malformed objects
|
|
*/
|
|
function cleanupMalformedObjects(content) {
|
|
console.log(' Processing malformed objects...');
|
|
|
|
// Fix excessive closing braces
|
|
content = content.replace(/\}\}\}+/g, '}');
|
|
|
|
// Fix missing opening braces for parameters
|
|
content = content.replace(/parameters:\s*([^{])/g, 'parameters: {\n$1');
|
|
|
|
// Ensure parameters blocks are properly closed
|
|
content = content.replace(/parameters:\s*\{([^}]*)\n\s*\}/g, (match, paramContent) => {
|
|
// Count opening and closing braces in parameter content
|
|
const openBraces = (paramContent.match(/\{/g) || []).length;
|
|
const closeBraces = (paramContent.match(/\}/g) || []).length;
|
|
|
|
if (openBraces > closeBraces) {
|
|
// Add missing closing braces
|
|
const missing = openBraces - closeBraces;
|
|
paramContent += '\n' + ' }'.repeat(missing);
|
|
}
|
|
|
|
return `parameters: {${paramContent}\n }`;
|
|
});
|
|
|
|
// Remove trailing commas before closing braces
|
|
content = content.replace(/,(\s*\})/g, '$1');
|
|
|
|
console.log(' ✅ Malformed objects cleaned up');
|
|
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 = comprehensiveSyntaxFix();
|
|
|
|
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 { comprehensiveSyntaxFix };
|