kira-crud
Version:
Intelligent CRUD Generator for Laravel and Angular
141 lines (112 loc) • 4.42 kB
JavaScript
/**
* Script pour corriger les routes Angular manquantes
* Ce script analyse app.routes.ts et ajoute les routes pour les composants importés
*/
const fs = require('fs').promises;
const path = require('path');
const chalk = require('chalk');
const ora = require('ora');
async function fixAngularRoutes(routesFilePath) {
const spinner = ora('Analysing Angular routes file...').start();
try {
// Lire le fichier de routes
const content = await fs.readFile(routesFilePath, 'utf8');
// Analyser les importations
const importRegex = /import\s+{\s+(\w+)Component\s+}\s+from\s+['"](.+)['"];/g;
let match;
const components = [];
while ((match = importRegex.exec(content)) !== null) {
const componentName = match[1];
let importPath = match[2];
// Skip LoginComponent
if (componentName === 'Login') continue;
// Vérifier si le chemin d'importation est correct (doit se terminer par /${kebab-case}.component)
const kebabCase = componentName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
if (!importPath.endsWith(`/${kebabCase}.component`)) {
// Corriger le chemin d'importation
const basePath = importPath.replace(/\/$/, '');
const correctPath = `${basePath}/${kebabCase}.component`;
// Mettre à jour le contenu avec le chemin corrigé
content = content.replace(
`import { ${componentName}Component } from '${importPath}';`,
`import { ${componentName}Component } from '${correctPath}';`
);
// Utiliser le chemin corrigé
importPath = correctPath;
spinner.info(`Corrected import path for ${componentName}Component: ${correctPath}`);
}
components.push({
name: componentName,
importPath
});
}
spinner.text = `Found ${components.length} component imports`;
// Lire le fichier paths.ts pour vérifier les enums
const pathsFilePath = path.join(path.dirname(routesFilePath), 'paths.ts');
const pathsContent = await fs.readFile(pathsFilePath, 'utf8');
// Vérifier quels composants ont déjà des routes
const routeRegex = /path:\s+Paths\.(\w+)/g;
const existingRoutes = [];
while ((match = routeRegex.exec(content)) !== null) {
existingRoutes.push(match[1]);
}
// Mettre à jour le fichier de routes
let updatedContent = content;
let addedRoutes = 0;
for (const component of components) {
const enumName = component.name.toUpperCase();
// Vérifier si l'enum existe dans paths.ts
if (!pathsContent.includes(`${enumName} =`)) {
spinner.warn(`Enum Paths.${enumName} not found in paths.ts, skipping`);
continue;
}
// Vérifier si la route existe déjà
if (existingRoutes.includes(enumName)) {
spinner.info(`Route for ${component.name}Component already exists`);
continue;
}
// Ajouter la route
const routeEntry = ` {
path: Paths.${enumName},
component: ${component.name}Component
},`;
// Insérer juste avant le dernier crochet fermant
if (updatedContent.includes(']\;')) {
updatedContent = updatedContent.replace(
/\n(\s*)\]\;/,
`,\n${routeEntry}\n$1];`
);
addedRoutes++;
}
}
if (addedRoutes > 0) {
// Écrire le fichier mis à jour
await fs.writeFile(routesFilePath, updatedContent, 'utf8');
spinner.succeed(`Added ${addedRoutes} missing routes to app.routes.ts`);
} else {
spinner.info('No routes needed to be added');
}
} catch (error) {
spinner.fail(`Error fixing Angular routes: ${error.message}`);
console.error(chalk.red(error.stack));
}
}
// Point d'entrée du script
if (require.main === module) {
const routesPath = process.argv[2];
if (!routesPath) {
console.error(chalk.red('Please provide the path to app.routes.ts'));
console.log(chalk.yellow('Usage: node fix-angular-routes.js <path-to-app-routes.ts>'));
process.exit(1);
}
const absolutePath = path.resolve(routesPath);
fixAngularRoutes(absolutePath)
.catch(error => {
console.error(chalk.red(`Error: ${error.message}`));
process.exit(1);
});
}
module.exports = {
fixAngularRoutes
};