-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplopfile.ts
265 lines (256 loc) · 8.72 KB
/
plopfile.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
import fs from 'fs';
import { join } from 'path';
import { ActionType, NodePlopAPI } from 'plop';
export default function (plop: NodePlopAPI) {
const servers = fs.readdirSync(join(__dirname, './src/servers'));
plop.load('plop-pack-json-modify');
plop.setGenerator('Generator', {
description: 'Generates a module based on user input',
prompts: [
{
type: 'list',
name: 'type',
message: 'What would you like to create?',
choices: ['module', 'server'],
},
{
type: 'input',
name: 'server',
message: 'What should we call the server?',
when(answers) {
return answers.type === 'server';
},
},
{
type: 'input',
name: 'port',
message: 'Which port should the server be running on?',
when(answers) {
return answers.type === 'server';
},
},
{
type: 'list',
choices: servers,
name: 'server',
message: 'Which server should the module be created in?',
when(answers) {
return answers.type === 'module';
},
},
{
type: 'input',
name: 'name',
message: 'What module should we create',
validate: (answer) => {
return Boolean(answer);
},
when(answers) {
return answers.type === 'module';
},
},
{
type: 'confirm',
name: 'repository',
message: 'Do you want to create a Repository?',
default: false,
when(answers) {
return answers.type === 'module';
},
},
],
actions: (data) => {
const items: ActionType[] = [];
if (data?.type === 'server') {
createServer(data, items);
} else if (data?.type === 'module') {
createModule(data, items);
}
return items;
},
});
}
const createServer = (data: any, items: ActionType[]) => {
const port = Number(data?.port);
items.push(
{
type: 'add',
path: 'src/servers/{{kebabCase server}}/{{kebabCase server}}.server.ts',
templateFile: 'templates/servers/server.ts.hbs',
},
{
type: 'add',
path: 'src/servers/{{kebabCase server}}/{{kebabCase server}}.controllers.ts',
templateFile: 'templates/servers/controllers.ts.hbs',
},
{
type: 'add',
path: 'src/servers/{{kebabCase server}}/{{kebabCase server}}.services.ts',
templateFile: 'templates/servers/services.ts.hbs',
},
{
type: 'add',
path: 'src/servers/{{kebabCase server}}/healthcheck/healthcheck.controller.ts',
templateFile: 'templates/servers/healthcheck/healthcheck.controller.ts.hbs',
},
{
// Modify .env file
type: 'append',
path: '.env',
template: `{{constantCase server}}_PORT=${port}`,
},
{
// Modify config.provider file
type: 'modify',
path: 'src/providers/config.provider.ts',
pattern: 'NODE_ENV: process.env.NODE_ENV,',
template: `NODE_ENV: process.env.NODE_ENV,\n {{constantCase server}}_PORT: Number(process.env.{{constantCase server}}_PORT),`,
},
{
// Add
type: 'json-modify-file' as any, // Point to this action
force: false, // Overrides, create non existing
JSONFile: './package.json' as any, // File to modify
JSONKey: 'scripts', // Property to append to
JSONEntryKey: '{{kebabCase server}}', // Property to add
JSONEntryValue: `node -r module-alias/register dist/servers/{{kebabCase server}}/{{kebabCase server}}.server.js`,
} as any,
{
// Add
type: 'json-modify-file' as any, // Point to this action
force: false, // Overrides, create non existing
JSONFile: './package.json' as any, // File to modify
JSONKey: 'scripts', // Property to append to
JSONEntryKey: '{{kebabCase server}}:dev', // Property to add
JSONEntryValue: `NODE_ENV=development tsc-watch --onSuccess "yarn {{kebabCase server}}"`,
} as any,
{
// Add
type: 'json-modify-file' as any, // Point to this action
force: false, // Overrides, create non existing
JSONFile: './package.json' as any, // File to modify
JSONKey: 'scripts', // Property to append to
JSONEntryKey: '{{kebabCase server}}:staging', // Property to add
JSONEntryValue: `NODE_ENV=staging yarn {{kebabCase server}}`,
} as any,
{
// Add
type: 'json-modify-file' as any, // Point to this action
force: false, // Overrides, create non existing
JSONFile: './package.json' as any, // File to modify
JSONKey: 'scripts', // Property to append to
JSONEntryKey: '{{kebabCase server}}:production', // Property to add
JSONEntryValue: `NODE_ENV=production yarn {{kebabCase server}}`,
} as any
);
// Docs
items.push(
{
type: 'add',
path: 'docs/{{kebabCase server}}/index.html',
templateFile: 'templates/docs/index.html.hbs',
},
{
type: 'add',
path: 'docs/{{kebabCase server}}/swagger.yml',
templateFile: 'templates/docs/swagger.yml.hbs',
}
);
};
const createModule = (data: any, items: ActionType[]) => {
items.push(
{
type: 'add',
path: 'src/servers/{{kebabCase server}}/{{kebabCase name}}/{{kebabCase name}}.middleware.ts',
templateFile: 'templates/modules/module.middleware.ts.hbs',
},
{
type: 'add',
path: 'src/servers/{{kebabCase server}}/{{kebabCase name}}/{{kebabCase name}}.dto.ts',
templateFile: 'templates/modules/module.dto.ts.hbs',
},
{
type: 'add',
path: 'src/servers/{{kebabCase server}}/{{kebabCase name}}/{{kebabCase name}}.response.ts',
templateFile: 'templates/modules/module.response.ts.hbs',
},
{
type: 'add',
path: 'src/servers/{{kebabCase server}}/{{kebabCase name}}/{{kebabCase name}}.exception.ts',
templateFile: 'templates/modules/module.exception.ts.hbs',
},
{
type: 'modify',
pattern: ".controller';",
template: `.controller';\nimport { {{pascalCase name}}Controller } from '@servers/{{kebabCase server}}/{{kebabCase name}}/{{kebabCase name}}.controller';`,
path: 'src/servers/{{kebabCase server}}/{{kebabCase server}}.controllers.ts',
},
{
type: 'modify',
pattern: 'export const controllers = [',
template: `export const controllers = [\n {{pascalCase name}}Controller,`,
path: 'src/servers/{{kebabCase server}}/{{kebabCase server}}.controllers.ts',
},
{
type: 'modify',
pattern: 'export const services = {',
template: `import { {{pascalCase name}}Service } from '@servers/{{kebabCase server}}/{{kebabCase name}}/{{kebabCase name}}.service';\nexport const services = {`,
path: 'src/servers/{{kebabCase server}}/{{kebabCase server}}.services.ts',
},
{
type: 'modify',
pattern: 'export const services = {',
template: `export const services = { {{camelCase name}}: new {{pascalCase name}}Service(),\n`,
path: 'src/servers/{{kebabCase server}}/{{kebabCase server}}.services.ts',
}
);
items.push({
type: 'add',
path: 'src/servers/{{kebabCase server}}/{{kebabCase name}}/{{kebabCase name}}.interface.ts',
templateFile: 'templates/modules/module.interface.ts.hbs',
});
if (data?.repository) {
items.push(
{
type: 'add',
path: 'src/servers/{{kebabCase server}}/{{kebabCase name}}/{{kebabCase name}}.controller.ts',
templateFile: 'templates/modules/module.controller.ts.hbs',
},
{
type: 'add',
path: 'src/servers/{{kebabCase server}}/{{kebabCase name}}/{{kebabCase name}}.service.ts',
templateFile: 'templates/modules/module.service.ts.hbs',
},
{
type: 'add',
path: 'src/repositories/{{kebabCase name}}.repository.ts',
templateFile: 'templates/repository.ts.hbs',
},
{
type: 'modify',
pattern: ".repository';",
template: `.repository';\nimport { {{pascalCase name}}Repository } from '@repositories/{{kebabCase name}}.repository';`,
path: 'src/repositories/index.repository.ts',
},
{
type: 'modify',
pattern: 'export const repositories = {',
template: `export const repositories = {\n {{camelCase name}}: new {{pascalCase name}}Repository(),`,
path: 'src/repositories/index.repository.ts',
}
);
} else {
items.push(
{
type: 'add',
path: 'src/servers/{{kebabCase server}}/{{kebabCase name}}/{{kebabCase name}}.controller.ts',
templateFile: 'templates/module/blank-controller.ts.hbs',
},
{
type: 'add',
path: 'src/servers/{{kebabCase server}}/{{kebabCase name}}/{{kebabCase name}}.service.ts',
templateFile: 'templates/module/blank-service.ts.hbs',
}
);
}
};