-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateFiles.js
147 lines (124 loc) · 3.55 KB
/
generateFiles.js
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
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-console */
const fs = require('fs').promises;
const path = require('path');
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1);
const files = [
{
name: 'controller.ts',
getCode: folderName =>
`
import { Request, Response } from "express";
import catchAsync from "../../../shared/catchAsync";
import sendResponse from "../../../shared/sendResponse";
import httpStatus from "http-status";
const insertDB = catchAsync(async (req: Request, res: Response) => {
const data = req.body;
const result = await ${capitalize(folderName)}Service.insertDB(data)
sendResponse<${capitalize(folderName)}>(res, {
statusCode: httpStatus.CREATED,
success: true,
message: 'Successfully ${capitalize(folderName)}',
data: result,
});
});
export const ${capitalize(folderName)}Controller = {insertDB};
`,
},
{
name: 'service.ts',
getCode: folderName =>
`
import { Prisma,${capitalize(folderName)} } from '@prisma/client';
import prisma from '../../../shared/prisma';
const insertDB = async (data: ${capitalize(folderName)}): Promise<${capitalize(
folderName
)}> => {
const result = await prisma.${folderName}.create({
data,
});
return result;
};
export const ${capitalize(folderName)}Service = {insertDB};
`,
},
{
name: 'validation.ts',
getCode: folderName =>
`
import { z } from 'zod';
const create${capitalize(folderName)} = z.object({
body: z.object({
year: z.number({
required_error: 'year is Required (zod)',
}),
title: z.string({
required_error: 'title is Required (zod)',
})
}),
});
export const ${capitalize(folderName)}Validation = { create${capitalize(
folderName
)} };
`,
},
{
name: 'route.ts',
getCode: folderName =>
`/* eslint-disable no-unused-vars */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Router } from 'express';
import { ${capitalize(
folderName
)}Controller } from './${folderName}.controller';
import {${capitalize(folderName)}Validation } from './${folderName}.validation';
const router = Router();
router.get('/')
router.post('/')
export const ${capitalize(folderName)}Routes = router;
`,
},
];
async function createFolderAndFiles(parentDirectory, folderName) {
try {
const moduleDirectory = path.join(parentDirectory, folderName);
// Create the folder
await fs.mkdir(moduleDirectory);
// Create the files using for...of loop and async/await
for (const file of files) {
const filePath = path.join(moduleDirectory, `${folderName}.${file.name}`);
await fs.writeFile(filePath, file.getCode(folderName));
console.log(`Created ${filePath}`);
}
console.log('Module and files created successfully.');
} catch (error) {
console.error('Error:', error);
}
}
async function getUserInput() {
return new Promise(resolve => {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
readline.question(
'Enter the Module name (or "exit" to terminate): ',
folderName => {
readline.close();
resolve(folderName);
}
);
});
}
async function start() {
const parentDirectory = 'src/app/modules';
// eslint-disable-next-line no-constant-condition
while (true) {
const folderName = await getUserInput();
if (folderName.toLowerCase() === 'exit') {
process.exit(0);
}
await createFolderAndFiles(parentDirectory, folderName);
}
}
start();