-
Notifications
You must be signed in to change notification settings - Fork 0
/
intrepeter.js
211 lines (178 loc) · 6.07 KB
/
intrepeter.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
const path = require('path');
const { utils } = require('codemaster');
const fs = require('fs');
const Printer = require('./printer');
const Model = require('./model');
const Controller = require('./controller');
const Views = require('./views');
const Router = require('./routes');
const Migration = require('./migrations');
const configPath = require('./configPath');
const DEFAULT_CHAINFILE = require('../.defaultchainfile');
let config;
let knexConfig;
const DEFAULT_FRONTEND = 'ejs';
const DEFAULT_BACKEND = 'enable';
const DEFAULT = {
frontend: 'ejs',
backend: 'enable',
middleware: 'disable',
};
const newMVC = (tableName, options) => {
config = getConfig();
knexConfig = getKnexConfig();
if (typeof tableName !== 'string') {
Printer.error('Not valid model name');
return;
}
const frontendType = getFrontendType(options);
const backend = getBackend(options);
const values = getValues(tableName, options, config);
const promises = createFiles(frontendType, backend, tableName, values);
Promise.all(promises).then().catch(() => { Printer.error('Error creating files'); });
};
const createFiles = (frontendType, backend, tableName, values) => {
const promises = [];
if (shouldCreate(frontendType)) {
promises.push(Views.createFile(tableName, values, config, frontendType));
}
if (shouldCreate(backend)) {
promises.push(Model.createFile(tableName, values, config));
promises.push(Controller.createFile(tableName, values, config));
promises.push(Router.createFile(values, config));
promises.push(Migration.createFile(tableName, values, config, knexConfig));
}
return promises;
};
const shouldCreate = (type) => {
return type !== 'disable';
};
const getBackend = (options) => {
options = options || {};
const configBackend = config ? config.backend : null;
return options.backend || configBackend || DEFAULT_BACKEND;
};
const getFrontendType = (options) => {
options = options || {};
const configFrontend = config ? config.frontend : null;
return options.frontend || configFrontend || DEFAULT_FRONTEND;
};
function getConfig() {
const p = path.join(process.cwd(), '.chainfile.js');
let defaultConfig = utils.cloneJSON(DEFAULT_CHAINFILE);
if (fs.existsSync(p)) {
// eslint-disable-next-line import/no-dynamic-require, global-require
const userConfig = require(p);
defaultConfig = Object.assign(defaultConfig, userConfig);
}
return defaultConfig;
}
function getKnexConfig() {
const p = config.knexfile;
const environment = process.env.NODE_ENV || 'development';
if (fs.existsSync(p)) {
return require(p)[environment]; // eslint-disable-line
}
const defaultPath = path.join(process.cwd(), '/knexfile.js');
if (fs.existsSync(defaultPath)) {
return require(defaultPath)[environment]; // eslint-disable-line
}
return defaultKnex(); // eslint-disable-line
}
function defaultKnex() {
return {
migrations: {
directory: path.join(process.cwd(), '/chinchapp/migrations'),
},
};
}
function getValues(tableName, options) {
let values = getNameValues(tableName);
values = addMiddlewareValues(values, options);
return values;
}
// #region GETNAMEVALUES
// eslint-disable-next-line max-lines-per-function
const getNameValues = (tableName) => {
tableName = parseCamelCase(tableName);
tableName = parseKebabCase(tableName);
tableName = tableName.toLowerCase();
const words = tableName.split('_');
const MODELNAME = Model.getName(tableName);
const CONTROLLERNAME = Controller.getName(MODELNAME);
const MODELFILENAME = Model.getFileName(MODELNAME);
return {
FLAT_CASE: toFlatCase(words),
PASCAL_CASE: toPascalCase(words),
CAMEL_CASE: toCamelCase(words),
SNAKE_CASE: toSnakeCase(words),
KEBAB_CASE: toKebabCase(words),
TRAIN_CASE: toTrainCase(words),
MACRO_CASE: toMacroCase(words),
MODELFILENAME,
MODELNAME,
CONTROLLERNAME,
CTRL2MODELPATH: path.relative(config.controllers.directory, path.join(config.models.directory, MODELFILENAME)).replace(/\\/g, '/'),
ROUTE2CTRL: path.relative(config.routes.directory, path.join(config.controllers.directory, CONTROLLERNAME)).replace(/\\/g, '/'),
CTRL2VIEWPATH: path.relative(config.controllers.directory, path.join(config.views.directory, MODELFILENAME)).replace(/\\/g, '/'),
TABLEPATH: path.relative(config.controllers.directory, configPath.TABLEPATH).replace(/\\/g, '/'),
TABLE_NAME: tableName,
};
};
const parseCamelCase = (input) => {
return input.replace(/([a-z0-9])([A-Z])/g, '$1_$2');
};
const parseKebabCase = (input) => {
return input.replace(/-/g, '_');
};
const toCamelCase = (array) => {
array = array || [];
let result = '';
if (array[0]) result += array[0];
for (let i = 1; i < array.length; i++) {
const element = array[i];
result += capitalizeFirstLetter(element);
}
return result;
};
const capitalizeFirstLetter = (string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
};
const toSnakeCase = (array) => {
array = array || [];
return array.join('_');
};
const toKebabCase = (array) => {
array = array || [];
return array.join('-');
};
const toFlatCase = (array) => {
array = array || [];
return array.join('');
};
const toPascalCase = (array) => {
return capitalizeFirstLetter(toCamelCase(array));
};
const toTrainCase = (array) => {
return toKebabCase(array).toUpperCase();
};
const toMacroCase = (array) => {
return toSnakeCase(array).toUpperCase();
};
// #endregion
const addMiddlewareValues = (values, options) => {
const middleware = getOption('middleware', options);
values = values || {};
const addToFrontend = ['frontend', 'enable', 'true'].includes(middleware);
values.MIDDLEWAREFRONTEND = addToFrontend ? 'Middleware.hasAccess, ' : '';
const addToAPI = ['api', 'enable', 'true'].includes(middleware);
values.MIDDLEWAREAPI = addToAPI ? 'Middleware.hasAccess, ' : '';
return values;
};
const getOption = (key, options) => {
options = options || {};
const configOption = config ? config[key] : null;
const value = options[key] || configOption || DEFAULT[key];
return value.toString().toLowerCase();
};
module.exports = { new: newMVC };