Files
yourpart3/backend/fix-index-fields-v2.js
Torsten Schulz (local) 1ab9407e79
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s
Refactor code structure for improved readability and maintainability; optimize performance across multiple modules.
2026-07-21 09:08:15 +02:00

150 lines
5.4 KiB
JavaScript
Executable File
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* Script zur automatischen Korrektur aller Index-Felder - Version 2
* Konvertiert camelCase zu snake_case für underscored: true Models
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Funktion zur Konvertierung von camelCase zu snake_case
function camelToSnakeCase(str) {
return str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
}
// Funktion zur Korrektur einer Model-Datei
function fixModelFile(filePath) {
try {
let content = fs.readFileSync(filePath, 'utf8');
let modified = false;
// Prüfe ob die Datei Indexe enthält
const hasIndexes = content.includes('indexes:') || content.includes('fields:');
if (hasIndexes) {
console.log(`\n🔍 Prüfe: ${path.basename(filePath)} (enthält Indexe)`);
// Suche nach verschiedenen Index-Formaten
const patterns = [
// Standard-Format: fields: ['field1', 'field2']
{
regex: /fields:\s*\[([^\]]+)\]/g,
name: 'Standard fields'
},
// Mit Leerzeichen: fields: [ 'field1', 'field2' ]
{
regex: /fields:\s*\[\s*([^\]]+)\s*\]/g,
name: 'Fields mit Leerzeichen'
}
];
let foundIndexes = false;
for (const pattern of patterns) {
let match;
while ((match = pattern.regex.exec(content)) !== null) {
foundIndexes = true;
const fieldsString = match[1];
console.log(` 📍 ${pattern.name}: ${fieldsString}`);
// Extrahiere Felder
const fields = fieldsString.split(',').map(f => {
const cleaned = f.trim().replace(/['"]/g, '');
return cleaned;
}).filter(f => f.length > 0);
console.log(` 📋 Gefundene Felder: [${fields.join(', ')}]`);
// Konvertiere Felder
const correctedFields = fields.map(field => {
if (field.includes('_')) {
console.log(`${field} (bereits snake_case)`);
return field;
}
const corrected = camelToSnakeCase(field);
if (corrected !== field) {
console.log(` 🔄 ${field}${corrected}`);
modified = true;
}
return corrected;
});
// Ersetze in der Datei
const oldFieldsString = `fields: [${fields.map(f => `'${f}'`).join(', ')}]`;
const newFieldsString = `fields: [${correctedFields.map(f => `'${f}'`).join(', ')}]`;
if (oldFieldsString !== newFieldsString) {
console.log(` 📝 Ersetze: ${oldFieldsString}`);
console.log(` 📝 Durch: ${newFieldsString}`);
content = content.replace(oldFieldsString, newFieldsString);
}
}
}
if (!foundIndexes) {
console.log(` ⚠️ Datei enthält 'indexes:' aber keine 'fields:' Definitionen`);
}
if (modified) {
fs.writeFileSync(filePath, content, 'utf8');
console.log(` ✅ Datei korrigiert und gespeichert`);
} else {
console.log(` Keine Änderungen nötig`);
}
} else {
console.log(`\n🔍 Prüfe: ${path.basename(filePath)} (keine Indexe)`);
}
return modified;
} catch (error) {
console.error(`❌ Fehler bei ${filePath}:`, error.message);
return false;
}
}
// Hauptfunktion
async function main() {
console.log('🔧 Starte automatische Korrektur aller Index-Felder (Version 2)...');
const modelsDir = path.join(__dirname, 'models');
let totalFiles = 0;
let modifiedFiles = 0;
// Rekursiv alle .js-Dateien im models-Verzeichnis durchsuchen
function processDirectory(dir) {
const items = fs.readdirSync(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
processDirectory(fullPath);
} else if (item.endsWith('.js')) {
totalFiles++;
if (fixModelFile(fullPath)) {
modifiedFiles++;
}
}
}
}
processDirectory(modelsDir);
console.log('\n🎉 Korrektur abgeschlossen!');
console.log(`📊 Gesamt: ${totalFiles} Dateien geprüft`);
console.log(`✅ Geändert: ${modifiedFiles} Dateien`);
if (modifiedFiles > 0) {
console.log('\n💡 Führen Sie jetzt das Deployment erneut aus:');
console.log(' sudo ./deploy-backend.sh');
}
}
main().catch(console.error);