Skip to content

Commit 9a4c30a

Browse files
committed
feat: auto-register routes and remove plural 's' from generated services
IMPROVEMENTS: 1. Routes now auto-register in src/app.ts (no manual step!) 2. Removed plural 's' from route prefix (/classify instead of /classifys) 3. Removed plural 's' from Prisma table names (classify instead of classifys) HOW IT WORKS: - Generator now modifies src/app.ts to add import and registration - Inserts after Todo routes as a reference point - Uses singular form for both routes and table names BEFORE: ``` Next steps: 1. Register route in src/app.ts manually prefix: '/classifys' ❌ ``` AFTER: ``` ✅ Auto-registered in src/app.ts prefix: '/classify' ✅ ``` Much better DX!
1 parent 872a64b commit 9a4c30a

1 file changed

Lines changed: 30 additions & 12 deletions

File tree

plopfile.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,40 +169,58 @@ export default function (plop) {
169169
type: 'append',
170170
path: 'prisma/schema.prisma',
171171
pattern: /(\/\/ Add models below)/gi,
172-
template: '\n// {{pascalCase name}} model\nmodel {{pascalCase name}} {\n id String @id @default(cuid())\n createdAt DateTime @default(now())\n updatedAt DateTime @updatedAt\n\n @@map("{{kebabCase name}}s")\n}\n',
172+
template: '\n// {{pascalCase name}} model\nmodel {{pascalCase name}} {\n id String @id @default(cuid())\n createdAt DateTime @default(now())\n updatedAt DateTime @updatedAt\n\n @@map("{{kebabCase name}}")\n}\n',
173173
});
174174
}
175175

176-
// 8. Success message
177-
actions.push(() => {
176+
// 8. Auto-register route in src/app.ts
177+
if (data.includeRoute) {
178178
const kebabName = serviceName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
179179
const camelName = serviceName.charAt(0).toLowerCase() + serviceName.slice(1);
180180

181+
// Add import
182+
actions.push({
183+
type: 'modify',
184+
path: 'src/app.ts',
185+
pattern: /(import todoRoutes from '.\/routes\/todos\/index';)/g,
186+
template: `$1\nimport ${camelName}Routes from './routes/${kebabName}/index';`,
187+
});
188+
189+
// Add registration
190+
actions.push({
191+
type: 'modify',
192+
path: 'src/app.ts',
193+
pattern: /(\/\/ Todo routes \(demonstrates Golden Orchestrator pattern\)\s+await fastify\.register\(todoRoutes, { prefix: '\/todos' }\);)/g,
194+
template: `$1\n\n // ${serviceName} routes (demonstrates Golden Orchestrator pattern)\n await fastify.register(${camelName}Routes, { prefix: '/${kebabName}' });`,
195+
});
196+
}
197+
198+
// 9. Success message
199+
actions.push(() => {
200+
const kebabName = serviceName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
201+
181202
console.log('\n✨ Service created successfully!\n');
182203
console.log(`📁 Service: src/services/${kebabName}/`);
183204
if (data.includeRoute) {
184205
console.log(`🛣️ Route: src/routes/${kebabName}/`);
206+
console.log(`✅ Auto-registered in src/app.ts`);
185207
}
186208
if (data.includeTests) {
187209
console.log(`🧪 Tests: src/services/${kebabName}/__tests__/`);
188210
}
189211

190212
console.log('\n📝 Next steps:\n');
191-
console.log('1️⃣ Register route in src/app.ts:');
192-
console.log(` \x1b[36mimport ${camelName}Routes from './routes/${kebabName}/index';\x1b[0m`);
193-
console.log(` \x1b[36mawait fastify.register(${camelName}Routes, { prefix: '/${kebabName}s' });\x1b[0m\n`);
194-
195-
console.log('2️⃣ Implement business logic in operations/\n');
213+
console.log('1️⃣ Implement business logic in operations/\n');
196214

197215
if (data.includePrisma) {
198-
console.log('3️⃣ Run database migration:');
216+
console.log('2️⃣ Run database migration:');
199217
console.log(` \x1b[36mnpm run db:migrate\x1b[0m\n`);
200218
}
201219

202-
console.log('4️⃣ Restart dev server (or it auto-restarts)\n');
220+
console.log('3️⃣ Restart dev server (or it auto-restarts)\n');
203221

204-
console.log('5️⃣ View in Swagger:');
205-
console.log(` \x1b[36mhttp://localhost:8080/documentation\x1b[0m`);
222+
console.log('4️⃣ View in Swagger:');
223+
console.log(` \x1b[36mhttp://localhost:3000/documentation\x1b[0m`);
206224
console.log(` \x1b[90m(Tags auto-detected - no manual config needed!)\x1b[0m\n`);
207225

208226
return 'Generator completed!';

0 commit comments

Comments
 (0)