-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
269 lines (232 loc) · 8.4 KB
/
main.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
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
#!/usr/bin/env node
const fs = require('mz/fs');
const inquirer = require('inquirer');
const getObjectName = [
{
type: "input",
name: "objectName",
message: "\n Enter the name of the object/class you want to generate (case sensitive): "
}
];
const templateChoices = ["Controller, Module, and Service", "Controller", "Service", "Module", ]
const getTemplateInput = [
{
type: "list",
name: "templateName",
message: `\n What template would you like to generate? `,
choices: templateChoices
}
];
var overWriteAll = false
async function main() {
const getObjectNameResult = (await inquirer.prompt(getObjectName));
const name = getObjectNameResult.objectName
console.log(`${getObjectNameResult.objectName}`)
const getTemplateInputResult = (await inquirer.prompt(getTemplateInput));
const templateChoice = getTemplateInputResult.templateName;
const firstLetterUpper = name.charAt(0).toUpperCase()
const firstLetterLower = name.charAt(0).toLowerCase()
const withoutFirstLetter = name.substr(1)
const cammelUpper = firstLetterUpper + withoutFirstLetter
const cammelLower = firstLetterLower + withoutFirstLetter
const dir = process.cwd() + `/src/${name}`
const overWrtieChoices = ["Abort", "Overwrite All"]
const overWritePrompt = [
{
type: "list",
name: "overWrite",
message: `\n A file currently exist at ${dir}, continuing will overwrite existing files \n Are you sure you want to overwrite?`,
choices: overWrtieChoices
}
];
const finishPrompt = [
{
type: "message",
name: "finishMsg",
message: `==================================================================================================== \n Done! Please remeber to import your new ${cammelUpper}Module into your app.module.ts in the 'imports' array so \n nestJS can know about it! [Press return or enter to exit] \n ====================================================================================================`,
}
];
const abortPrompt = [
{
type: "message",
name: "abortMsg",
message: `\n ==================================================================================================== \n Aborted! That was a close one! Phew! [Press return or enter to exit] \n ====================================================================================================`,
}
];
if (fs.existsSync(dir) == false){
fs.mkdirSync(dir, {recursive: true});
} else {
const getOverWritePremission = (await inquirer.prompt(overWritePrompt));
if (getOverWritePremission.overWrite == overWrtieChoices[1]) {
overWriteAll = true
} else {
await (await inquirer.prompt(abortPrompt));
process.exit()
}
}
if (templateChoice == templateChoices[0]) {
await writeController(name, cammelUpper, cammelLower)
await writeService(name, cammelUpper, cammelLower)
await writeModule(name, cammelUpper, cammelLower)
await writeServiceTest(name, cammelUpper, cammelLower)
await writeControllerTest(name, cammelUpper, cammelLower)
await (await inquirer.prompt(finishPrompt));
process.exit()
} else if (templateChoice == templateChoices[1]){
await writeController(name, cammelUpper, cammelLower)
await writeControllerTest(name, cammelUpper, cammelLower)
await (await inquirer.prompt(finishPrompt));
process.exit()
} else if (templateChoice == templateChoices[2]){
await writeServiceTest(name, cammelUpper, cammelLower)
await writeService(name, cammelUpper, cammelLower)
await (await inquirer.prompt(finishPrompt));
process.exit()
} else if (templateChoice == templateChoices[2]){
await writeModule(name, cammelUpper, cammelLower)
await (await inquirer.prompt(finishPrompt));
process.exit()
}
}
try {
main();
} catch (error) {
console.log(error)
}
async function writeController(name, cammelUpper, cammelLower) {
const content =
`import {
Controller,
Get,
Param,
Post,
Body,
Put,
Delete,
} from '@nestjs/common';
import { ${cammelUpper}Service } from './${name}.service';
import { ${cammelUpper} as ${name}Model } from '@prisma/client';
@Controller('${cammelLower}')
export class ${cammelUpper}Controller {
constructor(private readonly ${cammelLower}Service: ${name}Service) {}
@Get('${name}/:id')
async get${cammelUpper}ByID(@Param('id') id: string): Promise<${name}Model> {
return this.${cammelLower}Service.${name}({ id: Number(id) });
}
@Post()
async create${cammelUpper}(@Body() ${cammelLower}Data): Promise<${name}Model> {
return this.${cammelLower}Service.create${cammelUpper}( ${cammelLower}Data);
}
@Delete('${name}/:id')
async delete${cammelUpper}ByID(@Param('id') id: string): Promise<${name}Model> {
rreturn this.${cammelLower}Service.delete({ id: Number(id) });
}
}
`
await fs.writeFile(process.cwd() + `/src/${name}/${cammelLower}.controller.ts`,content)
}
async function writeService(name, cammelUpper, cammelLower) {
const content =
`import { Injectable } from '@nestjs/common';
import { PrismaService } from './prisma.service';
import { ${cammelUpper}, Prisma } from '@prisma/client';
@Injectable()
export class ${cammelUpper}Service {
constructor(private prisma: PrismaService) {}
async ${name}(${cammelLower}WhereUniqueInput: Prisma.${cammelUpper}WhereUniqueInput): Promise<${name} | null> {
return this.prisma.${name}.findUnique({
where: ${cammelLower}WhereUniqueInput,
});
}
async ${name}s(params: {
skip?: number;
take?: number;
cursor?: Prisma.${cammelUpper}WhereUniqueInput;
where?: Prisma.${cammelUpper}WhereInput;
orderBy?: Prisma.${name}OrderByInput;
}): Promise<${name}[]> {
const { skip, take, cursor, where, orderBy } = params;
return this.prisma.${name}.findMany({
skip,
take,
cursor,
where,
orderBy,
});
}
async create${cammelUpper}(data: Prisma.${name}CreateInput): Promise<${name}> {
return this.prisma.${cammelLower}.create({
data,
});
}
async update${cammelUpper}(params: {
where: Prisma.${cammelUpper}WhereUniqueInput;
data: Prisma.${cammelUpper}UpdateInput;
}): Promise<${name}> {
const { where, data } = params;
return this.prisma.${cammelLower}.update({
data,
where,
});
}
async delete${cammelUpper}(where: Prisma.${cammelUpper}WhereUniqueInput): Promise<${name}> {
return this.prisma.${cammelLower}.delete({
where,
});
}
}
`
await fs.writeFile(process.cwd() + `/src/${name}/${cammelLower}.service.ts`,content)
}
async function writeModule(name, cammelUpper, cammelLower) {
const content =
`import { Module } from '@nestjs/common';
import { ${cammelUpper}Controller } from './${cammelLower}.controller';
import { ${cammelUpper}Service } from './${cammelLower}.service';
@Module({
imports: [],
controllers: [${cammelUpper}Controller],
providers: [${cammelUpper}Service],
})
export class ${cammelUpper}Module {}
`
await fs.writeFile(process.cwd() + `/src/${name}/${cammelLower}.module.ts`, content)
}
async function writeControllerTest(name, cammelUpper, cammelLower) {
content =
`import { Test, TestingModule } from '@nestjs/testing';
import { ${cammelUpper}Controller } from './${cammelLower}.controller';
describe('${cammelUpper}Controller', () => {
let controller: ${cammelUpper}Controller;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [${cammelUpper}Controller],
}).compile();
controller = module.get<${cammelUpper}Controller>(${cammelUpper}Controller);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
`
await fs.writeFile(process.cwd() + `/src/${name}/${cammelLower}.controller.spec.ts`, content)
}
async function writeServiceTest(name, cammelUpper, cammelLower) {
const content =
`import { Test, TestingModule } from '@nestjs/testing';
import { ${cammelUpper}Service } from './${cammelLower}.service';
describe('${cammelUpper}Service', () => {
let service: ${cammelUpper}Service;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [${cammelUpper}Service],
}).compile();
service = module.get<${cammelUpper}Service>(${cammelUpper}Service);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
`
await fs.writeFile(process.cwd() + `/src/${name}/${cammelLower}.service.spec.ts`, content)
}