first
This commit is contained in:
273
remove-only-duplicates.js
Normal file
273
remove-only-duplicates.js
Normal file
@@ -0,0 +1,273 @@
|
||||
/**
|
||||
* @fileoverview Remove only duplicate parameters while preserving all original parameters
|
||||
* This script keeps all parameters but removes duplicates within the same endpoint
|
||||
*/
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
/**
|
||||
* Remove only duplicate parameters, keep all original parameters
|
||||
*/
|
||||
function removeOnlyDuplicateParameters() {
|
||||
try {
|
||||
console.log('=== REMOVING ONLY DUPLICATE PARAMETERS ===');
|
||||
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_duplicate_removal_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 = removeDuplicatesFromSectionOnly(sectionContent, sectionName);
|
||||
totalDuplicatesRemoved += result.duplicatesRemoved;
|
||||
|
||||
if (result.duplicatesRemoved > 0) {
|
||||
console.log(` ✅ Removed ${result.duplicatesRemoved} duplicate parameters`);
|
||||
} else {
|
||||
console.log(` ✅ No duplicates found`);
|
||||
}
|
||||
|
||||
return start + result.cleanedContent + end;
|
||||
});
|
||||
});
|
||||
|
||||
// Fix any syntax issues without removing parameters
|
||||
console.log('🔧 Fixing syntax issues...');
|
||||
content = fixSyntaxIssuesOnly(content);
|
||||
|
||||
// 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('✅ Duplicate parameter removal completed!');
|
||||
|
||||
return {
|
||||
backupPath: backupPath,
|
||||
duplicatesRemoved: totalDuplicatesRemoved,
|
||||
success: true
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error removing duplicate parameters:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove duplicates from a specific section while preserving all parameters
|
||||
*/
|
||||
function removeDuplicatesFromSectionOnly(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 to remove duplicates within that endpoint only
|
||||
endpointMatches.forEach((endpoint, index) => {
|
||||
const result = removeDuplicatesFromSingleEndpoint(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 while preserving all unique parameters
|
||||
*/
|
||||
function removeDuplicatesFromSingleEndpoint(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 = removeDuplicateParametersOnly(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 while preserving all unique parameters
|
||||
*/
|
||||
function removeDuplicateParametersOnly(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 = [];
|
||||
let inParameterDefinition = false;
|
||||
|
||||
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++;
|
||||
console.log(` Removing duplicate parameter: ${paramName}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Start new parameter
|
||||
currentParam = paramMatch[1];
|
||||
currentParamLines = [line];
|
||||
inParameterDefinition = true;
|
||||
} else if (inParameterDefinition && 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++;
|
||||
console.log(` Removing duplicate parameter: ${paramName}`);
|
||||
}
|
||||
|
||||
currentParam = null;
|
||||
currentParamLines = [];
|
||||
inParameterDefinition = false;
|
||||
}
|
||||
} else {
|
||||
// Line not part of a parameter (whitespace, comments, etc.)
|
||||
if (!inParameterDefinition) {
|
||||
cleanParameters.push(line);
|
||||
} else {
|
||||
// Part of current parameter
|
||||
currentParamLines.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++;
|
||||
console.log(` Removing duplicate parameter: ${paramName}`);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
cleanedContent: cleanParameters.join('\n'),
|
||||
duplicatesRemoved
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix only syntax issues without removing parameters
|
||||
*/
|
||||
function fixSyntaxIssuesOnly(content) {
|
||||
// Fix bracket notation in parameter names
|
||||
content = content.replace(/(\s+)([a-zA-Z_][a-zA-Z0-9_]*\[[^\]]+\](?:\[[^\]]+\])?)(\s*:\s*\{)/g, '$1"$2"$3');
|
||||
|
||||
// Fix missing commas between parameters (but preserve all parameters)
|
||||
content = content.replace(/(\}\s*)\n(\s+[a-zA-Z_"'])/g, '$1,\n$2');
|
||||
|
||||
// Fix trailing commas before closing braces
|
||||
content = content.replace(/,(\s*\})/g, '$1');
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
// Run the duplicate removal
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
(async () => {
|
||||
try {
|
||||
const result = removeOnlyDuplicateParameters();
|
||||
|
||||
console.log('');
|
||||
console.log('=== SUMMARY ===');
|
||||
console.log(`💾 Backup saved: ${result.backupPath}`);
|
||||
console.log(`🎯 Duplicates removed: ${result.duplicatesRemoved}`);
|
||||
console.log('✅ All original parameters preserved');
|
||||
console.log('✅ Only duplicate parameters removed');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to remove duplicate parameters:', error);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
export { removeOnlyDuplicateParameters };
|
Reference in New Issue
Block a user