-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-files.ts
483 lines (391 loc) · 18.8 KB
/
generate-files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import fs from 'fs-extra';
import path from 'path';
import inquirer from 'inquirer';
const paths = {
model: 'src/models',
controller: 'src/controllers',
service: 'src/services',
routes: 'src/routes',
validators: 'src/validators',
swaggerPath: 'src/config/swagger/paths',
swaggerDefPath: 'src/config/swagger/definitions',
};
const schemaPath = path.join(process.cwd(), 'prisma/schema.prisma');
const schemaContent = fs.readFileSync(schemaPath, 'utf-8');
function getTypeValidation(type: string): string {
switch (type.replace(/[?]/g, '')) {
case 'String':
return `.isString().withMessage(i18n.__('validator.MUST_BE_A_STRING'))`;
case 'Int':
return `.toInt().isInt().withMessage(i18n.__('validator.MUST_BE_A_VALID_INTEGER'))`;
case 'Float':
return `.isFloat().withMessage(i18n.__('validator.MUST_BE_A_VALID_FLOAT'))`;
case 'Boolean':
return `.isBoolean().withMessage(i18n.__('validator.MUST_BE_A_BOOLEAN'))`;
default:
return '';
}
}
function extractModelDefinition(modelName: string): { fields: string[]; types: string[]; optionalFields: Set<string> } {
const modelRegex = new RegExp(`model ${modelName} {([\\s\\S]*?)}`, 'm');
const match = schemaContent.match(modelRegex);
if (!match) throw new Error(`Model ${modelName} not found in schema.prisma`);
const fieldLines = match[1].trim().split('\n');
const fields: string[] = [];
const types: string[] = [];
const optionalFields: Set<string> = new Set();
for (const line of fieldLines) {
const [field, type, ...rest] = line.trim().split(/\s+/);
const isOptional = type.endsWith('?') || field === 'id' || field === 'deletedAt';
const isArray = type.endsWith('[]');
const baseType = type.replace(/[?\[\]]/g, ''); // Remove `?` and `[]`
fields.push(field);
types.push(`${baseType}${isArray ? '[]' : ''}`);
if (isOptional) {
optionalFields.add(field);
}
}
return { fields, types, optionalFields };
}
function extractValidationRules(modelName: string) {
const modelRegex = new RegExp(`model ${modelName} {([\\s\\S]*?)}`, 'm');
const match = schemaContent.match(modelRegex);
if (!match) {
throw new Error(`Model ${modelName} not found in schema.prisma`);
}
const fieldLines = match[1].trim().split('\n');
const validationRules: string[] = [];
const updateRules: string[] = [];
for (const line of fieldLines) {
const [field, type, ...rest] = line.trim().split(/\s+/);
// Skip system fields
if (['id', 'createdAt', 'updatedAt', 'deletedAt'].includes(field)) {
continue;
}
const isUnique = rest.includes('@unique');
const isRequired = !rest.includes('?');
const maxLength = type === 'String' ? 190 : null;
const typeValidation = getTypeValidation(type);
// Create rules
const createRule = [
`body('${field}')`,
isRequired ? `.notEmpty().withMessage(i18n.__('validator.${modelName.toUpperCase()}_${field.toUpperCase()}_IS_REQUIRED'))` : `.optional()`,
typeValidation,
maxLength ? `.isLength({ max: ${maxLength} }).withMessage(i18n.__('validator.${modelName.toUpperCase()}_${field.toUpperCase()}_MUST_BE_LESS_THAN_${maxLength + 1}_CHARACTERS'))` : '',
isUnique
? `.custom(async (${field}) => {
const ${modelName.toLowerCase()} = await prisma.${modelName.toLowerCase()}.findUnique({
where: { ${field} },
});
if (${modelName.toLowerCase()}) {
throw new Error(i18n.__('validator.${modelName.toUpperCase()}_${field.toUpperCase()}_MUST_BE_UNIQUE'));
}
return true;
})`
: '',
].filter(Boolean);
validationRules.push(createRule.join('\n'));
// Update rules
const updateRule = [
`body('${field}')`,
`.optional()`,
typeValidation,
maxLength ? `.isLength({ max: ${maxLength} }).withMessage(i18n.__('validator.${modelName.toUpperCase()}_${field.toUpperCase()}_MUST_BE_LESS_THAN_${maxLength + 1}_CHARACTERS'))` : '',
isUnique
? `.custom(async (${field} ,{ req }) => {
const { id } : any = req.params;
const ${modelName.toLowerCase()} = await prisma.${modelName.toLowerCase()}.findUnique({
where: { ${field} },
});
if (${modelName.toLowerCase()} && ${modelName.toLowerCase()}.id !== Number(id)) {
throw new Error(i18n.__('validator.${modelName.toUpperCase()}_${field.toUpperCase()}_MUST_BE_UNIQUE'));
}
return true;
})`
: '',
].filter(Boolean);
updateRules.push(updateRule.join('\n'));
}
return { validationRules, updateRules };
}
function generateModel(modelName: string, fields: string[], types: string[], optionalFields: Set<string>) {
const fieldsStr = fields
.map((field, idx) => {
const tsType = mapPrismaTypeToTS(types[idx]);
const optional = optionalFields.has(field) ? '?' : '';
return `${field}${optional}: ${tsType};`;
})
.join('\n ');
const content = `export interface ${modelName} {
${fieldsStr}
}
`;
const outputPath = path.join('src', 'models', `${modelName}.ts`);
fs.outputFileSync(outputPath, content);
console.log(`Model file for ${modelName} created at ${outputPath}`);
}
function mapPrismaTypeToTS(prismaType: string): string {
const map: Record<string, string> = {
String: 'string',
Int: 'number',
Float: 'number',
Boolean: 'boolean',
DateTime: 'Date',
Decimal: 'number', // Prisma Decimal is typically mapped to 'number'
BigInt: 'bigint', // Prisma BigInt is mapped to 'bigint'
Json: 'any', // Json is typically mapped to 'any'
Bytes: 'Buffer', // Prisma Bytes is mapped to Node.js Buffer
};
const isArray = prismaType.endsWith('[]');
const isOptional = prismaType.includes('| undefined');
const baseType = prismaType.replace('[]', '').replace('| undefined', '');
// Look up the Prisma type in the map, or fallback to the base type if not found.
const tsType = map[baseType] || baseType;
return `${tsType}${isArray ? '[]' : ''}${isOptional ? ' | undefined' : ''}`;
}
function generateSwagger(modelName: string): any {
const { fields, types } = extractModelDefinition(modelName);
// Define the basic structure for Swagger definitions
const swaggerDefinition: any = {
[modelName]: {
type: 'object',
required: [],
properties: {},
},
};
// Loop through the fields and types to build the Swagger definition
fields.forEach((field, idx) => {
if (field === 'id' || field === 'createdAt' || field === 'updatedAt' || field === 'deletedAt') {
return; // Skip these fields for create method
}
// Mark fields like 'name' as required
const isRequired = !types[idx].includes('null') && !types[idx].includes('undefined');
// Map the field to Swagger definition
swaggerDefinition[modelName].properties[field] = {
type: mapPrismaTypeToSwagger(types[idx]),
};
if (isRequired) {
swaggerDefinition[modelName].required.push(field);
}
});
return swaggerDefinition;
}
function mapPrismaTypeToSwagger(prismaType: string): string {
const map: Record<string, string> = {
String: 'string',
Int: 'integer',
Float: 'number',
Boolean: 'boolean',
DateTime: 'string', // Swagger uses string for Date
};
const isArray = prismaType.endsWith('[]');
const baseType = prismaType.replace('[]', '');
const swaggerType = map[baseType] || 'string'; // Default to string if no mapping found
return isArray ? `array[${swaggerType}]` : swaggerType;
}
function generateController(modelName: string){
const templatePath = path.join('src', 'config', 'templates', 'controller.ts');
let content = fs.readFileSync(templatePath, 'utf-8');
const modelNameLowerCase = modelName.charAt(0).toLowerCase() + modelName.slice(1);
content = content.replace(/\${modelName}/g, modelNameLowerCase);
content = content.replace(/\${ModelName}/g, modelName);
fs.outputFileSync(path.join(paths.controller, `${modelNameLowerCase}Controller.ts`), content);
console.log(`Controller file for ${modelName} created at ${paths.controller}/${modelNameLowerCase}Controller.ts`);
}
function generateService(modelName: string){
const templatePath = path.join('src', 'config', 'templates', 'service.ts');
let content = fs.readFileSync(templatePath, 'utf-8');
const modelNameLowerCase = modelName.charAt(0).toLowerCase() + modelName.slice(1);
content = content.replace(/\${modelName}/g, modelNameLowerCase);
content = content.replace(/\${ModelName}/g, modelName);
fs.outputFileSync(path.join(paths.service, `${modelNameLowerCase}Service.ts`), content);
console.log(`Service file for ${modelName} created at ${paths.service}/${modelNameLowerCase}Service.ts`);
}
function generateRoutes(modelName: string,permission: string){
const templatePath = path.join('src', 'config', 'templates', 'routes.ts');
let content = fs.readFileSync(templatePath, 'utf-8');
const modelNameLowerCase = modelName.charAt(0).toLowerCase() + modelName.slice(1);
content = content.replace(/\${modelName}/g, modelNameLowerCase);
content = content.replace(/\${ModelName}/g, modelName);
content = content.replace(/\${permission}/g, permission);
fs.outputFileSync(path.join(paths.routes, `${modelNameLowerCase}Routes.ts`), content);
console.log(`Routes file for ${modelName} created at ${paths.routes}/${modelNameLowerCase}Routes.ts`);
}
function generateValidationFile(modelName: string) {
const { validationRules, updateRules } = extractValidationRules(modelName);
const templatePath = path.join('src', 'config', 'templates', 'validator.ts');
let content = fs.readFileSync(templatePath, 'utf-8');
const modelNameLowerCase = modelName.charAt(0).toLowerCase() + modelName.slice(1);
content = content.replace(/\${modelName}/g, modelNameLowerCase);
content = content.replace(/\${ModelName}/g, modelName);
content = content.replace(/\${createValidation}/g, validationRules.join(',\n '));
content = content.replace(/\${updateValidation}/g, updateRules.join(',\n '));
fs.outputFileSync(path.join(paths.validators, `${modelNameLowerCase}Validator.ts`), content);
console.log(`Validator file for ${modelName} created at ${paths.validators}/${modelNameLowerCase}Validator.ts`);
}
function addImportsToInversifyConfig(modelName: string) {
const inversifyConfigPath = path.join('src', 'config', 'inversifyConfig.ts');
let content = fs.readFileSync(inversifyConfigPath, 'utf-8');
const modelNameLowerCase = modelName.charAt(0).toLowerCase() + modelName.slice(1);
const newImports = `
import { ${modelName}Controller } from '../controllers/${modelNameLowerCase}Controller';
import { ${modelName}Service } from '../services/${modelNameLowerCase}Service';
`;
const newBinds = `
container.bind<${modelName}Controller>(${modelName}Controller).toSelf();
container.bind<${modelName}Service>(${modelName}Service).toSelf();
`;
const importsExist = content.includes(`${modelName}Controller`) && content.includes(`${modelName}Service`);
if (importsExist) {
console.log(`${modelName}Controller and ${modelName}Service imports are already present.`);
return;
}
// Find the position of the line `const container = new Container();`
const containerLineIndex = content.indexOf('const container = new Container();');
if (containerLineIndex !== -1) {
// Insert the new imports just above this line and add an empty line above and below the inserted imports
content = content.slice(0, containerLineIndex) + newImports + content.slice(containerLineIndex);
}
// Find the position of the line `export default container;`
const containerBindIndex = content.indexOf('export default container;');
if (containerBindIndex !== -1) {
// Insert the new Binds just above this line
content = content.slice(0, containerBindIndex) + newBinds + content.slice(containerBindIndex);
}
fs.outputFileSync(inversifyConfigPath, content);
if (!importsExist) {
console.log(`Added ${modelName}Controller and ${modelName}Service imports to inversifyConfig.ts`);
}
}
function addImportsToV1Routes(modelName: string) {
const v1RoutesPath = path.join('src', 'routes', 'v1.ts');
let content = fs.readFileSync(v1RoutesPath, 'utf-8');
const modelNameLowerCase = modelName.charAt(0).toLowerCase() + modelName.slice(1);
const newImports = `import ${modelName}Routes from './${modelNameLowerCase}Routes';
`;
const newBinds = `router.use('/api/v1/${modelNameLowerCase}', authMiddleware, ${modelName}Routes);
`;
const importsExist = content.includes(`${modelNameLowerCase}Routes`);
if (importsExist) {
console.log(`${modelName}Routes already present.`);
return;
}
// Find the position of the line `const router = Router();`
const containerLineIndex = content.indexOf('const router = Router();');
if (containerLineIndex !== -1) {
// Insert the new imports just above this line and add an empty line above and below the inserted imports
content = content.slice(0, containerLineIndex) + newImports + content.slice(containerLineIndex);
}
// Find the position of the line `export default container;`
const containerBindIndex = content.indexOf('export default router;');
if (containerBindIndex !== -1) {
// Insert the new Binds just above this line
content = content.slice(0, containerBindIndex) + newBinds + content.slice(containerBindIndex);
}
fs.outputFileSync(v1RoutesPath, content);
if (!importsExist) {
console.log(`Added ${modelName}Routes to V1Routes`);
}
}
function generateSwaggerPath(modelName: string){
const templatePath = path.join('src', 'config', 'templates', 'swaggerPaths.ts');
let content = fs.readFileSync(templatePath, 'utf-8');
const modelNameLowerCase = modelName.charAt(0).toLowerCase() + modelName.slice(1);
content = content.replace(/\${modelName}/g, modelNameLowerCase);
content = content.replace(/\${ModelName}/g, modelName);
fs.outputFileSync(path.join(paths.swaggerPath, `${modelNameLowerCase}Paths.ts`), content);
console.log(`Swagger Path file for ${modelName} created at ${paths.swaggerPath}/${modelNameLowerCase}Paths.ts`);
const templateDefPath = path.join('src', 'config', 'templates', 'swaggerDefinition.ts');
let contentDef = fs.readFileSync(templateDefPath, 'utf-8');
const swaggerDef = generateSwagger(modelName);
contentDef = contentDef.replace(/\${modelName}/g, modelNameLowerCase);
contentDef = contentDef.replace(/\${ModelName}/g, modelName);
contentDef = contentDef.replace(/\${swaggerDef}/g, JSON.stringify(swaggerDef, null, 2));
fs.outputFileSync(path.join(paths.swaggerDefPath, `${modelNameLowerCase}Definition.ts`), contentDef);
console.log(`Swagger Definition file for ${modelName} created at ${paths.swaggerDefPath}/${modelNameLowerCase}Definitions.ts`);
}
function addImportsToSwaggerConfig(modelName: string) {
const v1RoutesPath = path.join('src', 'config','swagger' ,'swaggerConfig.ts');
let content = fs.readFileSync(v1RoutesPath, 'utf-8');
const modelNameLowerCase = modelName.charAt(0).toLowerCase() + modelName.slice(1);
const pathImports = `import ${modelNameLowerCase}Paths from './paths/${modelNameLowerCase}Paths';
`;
const defImports = `import ${modelNameLowerCase}Definitions from './definitions/${modelNameLowerCase}Definition';
`;
const pathInclude = `...${modelNameLowerCase}Paths,
`;
const defInclude = `...${modelNameLowerCase}Definitions,
`;
const importsExist = content.includes(`${modelNameLowerCase}Paths`);
if (importsExist) {
console.log(`${modelName}Paths & Definition already present.`);
return;
}
// Find the position of the line `const router = Router();`
const containerPathIndex = content.indexOf('// Path Imports ends');
if (containerPathIndex !== -1) {
// Insert the new imports just above this line and add an empty line above and below the inserted imports
content = content.slice(0, containerPathIndex) + pathImports + content.slice(containerPathIndex);
}
// Find the position of the line `export default container;`
const containerDefIndex = content.indexOf('// Definition Imports ends');
if (containerDefIndex !== -1) {
// Insert the new Binds just above this line
content = content.slice(0, containerDefIndex) + defImports + content.slice(containerDefIndex);
}
// Find the position of the line `export default container;`
const containerPathInclude = content.indexOf('// register new paths here');
if (containerPathInclude !== -1) {
// Insert the new Binds just above this line
content = content.slice(0, containerPathInclude) + pathInclude + content.slice(containerPathInclude);
}
// Find the position of the line `export default container;`
const containerDefInclude = content.indexOf('// register new defintions here');
if (containerDefIndex !== -1) {
// Insert the new Binds just above this line
content = content.slice(0, containerDefInclude) + defInclude + content.slice(containerDefInclude);
}
fs.outputFileSync(v1RoutesPath, content);
if (!importsExist) {
console.log(`Added ${modelName}Paths and Definitions to SwaggerConfig`);
}
}
async function promptInputs() {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'modelName',
message: 'Enter the model name:',
validate: (input) => (input ? true : 'Model name is required'),
},
{
type: 'input',
name: 'permissionName',
message: 'Enter the permission name:',
validate: (input) => (input ? true : 'Permission name is required'),
},
]);
return answers;
}
(async function () {
try {
const { modelName, permissionName } = await promptInputs();
const { fields, types, optionalFields } = extractModelDefinition(modelName);
generateModel(modelName, fields, types , optionalFields);
generateController(modelName);
generateService(modelName);
generateRoutes(modelName,permissionName);
generateValidationFile(modelName);
addImportsToInversifyConfig(modelName);
addImportsToV1Routes(modelName);
generateSwaggerPath(modelName);
addImportsToSwaggerConfig(modelName);
console.log(`Files for ${modelName} created successfully!`);
} catch (err) {
if (err instanceof Error) {
console.error(err.message);
} else {
console.error('An unknown error occurred:', err);
}
}
})();