first
This commit is contained in:
300
advanced-duplicate-removal.js
Normal file
300
advanced-duplicate-removal.js
Normal file
@@ -0,0 +1,300 @@
|
||||
/**
|
||||
* @fileoverview Advanced duplicate parameter removal for endpoints.js
|
||||
* Detects and removes all types of duplicate parameters
|
||||
*/
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
/**
|
||||
* Advanced duplicate parameter removal
|
||||
*/
|
||||
function advancedDuplicateRemoval() {
|
||||
try {
|
||||
console.log('=== ADVANCED DUPLICATE PARAMETER REMOVAL ===');
|
||||
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_advanced_backup_${Date.now()}.js`);
|
||||
fs.writeFileSync(backupPath, content);
|
||||
console.log(`💾 Backup created: ${backupPath}`);
|
||||
|
||||
let totalDuplicatesRemoved = 0;
|
||||
|
||||
// Process each endpoint section
|
||||
const sections = [
|
||||
'PUBLIC_ENDPOINTS',
|
||||
'PROVIDER_ENDPOINTS',
|
||||
'PATIENT_ENDPOINTS',
|
||||
'PARTNER_ENDPOINTS',
|
||||
'AFFILIATE_ENDPOINTS',
|
||||
'NETWORK_ENDPOINTS'
|
||||
];
|
||||
|
||||
sections.forEach(sectionName => {
|
||||
console.log(`🔧 Processing ${sectionName}...`);
|
||||
|
||||
const sectionRegex = new RegExp(`(export const ${sectionName}\\s*=\\s*\\[)([\\s\\S]*?)(\\];)`, 'g');
|
||||
|
||||
content = content.replace(sectionRegex, (match, start, sectionContent, end) => {
|
||||
const result = removeDuplicatesFromSection(sectionContent, sectionName);
|
||||
totalDuplicatesRemoved += result.duplicatesRemoved;
|
||||
|
||||
if (result.duplicatesRemoved > 0) {
|
||||
console.log(` ✅ Removed ${result.duplicatesRemoved} duplicate parameters`);
|
||||
}
|
||||
|
||||
return start + result.cleanedContent + end;
|
||||
});
|
||||
});
|
||||
|
||||
// Write the fixed content
|
||||
fs.writeFileSync(endpointsPath, content);
|
||||
|
||||
console.log(`📊 Fixed file size: ${content.length} characters`);
|
||||
console.log(`🎯 Total duplicate parameters removed: ${totalDuplicatesRemoved}`);
|
||||
console.log('');
|
||||
console.log('✅ Advanced duplicate removal completed!');
|
||||
|
||||
return {
|
||||
backupPath: backupPath,
|
||||
duplicatesRemoved: totalDuplicatesRemoved,
|
||||
success: true
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error in advanced duplicate removal:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove duplicates from a specific section
|
||||
*/
|
||||
function removeDuplicatesFromSection(sectionContent, sectionName) {
|
||||
let duplicatesRemoved = 0;
|
||||
let cleanedContent = sectionContent;
|
||||
|
||||
// Find all endpoint objects in this section
|
||||
const endpointMatches = [];
|
||||
const endpointRegex = /\{[\s\S]*?\}/g;
|
||||
let match;
|
||||
|
||||
while ((match = endpointRegex.exec(sectionContent)) !== null) {
|
||||
endpointMatches.push({
|
||||
original: match[0],
|
||||
start: match.index,
|
||||
end: match.index + match[0].length
|
||||
});
|
||||
}
|
||||
|
||||
// Process each endpoint
|
||||
endpointMatches.forEach((endpoint, index) => {
|
||||
const result = removeDuplicatesFromEndpoint(endpoint.original);
|
||||
if (result.duplicatesRemoved > 0) {
|
||||
duplicatesRemoved += result.duplicatesRemoved;
|
||||
cleanedContent = cleanedContent.replace(endpoint.original, result.cleanedEndpoint);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
cleanedContent,
|
||||
duplicatesRemoved
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove duplicates from a single endpoint
|
||||
*/
|
||||
function removeDuplicatesFromEndpoint(endpointStr) {
|
||||
let duplicatesRemoved = 0;
|
||||
let cleanedEndpoint = endpointStr;
|
||||
|
||||
// Find the parameters section
|
||||
const paramMatch = endpointStr.match(/parameters:\s*\{([\s\S]*?)\}(?=\s*[,}])/);
|
||||
|
||||
if (paramMatch) {
|
||||
const paramContent = paramMatch[1];
|
||||
const result = removeDuplicateParameters(paramContent);
|
||||
|
||||
if (result.duplicatesRemoved > 0) {
|
||||
duplicatesRemoved = result.duplicatesRemoved;
|
||||
|
||||
// Replace the parameters section
|
||||
cleanedEndpoint = endpointStr.replace(
|
||||
/parameters:\s*\{[\s\S]*?\}(?=\s*[,}])/,
|
||||
`parameters: {${result.cleanedContent}}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
cleanedEndpoint,
|
||||
duplicatesRemoved
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove duplicate parameters from parameter content
|
||||
*/
|
||||
function removeDuplicateParameters(paramContent) {
|
||||
const seenParameters = new Map();
|
||||
const cleanParameters = [];
|
||||
let duplicatesRemoved = 0;
|
||||
|
||||
// Split into lines and process each parameter
|
||||
const lines = paramContent.split('\n');
|
||||
let currentParam = null;
|
||||
let currentParamLines = [];
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
|
||||
// Check if this line starts a new parameter
|
||||
const paramMatch = line.match(/^\s*([a-zA-Z_"'][^:]*?):\s*\{/);
|
||||
|
||||
if (paramMatch) {
|
||||
// Save previous parameter if it exists
|
||||
if (currentParam && currentParamLines.length > 0) {
|
||||
const paramName = currentParam.replace(/['"]/g, ''); // Remove quotes for comparison
|
||||
|
||||
if (!seenParameters.has(paramName)) {
|
||||
seenParameters.set(paramName, true);
|
||||
cleanParameters.push(...currentParamLines);
|
||||
} else {
|
||||
duplicatesRemoved++;
|
||||
}
|
||||
}
|
||||
|
||||
// Start new parameter
|
||||
currentParam = paramMatch[1];
|
||||
currentParamLines = [line];
|
||||
} else if (currentParam) {
|
||||
// Continue current parameter
|
||||
currentParamLines.push(line);
|
||||
|
||||
// Check if this line ends the current parameter
|
||||
if (line.includes('}')) {
|
||||
// Parameter definition complete
|
||||
const paramName = currentParam.replace(/['"]/g, '');
|
||||
|
||||
if (!seenParameters.has(paramName)) {
|
||||
seenParameters.set(paramName, true);
|
||||
cleanParameters.push(...currentParamLines);
|
||||
} else {
|
||||
duplicatesRemoved++;
|
||||
}
|
||||
|
||||
currentParam = null;
|
||||
currentParamLines = [];
|
||||
}
|
||||
} else {
|
||||
// Line not part of a parameter (whitespace, comments, etc.)
|
||||
cleanParameters.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle any remaining parameter
|
||||
if (currentParam && currentParamLines.length > 0) {
|
||||
const paramName = currentParam.replace(/['"]/g, '');
|
||||
|
||||
if (!seenParameters.has(paramName)) {
|
||||
seenParameters.set(paramName, true);
|
||||
cleanParameters.push(...currentParamLines);
|
||||
} else {
|
||||
duplicatesRemoved++;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
cleanedContent: cleanParameters.join('\n'),
|
||||
duplicatesRemoved
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the cleaned file
|
||||
*/
|
||||
function validateCleanedFile() {
|
||||
try {
|
||||
console.log('🔍 Validating cleaned endpoints.js...');
|
||||
|
||||
const endpointsPath = path.join(process.cwd(), 'src/config/endpoints.js');
|
||||
const content = fs.readFileSync(endpointsPath, 'utf8');
|
||||
|
||||
// Basic validation checks
|
||||
const issues = [];
|
||||
|
||||
// Check for unmatched brackets
|
||||
const openBrackets = (content.match(/\{/g) || []).length;
|
||||
const closeBrackets = (content.match(/\}/g) || []).length;
|
||||
if (openBrackets !== closeBrackets) {
|
||||
issues.push(`Unmatched brackets: ${openBrackets} open, ${closeBrackets} close`);
|
||||
}
|
||||
|
||||
// Check for duplicate parameter names in the same block
|
||||
const paramBlocks = content.match(/parameters:\s*\{[^}]*\}/g) || [];
|
||||
paramBlocks.forEach((block, index) => {
|
||||
const paramNames = [];
|
||||
const paramMatches = block.match(/\s*([a-zA-Z_"'][^:]*?):\s*\{/g) || [];
|
||||
|
||||
paramMatches.forEach(match => {
|
||||
const name = match.match(/\s*([a-zA-Z_"'][^:]*?):\s*\{/)[1].replace(/['"]/g, '');
|
||||
if (paramNames.includes(name)) {
|
||||
issues.push(`Duplicate parameter '${name}' in block ${index + 1}`);
|
||||
} else {
|
||||
paramNames.push(name);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (issues.length === 0) {
|
||||
console.log('✅ File validation passed - no duplicate parameters found');
|
||||
return true;
|
||||
} else {
|
||||
console.log('⚠️ Validation issues found:');
|
||||
issues.forEach(issue => console.log(` - ${issue}`));
|
||||
return false;
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error validating file:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Run the advanced duplicate removal
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
(async () => {
|
||||
try {
|
||||
const result = advancedDuplicateRemoval();
|
||||
|
||||
console.log('');
|
||||
console.log('=== VALIDATION ===');
|
||||
|
||||
const isValid = validateCleanedFile();
|
||||
|
||||
if (isValid && result.duplicatesRemoved > 0) {
|
||||
console.log('🎉 All duplicate parameters successfully removed!');
|
||||
} else if (isValid && result.duplicatesRemoved === 0) {
|
||||
console.log('✅ No duplicate parameters found - file is already clean!');
|
||||
} else {
|
||||
console.log('⚠️ Some issues may remain. Manual review recommended.');
|
||||
}
|
||||
|
||||
console.log(`💾 Backup saved: ${result.backupPath}`);
|
||||
console.log(`🎯 Duplicates removed: ${result.duplicatesRemoved}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to remove duplicate parameters:', error);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
export { advancedDuplicateRemoval };
|
Reference in New Issue
Block a user