-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule-generator.js
122 lines (96 loc) · 3.26 KB
/
module-generator.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
/* 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) =>
`/* eslint-disable @typescript-eslint/no-unused-vars */
import { ${capitalize(folderName)}Service } from './${folderName}.service';
import { I${capitalize(folderName)} } from './${folderName}.interface';
export const ${capitalize(folderName)}Controller = {};
`,
},
{
name: 'interface.ts',
getCode: (folderName) =>
`/* eslint-disable @typescript-eslint/no-unused-vars */
import { Model, Types } from 'mongoose';
export type I${capitalize(folderName)} = {
}
export type I${capitalize(folderName)}Model = Model<I${capitalize(folderName)}, Record<string, unknown>>;`
},
{
name: 'model.ts',
getCode: (folderName) =>
`import { Schema, model } from 'mongoose';
import { I${capitalize(folderName)}, I${capitalize(folderName)}Model } from './${folderName}.interface';
const ${capitalize(folderName)}Schema = new Schema<I${capitalize(folderName)}, I${capitalize(folderName)}Model>(
{
}
);
export const ${capitalize(folderName)} = model<I${capitalize(folderName)}, I${capitalize(folderName)}Model>('${capitalize(folderName)}', ${capitalize(folderName)}Schema);
`
},
{
name: 'service.ts',
getCode: (folderName) =>
`/* eslint-disable @typescript-eslint/no-unused-vars */
import { I${capitalize(folderName)} } from './${folderName}.interface';
import { ${capitalize(folderName)} } from './${folderName}.model';
export const ${capitalize(folderName)}Service = {};
`
},
{
name: 'route.ts',
getCode: (folderName) =>
`/* eslint-disable @typescript-eslint/no-unused-vars */
import { Router } from 'express';
import { ${capitalize(folderName)}Controller } from './${folderName}.controller';
const router = Router();
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();