68 lines
2.1 KiB
JavaScript
68 lines
2.1 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Quick Fix Tool - Reparerer materialPackageService initialisering
|
|
*/
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const fixServerFile = () => {
|
|
const serverPath = path.join(__dirname, 'unified-server.js');
|
|
let content = fs.readFileSync(serverPath, 'utf8');
|
|
|
|
// Find hvor API routes starter
|
|
const apiRoutesStart = content.indexOf('// Get all material packages');
|
|
const apiRoutesEnd = content.indexOf('// Handle React Router');
|
|
|
|
if (apiRoutesStart === -1 || apiRoutesEnd === -1) {
|
|
console.log('❌ Kunne ikke finde API routes sektion');
|
|
return;
|
|
}
|
|
|
|
// Extract API routes
|
|
const apiRoutes = content.substring(apiRoutesStart, apiRoutesEnd);
|
|
|
|
// Remove API routes from current position
|
|
const beforeRoutes = content.substring(0, apiRoutesStart);
|
|
const afterRoutes = content.substring(apiRoutesEnd);
|
|
|
|
// Find where to insert API routes (after initializeBackend function)
|
|
const insertPoint = content.indexOf('// Start server');
|
|
|
|
if (insertPoint === -1) {
|
|
console.log('❌ Kunne ikke finde insertion point');
|
|
return;
|
|
}
|
|
|
|
// Create new content with API routes moved
|
|
const beforeInsert = content.substring(0, insertPoint);
|
|
const afterInsert = content.substring(insertPoint);
|
|
|
|
// Add guard check for materialPackageService
|
|
const guardedApiRoutes = `
|
|
// Initialize API routes after backend is ready
|
|
const initializeAPIRoutes = () => {
|
|
if (!materialPackageService) {
|
|
console.error('❌ materialPackageService not initialized!');
|
|
return;
|
|
}
|
|
|
|
console.log('✅ Initializing Material Package API routes...');
|
|
|
|
${apiRoutes.replace(/^/gm, ' ')}
|
|
};
|
|
|
|
`;
|
|
|
|
const newContent = beforeInsert + guardedApiRoutes + afterInsert;
|
|
|
|
// Remove old API routes section
|
|
const cleanedContent = newContent.replace(apiRoutes, '');
|
|
|
|
fs.writeFileSync(serverPath, cleanedContent);
|
|
console.log('✅ Server file fixed!');
|
|
};
|
|
|
|
console.log('🔧 Fixing materialPackageService initialization...');
|
|
fixServerFile(); |