baja-lite 1.5.20 → 1.5.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/code.js +195 -121
- package/package.json +1 -1
package/README.md
CHANGED
package/code.js
CHANGED
|
@@ -47,112 +47,190 @@ const lxMap = {
|
|
|
47
47
|
multipolygon: "Object",
|
|
48
48
|
geometrycollection: "Object"
|
|
49
49
|
};
|
|
50
|
+
function info(i) {
|
|
51
|
+
console.log(info2(i));
|
|
52
|
+
}
|
|
53
|
+
function warn(i) {
|
|
54
|
+
console.log(warn2(i));
|
|
55
|
+
}
|
|
56
|
+
function success(i) {
|
|
57
|
+
console.log(success2(i));
|
|
58
|
+
}
|
|
59
|
+
function error(i) {
|
|
60
|
+
console.log(error2(i));
|
|
61
|
+
}
|
|
62
|
+
function log(i) {
|
|
63
|
+
console.log(log2(i));
|
|
64
|
+
}
|
|
65
|
+
function info2(i) {
|
|
66
|
+
return `\x1b[46m\x1b[30m${i}\x1b[0m`;
|
|
67
|
+
}
|
|
68
|
+
function warn2(i) {
|
|
69
|
+
return `\x1b[43m\x1b[30m${i}\x1b[0m`;
|
|
70
|
+
}
|
|
71
|
+
function success2(i) {
|
|
72
|
+
return `\x1b[42m\x1b[30m${i}\x1b[0m`;
|
|
73
|
+
}
|
|
74
|
+
function error2(i) {
|
|
75
|
+
return `\x1b[41m\x1b[30m${i}\x1b[0m`;
|
|
76
|
+
}
|
|
77
|
+
function log2(i) {
|
|
78
|
+
return `\x1b[47m\x1b[30m${i}\x1b[0m`;
|
|
79
|
+
}
|
|
80
|
+
let js = false;
|
|
81
|
+
function checkPath(p) {
|
|
82
|
+
let file = `${p}/baja.code.json`;
|
|
83
|
+
try {
|
|
84
|
+
info(`查找配置文件: ${file}`);
|
|
85
|
+
fs.statSync(file);
|
|
86
|
+
success(`发现配置文件: ${file}`);
|
|
87
|
+
return file;
|
|
88
|
+
}
|
|
89
|
+
catch (e1) {
|
|
90
|
+
try {
|
|
91
|
+
file = `${p}/baja.code.js`;
|
|
92
|
+
info(`查找配置文件: ${file}`);
|
|
93
|
+
fs.statSync(file);
|
|
94
|
+
success(`发现配置文件: ${file}`);
|
|
95
|
+
js = true;
|
|
96
|
+
return file;
|
|
97
|
+
}
|
|
98
|
+
catch (e2) {
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
50
103
|
let force = false;
|
|
51
104
|
let basepath = path.join(import.meta.dirname, '..', '..');
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
catch (error) {
|
|
105
|
+
let config = checkPath(basepath);
|
|
106
|
+
if (config === null) {
|
|
56
107
|
basepath = path.join(import.meta.dirname, '..', '..', '..', '..', '..');
|
|
108
|
+
config = checkPath(basepath);
|
|
57
109
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
"host": "",
|
|
66
|
-
"port": "",
|
|
67
|
-
"user": "",
|
|
68
|
-
"password": "",
|
|
69
|
-
"database": "",
|
|
110
|
+
function help1() {
|
|
111
|
+
console.log(`
|
|
112
|
+
**********************-----------
|
|
113
|
+
${info2('配置文件:')}
|
|
114
|
+
请在项目根目录添加文件:${warn2('baja.code.json')} 或者 ${warn2('baja.code.js(export defult {})')}
|
|
115
|
+
${info2('配置示例:')}
|
|
116
|
+
{
|
|
70
117
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
"
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
"
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
"
|
|
86
|
-
"logicDeleteK"?: "逻辑删除字段名",
|
|
87
|
-
"logicDeleteV"?: "逻辑删除值",如果是字符串需要这样 logicDeleteV: "'0'"
|
|
88
|
-
"NotlogicDeleteV"?: "未逻辑删除值",如果是字符串需要这样 NotlogicDeleteV: "'0'",
|
|
89
|
-
"skipColumns"?: ["跳过的列名1", "跳过的列名2"],
|
|
90
|
-
"custom"?: (列名:string) => string; 该属性支持为每个数据库列自定义一个名称,例如 字段名为 ci_name 可通过custom改造为 Ci Name
|
|
118
|
+
"host": "",
|
|
119
|
+
"port": "",
|
|
120
|
+
"user": "",
|
|
121
|
+
"password": "",
|
|
122
|
+
"database": "",
|
|
123
|
+
|
|
124
|
+
"command": {
|
|
125
|
+
"entity": "src/{vueName}/{vueName}.entity.ts",
|
|
126
|
+
"controller": "src/{vueName}/{vueName}.controller.ts",
|
|
127
|
+
"service": "src/{vueName}/{vueName}.service.ts",
|
|
128
|
+
"sql": "src/sql/{vueName}.mu",
|
|
129
|
+
"module": "src/{vueName}/{vueName}.module.ts"
|
|
130
|
+
},
|
|
131
|
+
"commands": {
|
|
132
|
+
"s": ["entity", "controller", "service", "sql", "module"]
|
|
91
133
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
output
|
|
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
|
-
|
|
134
|
+
"commands": {
|
|
135
|
+
"s": ["entity", "controller", "service", "sql", "module"]
|
|
136
|
+
},
|
|
137
|
+
"output": "{ClassName}Module",
|
|
138
|
+
"id"? : "uuidShort: true, notNull: true, uuid: true",
|
|
139
|
+
"logicDeleteK"?: "逻辑删除字段名",
|
|
140
|
+
"logicDeleteV"?: "逻辑删除值",如果是字符串需要这样 logicDeleteV: "'0'"
|
|
141
|
+
"NotlogicDeleteV"?: "未逻辑删除值",如果是字符串需要这样 NotlogicDeleteV: "'0'",
|
|
142
|
+
"skipColumns"?: ["跳过的列名1", "跳过的列名2"],
|
|
143
|
+
"custom"?: (列名:string) => string; 该属性支持为每个数据库列自定义一个名称,例如 字段名为 ci_name 可通过custom改造为 Ci Name
|
|
144
|
+
}
|
|
145
|
+
${warn2(`command`)}是生成命令,这里声明命令同时定义文件生成路径, 同时要求在模板中必须有对应模板. ${info2('路径是相对于项目根目录的相对路径')}.
|
|
146
|
+
${warn2('commands')}是组合命令,上面的配置表示输入${error2('s')}同时生成:${success2('"entity", "controller", "service", "sql", "module"这几个模块')}
|
|
147
|
+
${warn2('output')} 是生成后打印出的内容
|
|
148
|
+
`);
|
|
149
|
+
}
|
|
150
|
+
function help2() {
|
|
151
|
+
console.log(`
|
|
152
|
+
${error2('模板文件说明')}
|
|
153
|
+
使用${error2('mustache')}
|
|
154
|
+
${warn2('模板变量输出')}${info2('<% 变量 %>')}
|
|
155
|
+
${warn2('模板转义')}${info2('<%& 变量 %>')}
|
|
156
|
+
${warn2('模板循环')}${info2('<%# 变量 %> <%/变量%>')}
|
|
157
|
+
${warn2('模板路径')} 项目根目录的${error2('code-template')}
|
|
158
|
+
${warn2('模板使用')} 在配置文件中的command下定义命令,每个命令的key
|
|
159
|
+
${error2('变量说明')}
|
|
160
|
+
---------------------------------------------------------------------------------------------------------------
|
|
161
|
+
************* 以下假设表名为${error2('event_main_info')},列名为${error2('sku_id,sku_name')}
|
|
162
|
+
title // 字符串,表的注释
|
|
163
|
+
|
|
164
|
+
tableName // 字符串,event_main_info
|
|
165
|
+
className // 字符串,eventMainInfo
|
|
166
|
+
ClassName // 字符串,EventMainInfo
|
|
167
|
+
vueName // 字符串,event-main-info
|
|
168
|
+
splitVueName // 字符串,event/main-info
|
|
169
|
+
splitName // 字符串,event/main/info
|
|
170
|
+
SplitName // 字符串,event/mainInfo
|
|
171
|
+
|
|
172
|
+
columns // 数组, 元素格式为{comment:注释,name: sku_id,Name: skuId,NAME: SkuId,Field:表示字段的注解,JSField_name、JSFieldName 分别表示JS的注解,Type:表示JS类
|
|
173
|
+
型,custom:自定义列名}
|
|
174
|
+
column_names // 数组, 元素是列名字符串,格式是 sku_id,sku_name
|
|
175
|
+
ColumnNames // 数组, 元素是列名字符串,格式是 skuId,skuName
|
|
176
|
+
column_names_join // 字符串,列名join的字符串,格式是 "sku_id,sku_name"
|
|
177
|
+
ColumnNames_join // 字符串,列名join的字符串,格式是 "skuId,skuName"
|
|
178
|
+
|
|
179
|
+
column_names_joinT // 字符串,列名join的字符串,格式是 "t.sku_id,t.sku_name"
|
|
180
|
+
ColumnNames_joinT // 字符串,列名join的字符串,格式是 "t.skuId,t.skuName"
|
|
127
181
|
|
|
182
|
+
columns_no_id // 数组, 不含主键, 元素格式为{comment:注释,name: sku_id,Name: skuId,NAME: SkuId,Field:表示字段的注解,JSField_name, JSFieldName 分别表示JS的注解,Type:表示JS类型
|
|
183
|
+
}
|
|
184
|
+
column_names_no_id // 数组, 不含主键, 元素是列名字符串,格式是 sku_id,sku_name
|
|
185
|
+
ColumnNames_no_id // 数组, 不含主键, 元素是列名字符串,格式是 skuId,skuName
|
|
186
|
+
column_names_no_id_join // 字符串, 不含主键, 列名join的字符串,格式是 "sku_id,sku_name"
|
|
187
|
+
ColumnNames_no_id_join // 字符串, 不含主键, 列名join的字符串,格式是 "skuId,skuName"
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
columns_no_skip // 数组, 不含跳过的列,元素格式为{comment:注释,name: sku_id,Name: skuId,NAME: SkuId,Field:表示字段的注解,JSField_name, JSFieldName 分别表示JS的注解,Type:表示JS类型}
|
|
191
|
+
column_names_no_skip // 数组, 不含跳过的列, 元素是列名字符串,格式是 sku_id,sku_name
|
|
192
|
+
ColumnNames_no_skip // 数组, 不含跳过的列, 元素是列名字符串,格式是 skuId,skuName
|
|
193
|
+
column_names_no_skip_join // 数组, 不含跳过的列, 元素是列名字符串,格式是 "sku_id,sku_name"
|
|
194
|
+
ColumnNames_no_skip_join // 数组, 不含跳过的列, 元素是列名字符串,格式是 "skuId,skuName"
|
|
128
195
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
196
|
+
ids // 数组, 只有主键, 元素格式为{comment:注释,name: sku_id,Name: skuId,NAME: SkuId,Field:表示字段的注解,JSField_name, JSFieldName 分别表示JS的注解,Type:表示JS类型}
|
|
197
|
+
id_names // 数组, 只有主键, 元素是列名字符串,格式是 sku_id,sku_name
|
|
198
|
+
IdNames // 数组, 只有主键, 元素是列名字符串,格式是 skuId,skuName
|
|
199
|
+
id_names_join // 字符串,列名join的字符串,格式是 "sku_id,sku_name"
|
|
200
|
+
IdNames_join // 字符串,列名join的字符串,格式是 "skuId,skuName"
|
|
134
201
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
table=. 表示扫描全库表
|
|
148
|
-
:模块名称 可以省略
|
|
149
|
-
-----
|
|
150
|
-
.force: 切换是否覆盖
|
|
202
|
+
modelName // 推断出的模块名称,可能为空字符串, 如果表名为event_main_info, 则模块为 event
|
|
203
|
+
modelPath // 模块名称实际就影响访问路径,所以这里会直接拼好controller的模块访问路径,如果模块为空,则该属性就是空字符串,否则是 /模块名称/
|
|
204
|
+
|
|
205
|
+
logicDelete // 逻辑删除的查询条件,可以附加在sql条件的末尾,可能是空的
|
|
206
|
+
---------------------------------------------------------------------------------------------------------------
|
|
207
|
+
${warn2('特殊命令')}
|
|
208
|
+
${warn2('多表操作')} .命令 table1,table2,table3:模块名称 ${info2(':模块名称 可以省略')}
|
|
209
|
+
${warn2('全库操作')} .命令 . ${info2('注意:.表示全表')}
|
|
210
|
+
${warn2('覆盖生成')} .force
|
|
211
|
+
${warn2('配置说明')} .help1
|
|
212
|
+
${warn2('模板说明')} .help2
|
|
213
|
+
-----
|
|
151
214
|
`);
|
|
215
|
+
}
|
|
216
|
+
if (config === null) {
|
|
217
|
+
error('NOT FOUND Config File!!');
|
|
218
|
+
help1();
|
|
219
|
+
throw new Error('NOT FOUND Config File');
|
|
220
|
+
}
|
|
221
|
+
const templatePath = path.join(basepath, 'code-template');
|
|
222
|
+
help2();
|
|
152
223
|
try {
|
|
153
224
|
const outputs = new Set();
|
|
154
|
-
|
|
155
|
-
|
|
225
|
+
let configData;
|
|
226
|
+
if (js === false) {
|
|
227
|
+
const _configData = fs.readFileSync(config, { encoding: 'utf-8' }).toString();
|
|
228
|
+
configData = JSON.parse(_configData);
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
const _configData = fs.readFileSync(config, { encoding: 'utf-8' }).toString();
|
|
232
|
+
configData = new Function(_configData.replace('export default', 'return'))();
|
|
233
|
+
}
|
|
156
234
|
configData.skipColumns ?? (configData.skipColumns = []);
|
|
157
235
|
configData.command ?? (configData.command = {});
|
|
158
236
|
configData.commands ?? (configData.commands = {});
|
|
@@ -162,10 +240,10 @@ try {
|
|
|
162
240
|
configData.logicDeleteV ?? (configData.logicDeleteV = 0);
|
|
163
241
|
configData.NotlogicDeleteV ?? (configData.NotlogicDeleteV = 0);
|
|
164
242
|
if (!configData.host || !configData.port || !configData.user || !configData.password || !configData.database) {
|
|
165
|
-
|
|
243
|
+
error(`[错误]配置文件缺少必要的参数: host, port, user, password, database`);
|
|
166
244
|
}
|
|
167
245
|
if (!fs.existsSync(templatePath)) {
|
|
168
|
-
|
|
246
|
+
error(`[错误]模板文件夹不存在,请在项目根目录创建code-template文件夹`);
|
|
169
247
|
}
|
|
170
248
|
const templates = Object.fromEntries(fs.readdirSync(templatePath).map(r => [path.basename(r, '.mu'), fs.readFileSync(path.join(templatePath, r), { encoding: 'utf-8' }).toString()]));
|
|
171
249
|
const pool = createPool({
|
|
@@ -286,7 +364,7 @@ try {
|
|
|
286
364
|
if (input !== '.') {
|
|
287
365
|
const checkTable = input.split(/,|\s/).map(r => r.trim());
|
|
288
366
|
if (checkTable.length !== tables.length) {
|
|
289
|
-
|
|
367
|
+
error(`[错误] 输入的表与数据库查询返回表不符,数据库返回${tables.length}个:${tables.map(i => i.tableName).join(',')}`);
|
|
290
368
|
return;
|
|
291
369
|
}
|
|
292
370
|
}
|
|
@@ -338,7 +416,7 @@ try {
|
|
|
338
416
|
};
|
|
339
417
|
const template = templates[command];
|
|
340
418
|
if (!template) {
|
|
341
|
-
|
|
419
|
+
error(`[错误] ${command} 未定义模板!`);
|
|
342
420
|
return;
|
|
343
421
|
}
|
|
344
422
|
const txt = mustache.render(template, data, {}, ['<%', '%>']);
|
|
@@ -348,24 +426,18 @@ try {
|
|
|
348
426
|
const filePath = path.join(basepath, fileName);
|
|
349
427
|
const dirname = path.dirname(filePath);
|
|
350
428
|
mkdir('-p', dirname);
|
|
351
|
-
// try {
|
|
352
|
-
// fs.statSync(dirname);
|
|
353
|
-
// } catch (error) {
|
|
354
|
-
// fs.mkdirSync(dirname);
|
|
355
|
-
// console.info(`[生成] ${dirname}`);
|
|
356
|
-
// }
|
|
357
429
|
try {
|
|
358
430
|
fs.statSync(filePath);
|
|
359
431
|
if (force === false) {
|
|
360
|
-
|
|
432
|
+
warn(`[跳过] ${filePath}`);
|
|
361
433
|
return;
|
|
362
434
|
}
|
|
363
435
|
else {
|
|
364
|
-
|
|
436
|
+
warn(`[覆盖] ${filePath}`);
|
|
365
437
|
}
|
|
366
438
|
}
|
|
367
439
|
catch (error) {
|
|
368
|
-
|
|
440
|
+
info(`[生成] ${filePath}`);
|
|
369
441
|
}
|
|
370
442
|
if (configData.output) {
|
|
371
443
|
outputs.add(configData.output.replace(/{([a-zA-Z]+)}/g, (a, b) => data[b]));
|
|
@@ -377,24 +449,24 @@ try {
|
|
|
377
449
|
const replServer = start();
|
|
378
450
|
function defineCommand(command, comands) {
|
|
379
451
|
if (comands) {
|
|
380
|
-
|
|
452
|
+
log(`\x1b[42m\x1b[30m[组合]${command}>${comands.join(',')}注册成功\x1b[0m`);
|
|
381
453
|
}
|
|
382
454
|
else {
|
|
383
|
-
|
|
455
|
+
log(`\x1b[42m\x1b[30m[命令]${command}注册成功\x1b[0m`);
|
|
384
456
|
}
|
|
385
457
|
if (comands) {
|
|
386
458
|
replServer.defineCommand(command, async (input) => {
|
|
387
459
|
outputs.clear();
|
|
388
460
|
const inputs = input.match(/([^:]+):{0,1}([a-zA-Z0-9]*)/);
|
|
389
461
|
if (inputs?.length !== 3) {
|
|
390
|
-
return
|
|
462
|
+
return error(`[错误]命令格式应为: table1,table2[:模块名]`);
|
|
391
463
|
}
|
|
392
464
|
const [_, tables, modelName] = inputs;
|
|
393
465
|
for (const c of comands) {
|
|
394
466
|
await excute(c, tables, modelName ?? '');
|
|
395
467
|
}
|
|
396
|
-
|
|
397
|
-
|
|
468
|
+
info('执行完毕!下面打印生成的输出');
|
|
469
|
+
info(Array.from(outputs).join(','));
|
|
398
470
|
});
|
|
399
471
|
}
|
|
400
472
|
else {
|
|
@@ -402,23 +474,25 @@ try {
|
|
|
402
474
|
outputs.clear();
|
|
403
475
|
const inputs = input.match(/([^:]+):{0,1}([a-zA-Z0-9]*)/);
|
|
404
476
|
if (inputs?.length !== 3) {
|
|
405
|
-
return
|
|
477
|
+
return error(`[错误]命令格式应为: table1,table2[:模块名]`);
|
|
406
478
|
}
|
|
407
479
|
const [_, tables, modelName] = inputs;
|
|
408
480
|
await excute(command, tables, modelName ?? '');
|
|
409
|
-
|
|
410
|
-
|
|
481
|
+
info('执行完毕!下面打印生成的输出');
|
|
482
|
+
info(Array.from(outputs).join(','));
|
|
411
483
|
});
|
|
412
484
|
}
|
|
413
485
|
}
|
|
414
486
|
replServer.defineCommand('force', () => {
|
|
415
487
|
force = !force;
|
|
416
|
-
|
|
488
|
+
log(force ? '覆盖生成' : '不覆盖生成!');
|
|
417
489
|
});
|
|
490
|
+
replServer.defineCommand('help1', help1);
|
|
491
|
+
replServer.defineCommand('help2', help2);
|
|
418
492
|
if (configData.command) {
|
|
419
493
|
for (const command of Object.keys(configData.command)) {
|
|
420
494
|
if (!templates[command]) {
|
|
421
|
-
|
|
495
|
+
error(`命令:${command}没有定义模板,该命令不会生效`);
|
|
422
496
|
}
|
|
423
497
|
else {
|
|
424
498
|
defineCommand(command);
|
|
@@ -428,9 +502,9 @@ try {
|
|
|
428
502
|
if (configData.commands) {
|
|
429
503
|
for (const [command, commands] of Object.entries(configData.commands)) {
|
|
430
504
|
const keys = commands;
|
|
431
|
-
const
|
|
432
|
-
if (
|
|
433
|
-
|
|
505
|
+
const e = keys.filter(k => !templates[k]).join(',');
|
|
506
|
+
if (e) {
|
|
507
|
+
error(`组合命令:${command}定义了${commands},但是${e}没有定义模板,该命令不会生效`);
|
|
434
508
|
}
|
|
435
509
|
else {
|
|
436
510
|
defineCommand(command, keys);
|
|
@@ -439,5 +513,5 @@ try {
|
|
|
439
513
|
}
|
|
440
514
|
}
|
|
441
515
|
catch (error) {
|
|
442
|
-
|
|
516
|
+
error(error);
|
|
443
517
|
}
|