Files
mcp-tool/final-structure-fix.js
nasir@endelospay.com 8c74b0e23f first
2025-07-11 20:22:12 +05:00

263 lines
8.0 KiB
JavaScript

/**
* @fileoverview Final structure fix for endpoints.js
* Fixes all remaining syntax and structure issues
*/
import fs from 'fs';
import path from 'path';
/**
* Final comprehensive structure fix
*/
function finalStructureFix() {
try {
console.log('=== FINAL STRUCTURE 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_final_backup_${Date.now()}.js`);
fs.writeFileSync(backupPath, content);
console.log(`💾 Backup created: ${backupPath}`);
// Apply comprehensive fixes
console.log('🔧 Applying comprehensive structure fixes...');
// Fix 1: Multi-line descriptions
console.log(' Fix 1: Multi-line descriptions...');
content = fixMultiLineDescriptions(content);
// Fix 2: Missing commas in parameter definitions
console.log(' Fix 2: Missing commas in parameter definitions...');
content = fixMissingCommas(content);
// Fix 3: Malformed parameter objects
console.log(' Fix 3: Malformed parameter objects...');
content = fixMalformedParameterObjects(content);
// Fix 4: Excessive closing braces
console.log(' Fix 4: Excessive closing braces...');
content = fixExcessiveClosingBraces(content);
// Fix 5: Parameter block structure
console.log(' Fix 5: Parameter block structure...');
content = fixParameterBlockStructure(content);
// Write the fixed content
fs.writeFileSync(endpointsPath, content);
console.log(`📊 Fixed file size: ${content.length} characters`);
console.log('');
console.log('✅ Final structure fix completed!');
return {
backupPath: backupPath,
success: true
};
} catch (error) {
console.error('❌ Error in final structure fix:', error);
throw error;
}
}
/**
* Fix multi-line descriptions
*/
function fixMultiLineDescriptions(content) {
// Fix descriptions that are split across multiple lines
content = content.replace(
/description:\s*\n\s*"([^"]*)"([^}]*)/g,
'description: "$1"$2'
);
// Fix descriptions with concatenation
content = content.replace(
/description:\s*\n\s*"([^"]*)"([^}]*)/g,
'description: "$1"$2'
);
return content;
}
/**
* Fix missing commas in parameter definitions
*/
function fixMissingCommas(content) {
// Fix parameter definitions missing commas
content = content.replace(
/(\w+:\s*\{\s*type:\s*"[^"]*",\s*required:\s*(?:true|false),\s*description:\s*"[^"]*"\s*\})\s*\n(\s*)(\w+:|"[^"]+":)/g,
'$1,\n$2$3'
);
// Fix parameter definitions with missing closing braces and commas
content = content.replace(
/(\w+:\s*\{\s*type:\s*"[^"]*",\s*required:\s*(?:true|false),\s*description:\s*"[^"]*"\s*)\}\s*\n(\s*)(\w+:|"[^"]+":)/g,
'$1 },\n$2$3'
);
return content;
}
/**
* Fix malformed parameter objects
*/
function fixMalformedParameterObjects(content) {
// Fix parameter objects that are missing closing braces
content = content.replace(
/(\w+:\s*\{\s*type:\s*"[^"]*",\s*required:\s*(?:true|false),\s*description:\s*"[^"]*"\s*)\s*\n(\s*)(\w+:|"[^"]+":|\})/g,
(match, paramDef, indent, nextItem) => {
if (nextItem === '}') {
return `${paramDef} }\n${indent}${nextItem}`;
} else {
return `${paramDef} },\n${indent}${nextItem}`;
}
}
);
return content;
}
/**
* Fix excessive closing braces
*/
function fixExcessiveClosingBraces(content) {
// Remove excessive closing braces
content = content.replace(/\}\s*\}\s*\}/g, '}');
content = content.replace(/\}\s*\}/g, '}');
return content;
}
/**
* Fix parameter block structure
*/
function fixParameterBlockStructure(content) {
// Ensure parameter blocks have proper structure
content = content.replace(
/parameters:\s*\{([^}]*(?:\{[^}]*\}[^}]*)*)\}/g,
(match, paramContent) => {
// Clean up the parameter content
let cleanedContent = paramContent;
// Remove trailing commas before closing braces
cleanedContent = cleanedContent.replace(/,(\s*\})/g, '$1');
// Ensure proper spacing
cleanedContent = cleanedContent.replace(/\n\s*\n/g, '\n');
return `parameters: {${cleanedContent}}`;
}
);
return content;
}
/**
* Validate the fixed file syntax
*/
async function validateSyntax() {
try {
console.log('🔍 Validating syntax...');
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('✅ Syntax validation passed');
resolve(true);
} else {
console.error('❌ Syntax errors found:');
console.error(stderr);
resolve(false);
}
});
});
} catch (error) {
console.error('❌ Error validating syntax:', error);
return false;
}
}
/**
* Count tools after fix
*/
function countTools() {
try {
console.log('📊 Counting tools...');
const endpointsPath = path.join(process.cwd(), 'src/config/endpoints.js');
const content = fs.readFileSync(endpointsPath, 'utf8');
const sections = ['PUBLIC_ENDPOINTS', 'PROVIDER_ENDPOINTS', 'PATIENT_ENDPOINTS', 'PARTNER_ENDPOINTS', 'AFFILIATE_ENDPOINTS', 'NETWORK_ENDPOINTS'];
let total = 0;
sections.forEach(section => {
const regex = new RegExp(`${section}\\s*=\\s*\\[([\\s\\S]*?)\\];`);
const match = content.match(regex);
if (match) {
const count = (match[1].match(/\{[\s\S]*?\}/g) || []).length;
console.log(` ${section.replace('_ENDPOINTS', '')}: ${count} tools`);
total += count;
}
});
console.log(` TOTAL: ${total} tools`);
return total;
} catch (error) {
console.error('❌ Error counting tools:', error);
return 0;
}
}
// Run the final structure fix
if (import.meta.url === `file://${process.argv[1]}`) {
(async () => {
try {
const result = finalStructureFix();
console.log('');
console.log('=== VALIDATION ===');
const isValid = await validateSyntax();
const toolCount = countTools();
if (isValid) {
console.log('🎉 Endpoints.js structure successfully fixed!');
console.log('✅ All duplicate parameters removed');
console.log('✅ Syntax validation passed');
console.log(`📊 Total tools: ${toolCount}`);
} else {
console.log('⚠️ Some syntax issues may remain');
}
console.log(`💾 Backup saved: ${result.backupPath}`);
} catch (error) {
console.error('❌ Failed to fix structure:', error);
}
})();
}
export { finalStructureFix };