-
Notifications
You must be signed in to change notification settings - Fork 3
/
codeGenerator.js
173 lines (164 loc) · 8.16 KB
/
codeGenerator.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
const prompts = require('prompts');
const path = require("path");
const fs = require("fs");
const replace = require('key-value-replace');
const setting = require("./setting");
const questions = require('./questions');
const constant = require("./constant")
const createModel = require("./repository/model");
const customAPI = require("./repository/api");
const createModule = require("./repository/module");
const createBoilerplate = require("./repository/boilerplate");
class codeGenerator {
constructor({ projectType, ormType, operation, projectPath, db }) {
this.ormType = ormType;
this.projectType = projectType;
this.projectPath = projectPath;
this.db = db;
this.setup = setting(projectType, ormType, projectPath, db);
this.operation = operation;
}
async updateApp() {
let modelPath = this.setup.userDirectoryPaths.modelPath;
if (constant.ORM.SEQUELIZE === this.ormType) {
modelPath = replace(modelPath, { orm: 'sequelize' })
} else if (constant.ORM.MONGOOSE === this.ormType) {
modelPath = replace(modelPath, { orm: 'mongoDB' })
}
// let operation = await prompts(questions.SELECT_OPERATION);
switch (this.operation) {
case constant.CREATE_MODEL:
let cm = new createModel({
projectPath: this.projectPath,
setup: this.setup,
orm: this.ormType,
projectType: this.projectType
});
let model = await prompts(questions.ASK_MODEL_NAME);
if (!model || !model.value) {
throw new Error("please provide model name");
}
let existingModels = await this.getModelNames(modelPath);
if(existingModels.length && existingModels.includes("index")){
existingModels = existingModels.filter((e)=>{
return e !== "index";
})
}
let ansExistModel='y';
if (existingModels.includes(model.value)) {
ansExistModel = await prompts(questions.ASK_EXIST_MODEL);
ansExistModel = ansExistModel.value;
}
if (ansExistModel === 'y' || ansExistModel === 'Y') {
let attributes = await prompts(questions.ASK_MODEL_ATTRIBUTE);
await cm.renderModel({ modelName: model.value, attributes: attributes.value,modelPath });
}
break;
case constant.CREATE_API:
let models = await this.getModelNames(modelPath);
if (!models.length) {
throw new Error("we are not able to find existing model");
}
let modelName = await prompts(questions.GET_EXISTING_MODEL(models));
if (!modelName || !modelName.value) {
throw new Error("please select model.");
}
let platform = await this.getPlatformName(path.join(`${this.projectPath}`, 'routes'));
if (!platform.length) {
throw new Error("we are not able to find existing platform");
}
let platformName = await prompts(questions.GET_EXISTING_PLATFORM(platform));
if (!platformName || !platformName.value) {
throw new Error("please select platform.");
}
let routeMethod = await prompts(questions.SELECT_ROUTE_METHOD);
if (!routeMethod || !routeMethod.value) {
routeMethod = { value: 'get' }
}
let routePattern = await prompts(questions.ASK_ROUTE_PATTERN);
if (!routePattern || !routePattern.value) {
routePattern = { value: '/<your-pattern>' }
}
let controllerFunctionName = await prompts(questions.ASK_CONTROLLER_FUNCTION_NAME);
if (!controllerFunctionName || !controllerFunctionName.value) {
controllerFunctionName = { value: 'functionName' }
}
let route = new customAPI({ projectPath: this.projectPath, setup: this.setup, orm: this.ormType, projectType: this.projectType });
await route.renderRoute({
model: modelName.value,
platform: platformName.value,
routeMethod: routeMethod.value,
routePattern: routePattern.value,
controllerFunctionName: controllerFunctionName.value
});
break;
case constant.CREATE_MODULE:
let moduleModels = await this.getModelNames(modelPath);
if(moduleModels.length && moduleModels.includes("index")){
moduleModels = moduleModels.filter((e)=>{
return e !== "index";
})
}
let moduleModelName = await prompts(questions.GET_EXISTING_MODEL(moduleModels, true));
let moduleModelAttributes = false;
let isNewModel = false;
if (moduleModelName.value === 1) {
moduleModelName = await prompts(questions.ASK_MODEL_NAME);
isNewModel = true;
let ansOfModel = 'y';
if (moduleModels.includes(moduleModelName.value)) {
ansOfModel = await prompts(questions.ASK_EXIST_MODEL);
ansOfModel = ansOfModel.value;
}
if(!ansOfModel || (ansOfModel !== 'y' && ansOfModel !== 'Y')){
break;
}
moduleModelAttributes = await prompts(questions.ASK_MODEL_ATTRIBUTE);
}
let modulePlatform = await this.getPlatformName(path.join(`${this.projectPath}`, 'routes'));
let modulePlatformName = await prompts(questions.GET_EXISTING_PLATFORM(modulePlatform, "multiselect"));
let modelPermission = await prompts(questions.SELECT_ROUTE_API);
if (!moduleModelName || !moduleModelName.value) {
throw new Error("please provide model name.");
}
if (!modulePlatformName || !modulePlatformName.value || !modulePlatformName.value.length) {
throw new Error("please select at least one platform.");
}
if (!modelPermission || !modelPermission.value || !modelPermission.value.length) {
modelPermission = { value: ["C"] }
}
let module = new createModule({ projectPath: this.projectPath, setup: this.setup, orm: this.ormType, projectType: this.projectType });
await module.renderModule({
model: moduleModelName.value,
platform: modulePlatformName.value,
modelPermission: modelPermission.value,
attributes: moduleModelAttributes ? moduleModelAttributes.value : '',
isNewModel,
modelPath
});
break;
case constant.CREATE_APP:
let projectName = await prompts(questions.ASK_PROJECT_NAME);
projectName = projectName.value;
let boilerplate = new createBoilerplate({ projectPath: this.projectPath, setup: this.setup, orm: this.ormType, projectType: this.projectType, db: this.db });
await boilerplate.renderBoilerplate({ projectName });
break;
}
}
async getModelNames(modelPath) {
let models = fs.readdirSync(modelPath);
let existingModel = [];
models.forEach(file => {
if (path.extname(file) == ".js") {
existingModel.push(path.parse(file).name);
}
})
return existingModel;
}
async getPlatformName(routesPath) {
return fs.readdirSync(routesPath, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
}
}
module.exports = codeGenerator;