-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcreate-backend-project.js
More file actions
345 lines (296 loc) · 10.4 KB
/
create-backend-project.js
File metadata and controls
345 lines (296 loc) · 10.4 KB
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
import {
copyFile,
createAndUpdateFile,
createFolder,
getTemplateDir,
updateFileContent,
writeToFile,
} from "./file-manager.js";
import { AppModuleContent } from "../../templates/backend/nestjs/base/app-module.js";
import path from "path";
import {
MongodbDatabaseConfig,
MongodbSchema,
} from "../../templates/backend/nestjs/base/databases.js";
import {
NEST_MONGOOSE_PACKAGE,
NestjsPackageJsonTemplate,
} from "../../templates/backend/nestjs/base/nestjs-package-json.js";
import { ENVIRONMENT_TEMPLATE } from "../../templates/backend/nestjs/base/environment.js";
import { EXPRESSJS_SERVER_TEMPLATE } from "../../templates/backend/expressjs/base/server.js";
import {
ExpressJsMongodbMongooseConnectionTemplate,
ExpressJsMongoDbMongooseSampleSchema,
} from "../../templates/backend/expressjs/base/database.js";
import { ExpressJsPackageJsonTemplate } from "../../templates/backend/expressjs/base/package-json.js";
import { ExpressJsEnvironmentTemplate } from "../../templates/backend/expressjs/base/config.js";
import { DJANGO_MANAGER } from "../../templates/backend/django/base/manage.js";
import { DJANGO_WSGI } from "../../templates/backend/django/base/wsgi.js";
import { DJANGO_ASGI } from "../../templates/backend/django/base/asgi.js";
import { DJANGO_SETTINGS } from "../../templates/backend/django/base/settings.js";
import { DJANGO_ENV_VARIABLES } from "../../templates/backend/django/base/env.js";
import {
DJANGO_POSTGRES_SETUP,
DJANGO_SQLITE_SETUP,
} from "../../templates/backend/django/base/database.js";
import ora from "ora";
import shell from "shelljs";
import crypto from "crypto";
import { processDependenciesInstall } from "./helper.js";
import { sendStat } from "./stat.js";
/**
* function to create backend projects
*/
export async function createBackendProject(
projectName,
framework,
database,
orm,
installDependencies
) {
const spinner = ora('Creating Project ...').start();
try {
const destinationPath = path.join(
process.cwd(),
projectName ?? `project-starter-${framework}-template`,
);
if (framework === "nestjs") {
let appModules = "";
let appModuleImports = "";
let packageJson = NestjsPackageJsonTemplate;
let environmentInterface = "";
let environmentContent = "";
// copy nestjs template to directory
copyFile(getTemplateDir("backend/nestjs/nestjs-temp"), destinationPath);
// update app module file content
writeToFile(`${destinationPath}/src/app.module.ts`, AppModuleContent);
// add environment file
writeToFile(
`${destinationPath}/src/common/configs/environment.ts`,
ENVIRONMENT_TEMPLATE,
);
if (database) {
spinner.succeed();
spinner.start('Adding Database Module ...');
switch (database) {
case "mongodb":
switch (orm) {
case "mongoose":
// write db config
createAndUpdateFile(
`${destinationPath}/src/module/v1/database/database.module.ts`,
MongodbDatabaseConfig,
);
// create sample schema file for db
createAndUpdateFile(
`${destinationPath}/src/module/v1/database/sample.schema.ts`,
MongodbSchema,
);
// add mongoose dependencies
packageJson.dependencies = {
...packageJson.dependencies,
...NEST_MONGOOSE_PACKAGE.dependencies,
};
// update environment
environmentInterface += `\nDB: {
URL: string;}`;
environmentContent += `\n DB: {
URL: process.env.DB_URL,}`;
// update app module
appModules += "DatabaseModule";
appModuleImports +=
'import { DatabaseModule } from "./module/v1/database/database.module";';
break;
default:
packageJson.dependencies = {
...packageJson.dependencies,
...NestjsPackageJsonTemplate.dependencies,
};
break;
}
break;
}
}
// update app module
updateFileContent(
`${destinationPath}/src/app.module.ts`,
AppModuleContent,
{
new_modules_path: appModuleImports,
new_modules: appModules,
},
);
// update environment config file
updateFileContent(
`${destinationPath}/src/common/configs/environment.ts`,
ENVIRONMENT_TEMPLATE,
{
environment_interface: environmentInterface,
environment_content: environmentContent,
},
);
// update packageJsonFile
createAndUpdateFile(
`${destinationPath}/package.json`,
JSON.stringify(packageJson, null, " "),
);
} else if (framework === "expressjs") {
let database_config = "";
let database_config_import = "";
let additional_environment_variables = "";
let packageJson = ExpressJsPackageJsonTemplate;
// copy expressjs template to directory
copyFile(
getTemplateDir("backend/expressjs/expressjs-temp"),
destinationPath,
);
// add server.js file
writeToFile(
`${destinationPath}/src/server.js`,
EXPRESSJS_SERVER_TEMPLATE,
);
if (database) {
spinner.succeed();
spinner.start('Adding Database Module ...');
// create schema folder
createFolder(`${destinationPath}/src/modules/schemas`);
switch (database) {
case "mongodb":
switch (orm) {
case "mongoose":
default:
// create db config file
createAndUpdateFile(
`${destinationPath}/src/common/config/database.js`,
ExpressJsMongodbMongooseConnectionTemplate,
);
// create sample schema file
createAndUpdateFile(
`${destinationPath}/src/modules/schemas/sample.schema.js`,
ExpressJsMongoDbMongooseSampleSchema,
);
// update database config for server js file
database_config_import = `import { connectDb } from "./common/config/database.js";`;
database_config = ` connectDb()`;
// update packageJson
packageJson.dependencies = {
...packageJson.dependencies,
mongoose: "^7.5.2",
};
// update db config
additional_environment_variables += `DB: {
URL: process.env.DB_URL
}`;
}
}
}
// update server template
updateFileContent(
`${destinationPath}/src/server.js`,
EXPRESSJS_SERVER_TEMPLATE,
{
database_config,
database_config_import,
},
);
// add and update config file
updateFileContent(
`${destinationPath}/src/common/config/environment.js`,
ExpressJsEnvironmentTemplate,
{ additional_environment_variables },
);
// add package json file
createAndUpdateFile(
`${destinationPath}/package.json`,
JSON.stringify(ExpressJsPackageJsonTemplate, null, " "),
);
} else if (framework === "django") {
// django does not support some file namings so the name has to be parsed into a valid python identifier.
projectName = projectName.replaceAll(/[-\. ]/g, "");
// copy django template to directory
copyFile(getTemplateDir("backend/django/django-temp"), destinationPath);
// rename project name in final source
shell.mv(
`${destinationPath}/django_boilerplate`,
`${destinationPath}/${projectName}`,
);
writeToFile(`${destinationPath}/.env`, DJANGO_ENV_VARIABLES);
writeToFile(`${destinationPath}/manage.py`, DJANGO_MANAGER);
writeToFile(
`${destinationPath}/${projectName}/settings.py`,
DJANGO_SETTINGS,
);
writeToFile(`${destinationPath}/${projectName}/wsgi.py`, DJANGO_WSGI);
writeToFile(`${destinationPath}/${projectName}/asgi.py`, DJANGO_ASGI);
if (database && database !== "sqlite3") {
switch (database) {
case "postgresql":
updateFileContent(
`${destinationPath}/${projectName}/settings.py`,
DJANGO_SETTINGS,
{
projectName,
DATABASE_IMPORT: "import dj_database_url",
DATABASE_SETUP: DJANGO_POSTGRES_SETUP,
},
);
updateFileContent(`${destinationPath}/.env`, DJANGO_ENV_VARIABLES, {
SECRET_KEY: crypto.randomUUID().split("-").join(""),
DATABASE_ENV:
"DATABASE_URL=postgres://username:password@localhost:5432",
});
break;
}
} else {
updateFileContent(
`${destinationPath}/${projectName}/settings.py`,
DJANGO_SETTINGS,
{
projectName,
DATABASE_IMPORT: "",
DATABASE_SETUP: DJANGO_SQLITE_SETUP,
},
);
updateFileContent(`${destinationPath}/.env`, DJANGO_ENV_VARIABLES, {
SECRET_KEY: crypto.randomUUID().split("-").join(""),
DATABASE_ENV: "",
});
}
// add updates to django starter files
updateFileContent(`${destinationPath}/.env`, DJANGO_ENV_VARIABLES, {
SECRET_KEY: crypto.randomUUID().split("-").join(""),
});
updateFileContent(`${destinationPath}/manage.py`, DJANGO_MANAGER, {
projectName,
});
updateFileContent(
`${destinationPath}/${projectName}/wsgi.py`,
DJANGO_WSGI,
{
projectName,
},
);
updateFileContent(
`${destinationPath}/${projectName}/asgi.py`,
DJANGO_ASGI,
{
projectName,
},
);
}
// process dependencies install
if (installDependencies) {
spinner.succeed()
spinner.start("Installing dependencies ...")
await processDependenciesInstall(framework, destinationPath);
}
// success message
spinner.succeed()
spinner.succeed(`Backend project created successfully! : ${destinationPath}`)
// send stat
sendStat("startease", framework).then(() => {
})
} catch (e) {
console.log(`Error Creating Backend Project: ${e}`);
}
}