- Umbenennung der Indexfelder von camelCase auf snake_case in verschiedenen Modelldateien zur Verbesserung der Konsistenz mit den Datenbankkonventionen. - Verbesserung der Regex-Logik zur Erkennung und Ersetzung von Index-Definitionen und Feld-Arrays im Skript zur automatischen Korrektur.
123 lines
4.2 KiB
JavaScript
123 lines
4.2 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Script zur automatischen Korrektur aller Index-Felder
|
|
* 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;
|
|
|
|
// Suche nach Index-Definitionen - verbesserte Regex
|
|
const indexRegex = /indexes:\s*\[([\s\S]*?)\]/g;
|
|
let match;
|
|
|
|
while ((match = indexRegex.exec(content)) !== null) {
|
|
const indexesSection = match[1];
|
|
|
|
// Suche nach fields-Arrays - verbesserte Regex
|
|
const fieldsRegex = /fields:\s*\[([^\]]+)\]/g;
|
|
let fieldsMatch;
|
|
|
|
while ((fieldsMatch = fieldsRegex.exec(indexesSection)) !== null) {
|
|
const fieldsString = fieldsMatch[1];
|
|
// Verbesserte Feld-Extraktion
|
|
const fields = fieldsString.split(',').map(f => {
|
|
const cleaned = f.trim().replace(/['"]/g, '');
|
|
return cleaned;
|
|
}).filter(f => f.length > 0);
|
|
|
|
// Konvertiere jedes Feld von camelCase zu snake_case
|
|
const correctedFields = fields.map(field => {
|
|
if (field.includes('_')) {
|
|
return field; // Bereits snake_case
|
|
}
|
|
const corrected = camelToSnakeCase(field);
|
|
if (corrected !== field) {
|
|
console.log(` 🔄 ${field} → ${corrected}`);
|
|
modified = true;
|
|
}
|
|
return corrected;
|
|
});
|
|
|
|
// Ersetze die fields-Array - verbesserte Ersetzung
|
|
const oldFieldsString = `fields: [${fields.map(f => `'${f}'`).join(', ')}]`;
|
|
const newFieldsString = `fields: [${correctedFields.map(f => `'${f}'`).join(', ')}]`;
|
|
|
|
// Debug-Ausgabe
|
|
console.log(` 📍 Datei: ${path.basename(filePath)}`);
|
|
console.log(` 🔍 Gefunden: ${oldFieldsString}`);
|
|
console.log(` ✅ Ersetzt: ${newFieldsString}`);
|
|
|
|
content = content.replace(oldFieldsString, newFieldsString);
|
|
}
|
|
}
|
|
|
|
if (modified) {
|
|
fs.writeFileSync(filePath, content, 'utf8');
|
|
console.log(`✅ ${filePath} korrigiert`);
|
|
}
|
|
|
|
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...');
|
|
|
|
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);
|