Skip to content

Commit

Permalink
feat: After creating the entity import it in server.js 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
fadhlaouir committed Mar 15, 2024
1 parent 0c2a232 commit 45d2b14
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cli/generateCrud.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ async function generateEmptyCrud(entity) {
controllerTemplate,
);
await fs.writeFile(`src/routes/${entity}.route.js`, routeTemplate);

// Line to add in server.js
const lineToAdd = `const ${entity}Routes = require('./src/routes/${entity}/${entity}.route');\napp.use('/v1/api', ${entity}Routes);\n`;

try {
// Append the line to server.js
const serverFilePath = path.join(__dirname, 'server.js');
await fs.appendFile(serverFilePath, lineToAdd);
console.log(`Added routes for ${entity} in server.js`);
} catch (error) {
console.error('Error appending route to server.js:', error);
}
}

/**
Expand Down Expand Up @@ -210,6 +222,18 @@ async function generateMinimalCrud(entity) {
controllerTemplate,
);
await fs.writeFile(`src/routes/${entity}.route.js`, routeTemplate);

// Line to add in server.js
const lineToAdd = `const ${entity}Routes = require('./src/routes/${entity}/${entity}.route');\napp.use('/v1/api', ${entity}Routes);\n`;

try {
// Append the line to server.js
const serverFilePath = path.join(__dirname, 'server.js');
await fs.appendFile(serverFilePath, lineToAdd);
console.log(`Added routes for ${entity} in server.js`);
} catch (error) {
console.error('Error appending route to server.js:', error);
}
}

module.exports = {
Expand Down

0 comments on commit 45d2b14

Please sign in to comment.