baja-lite 1.6.4 → 1.6.6

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.
Files changed (73) hide show
  1. package/boot-remote.d.ts +2 -0
  2. package/{src/boot-remote.ts → boot-remote.js} +66 -64
  3. package/boot.d.ts +2 -0
  4. package/{src/boot.ts → boot.js} +166 -170
  5. package/code.d.ts +2 -0
  6. package/{src/code.ts → code.js} +405 -414
  7. package/convert-xml.d.ts +10 -0
  8. package/{src/convert-xml.ts → convert-xml.js} +410 -460
  9. package/error.d.ts +5 -0
  10. package/error.js +13 -0
  11. package/event.d.ts +10 -0
  12. package/event.js +38 -0
  13. package/fn.d.ts +128 -0
  14. package/fn.js +172 -0
  15. package/{src/index.ts → index.d.ts} +10 -11
  16. package/index.js +10 -0
  17. package/math.d.ts +83 -0
  18. package/math.js +451 -0
  19. package/object.d.ts +126 -0
  20. package/object.js +321 -0
  21. package/package.json +13 -13
  22. package/snowflake.d.ts +12 -0
  23. package/{src/snowflake.ts → snowflake.js} +108 -127
  24. package/sql.d.ts +2151 -0
  25. package/sql.js +5371 -0
  26. package/sqlite.d.ts +32 -0
  27. package/{src/sqlite.ts → sqlite.js} +156 -157
  28. package/string.d.ts +17 -0
  29. package/string.js +105 -0
  30. package/test-mysql.d.ts +2 -0
  31. package/test-mysql.js +114 -0
  32. package/test-postgresql.d.ts +2 -0
  33. package/{src/test-postgresql.ts → test-postgresql.js} +91 -80
  34. package/test-sqlite.d.ts +1 -0
  35. package/{src/test-sqlite.ts → test-sqlite.js} +90 -80
  36. package/test-xml.d.ts +1 -0
  37. package/{src/test-xml.ts → test-xml.js} +2 -2
  38. package/test.d.ts +1 -0
  39. package/{src/test.ts → test.js} +2 -3
  40. package/wx/base.d.ts +11 -0
  41. package/wx/base.js +78 -0
  42. package/wx/mini.d.ts +52 -0
  43. package/wx/mini.js +112 -0
  44. package/wx/organ.d.ts +65 -0
  45. package/wx/organ.js +171 -0
  46. package/{src/wx/types.ts → wx/types.d.ts} +560 -549
  47. package/wx/types.js +1 -0
  48. package/{src/wx.ts → wx.d.ts} +3 -3
  49. package/wx.js +3 -0
  50. package/.eslintignore +0 -7
  51. package/.eslintrc.cjs +0 -89
  52. package/.prettierrc +0 -7
  53. package/.vscode/settings.json +0 -9
  54. package/ci.js +0 -33
  55. package/package-cjs.json +0 -17
  56. package/pnpm-lock.yaml +0 -2840
  57. package/pnpm-workspace.yaml +0 -2
  58. package/src/error.ts +0 -11
  59. package/src/event.ts +0 -34
  60. package/src/fn.ts +0 -295
  61. package/src/math.ts +0 -405
  62. package/src/object.ts +0 -342
  63. package/src/sql.ts +0 -5529
  64. package/src/string.ts +0 -111
  65. package/src/test-mysql.ts +0 -148
  66. package/src/wx/base.ts +0 -77
  67. package/src/wx/mini.ts +0 -147
  68. package/src/wx/organ.ts +0 -290
  69. package/tsconfig.cjs.json +0 -42
  70. package/tsconfig.json +0 -44
  71. package/xml/event-report.xml +0 -13
  72. package/yarn.lock +0 -1977
  73. /package/{Readme.md → README.md} +0 -0
@@ -1,112 +1,113 @@
1
- #!/usr/bin/env node
2
- import fs from 'fs';
3
- import mustache from 'mustache';
4
- import { createPool } from 'mysql2/promise';
5
- import path from 'path';
6
- import { start } from 'repl';
7
- import pkg from 'shelljs';
8
- const { mkdir } = pkg;
9
- const lxMap = {
10
- tinyint: "number",
11
- smallint: "number",
12
- mediumint: "number",
13
- int: "number",
14
- integer: "number",
15
- bigint: "number",
16
- bit: "boolean",
17
- double: "number",
18
- real: "number",
19
- float: "number",
20
- decimal: "number",
21
- numeric: "number",
22
- char: "string",
23
- varchar: "string",
24
- date: "Date",
25
- time: "string",
26
- year: "string",
27
- timestamp: "number",
28
- datetime: "Date",
29
- tinyblob: "string",
30
- blob: "string",
31
- mediumblob: "string",
32
- longblob: "string",
33
- tinytext: "string",
34
- text: "string",
35
- mediumtext: "string",
36
- longtext: "string",
37
- enum: "string",
38
- set: "string",
39
- binary: "string",
40
- varbinary: "string",
41
- point: "Object",
42
- linestring: "Object",
43
- polygon: "Object",
44
- geometry: "string",
45
- multipoint: "Object",
46
- multilinestring: "Object",
47
- multipolygon: "Object",
48
- geometrycollection: "Object"
49
- };
50
- function info(i: string) {
51
- console.log(info2(i));
52
- }
53
- function warn(i: string) {
54
- console.log(warn2(i));
55
- }
56
- function success(i: string) {
57
- console.log(success2(i));
58
- }
59
- function error(i: string) {
60
- console.log(error2(i));
61
- }
62
- function log(i: string) {
63
- console.log(log2(i));
64
- }
65
- function info2(i: string) {
66
- return `\x1b[46m\x1b[30m${i}\x1b[0m`;
67
- }
68
- function warn2(i: string) {
69
- return `\x1b[43m\x1b[30m${i}\x1b[0m`;
70
- }
71
- function success2(i: string) {
72
- return `\x1b[42m\x1b[30m${i}\x1b[0m`;
73
- }
74
- function error2(i: string) {
75
- return `\x1b[41m\x1b[30m${i}\x1b[0m`;
76
- }
77
- function log2(i: string) {
78
- return `\x1b[47m\x1b[30m${i}\x1b[0m`;
79
- }
80
- let js = false;
81
- function checkPath(p: string) {
82
- let file = `${p}/baja.code.json`;
83
- try {
84
- info(`查找配置文件: ${file}`);
85
- fs.statSync(file);
86
- success(`发现配置文件: ${file}`);
87
- return file;
88
- } catch (e1) {
89
- try {
90
- file = `${p}/baja.code.js`;
91
- info(`查找配置文件: ${file}`);
92
- fs.statSync(file);
93
- success(`发现配置文件: ${file}`);
94
- js = true;
95
- return file;
96
- } catch (e2) {
97
-
98
- }
99
- }
100
- return null;
101
- }
102
- let force = false;
103
- let basepath = path.join(import.meta.dirname, '..', '..');
104
- let config = checkPath(basepath);
105
- if (config === null) {
106
- basepath = path.join(import.meta.dirname, '..', '..', '..', '..', '..');
107
- config = checkPath(basepath);
108
- }
109
- function help1() {
1
+ #!/usr/bin/env node
2
+ import fs from 'fs';
3
+ import mustache from 'mustache';
4
+ import { createPool } from 'mysql2/promise';
5
+ import path from 'path';
6
+ import { start } from 'repl';
7
+ import pkg from 'shelljs';
8
+ const { mkdir } = pkg;
9
+ const lxMap = {
10
+ tinyint: "number",
11
+ smallint: "number",
12
+ mediumint: "number",
13
+ int: "number",
14
+ integer: "number",
15
+ bigint: "number",
16
+ bit: "boolean",
17
+ double: "number",
18
+ real: "number",
19
+ float: "number",
20
+ decimal: "number",
21
+ numeric: "number",
22
+ char: "string",
23
+ varchar: "string",
24
+ date: "Date",
25
+ time: "string",
26
+ year: "string",
27
+ timestamp: "number",
28
+ datetime: "Date",
29
+ tinyblob: "string",
30
+ blob: "string",
31
+ mediumblob: "string",
32
+ longblob: "string",
33
+ tinytext: "string",
34
+ text: "string",
35
+ mediumtext: "string",
36
+ longtext: "string",
37
+ enum: "string",
38
+ set: "string",
39
+ binary: "string",
40
+ varbinary: "string",
41
+ point: "Object",
42
+ linestring: "Object",
43
+ polygon: "Object",
44
+ geometry: "string",
45
+ multipoint: "Object",
46
+ multilinestring: "Object",
47
+ multipolygon: "Object",
48
+ geometrycollection: "Object"
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
+ }
103
+ let force = false;
104
+ let basepath = path.join(import.meta.dirname, '..', '..');
105
+ let config = checkPath(basepath);
106
+ if (config === null) {
107
+ basepath = path.join(import.meta.dirname, '..', '..', '..', '..', '..');
108
+ config = checkPath(basepath);
109
+ }
110
+ function help1() {
110
111
  console.log(`
111
112
  **********************-----------
112
113
  ${info2('配置文件:')}
@@ -144,9 +145,9 @@ ${info2('配置示例:')}
144
145
  ${warn2(`command`)}是生成命令,这里声明命令同时定义文件生成路径, 同时要求在模板中必须有对应模板. ${info2('路径是相对于项目根目录的相对路径')}.
145
146
  ${warn2('commands')}是组合命令,上面的配置表示输入${error2('s')}同时生成:${success2('"entity", "controller", "service", "sql", "module"这几个模块')}
146
147
  ${warn2('output')} 是生成后打印出的内容
147
- `);
148
- }
149
- function help2() {
148
+ `);
149
+ }
150
+ function help2() {
150
151
  console.log(`
151
152
  ${error2('模板文件说明')}
152
153
  使用${error2('mustache')}
@@ -210,84 +211,65 @@ ${warn2('覆盖生成')} .force
210
211
  ${warn2('配置说明')} .help1
211
212
  ${warn2('模板说明')} .help2
212
213
  -----
213
- `);
214
- }
215
- if (config === null) {
216
- error('NOT FOUND Config File!!');
217
- help1();
218
- throw new Error('NOT FOUND Config File');
219
- }
220
-
221
- const templatePath = path.join(basepath, 'code-template');
222
- help2();
223
-
224
- try {
225
- const outputs = new Set<string>();
226
- let configData: {
227
- host: string;
228
- port: number;
229
- user: string;
230
- password: string;
231
- database: string;
232
- command: Record<string, string>;
233
- commands: Record<string, string[]>;
234
- output: string;
235
- id: string;
236
- logicDeleteK: string;
237
- logicDeleteV: number;
238
- NotlogicDeleteV: number;
239
- tables: string;
240
- skipColumns: string[];
241
- custom?: (c: string) => string;
242
- };
243
- if (js === false) {
244
- const _configData = fs.readFileSync(config, { encoding: 'utf-8' }).toString();
245
- configData = JSON.parse(_configData);
246
- } else {
247
- const _configData = fs.readFileSync(config, { encoding: 'utf-8' }).toString();
248
- configData = new Function(_configData.replace('export default', 'return'))();
249
- }
250
-
251
- configData.skipColumns ??= [];
252
- configData.command ??= {};
253
- configData.commands ??= {};
254
- configData.output ??= '';
255
- configData.id ??= 'uuidShort: true, notNull: true, uuid: true';
256
- configData.logicDeleteK ??= '';
257
- configData.logicDeleteV ??= 0;
258
- configData.NotlogicDeleteV ??= 0;
259
- if (!configData.host || !configData.port || !configData.user || !configData.password || !configData.database) {
260
- error(`[错误]配置文件缺少必要的参数: host, port, user, password, database`);
261
- }
262
- if (!fs.existsSync(templatePath)) {
263
- error(`[错误]模板文件夹不存在,请在项目根目录创建code-template文件夹`);
264
- }
265
-
266
- const templates = Object.fromEntries(fs.readdirSync(templatePath).map(r => [path.basename(r, '.mu'), fs.readFileSync(path.join(templatePath, r), { encoding: 'utf-8' }).toString()]));
267
- const pool = createPool({
268
- host: configData.host,
269
- port: configData.port,
270
- user: configData.user,
271
- password: configData.password,
272
- database: configData.database
273
- });
274
- async function getTables(tableName: string) {
275
- const conn = await pool.getConnection();
276
- const params: (string | string[])[] = [configData.database];
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();
223
+ try {
224
+ const outputs = new Set();
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
+ }
234
+ configData.skipColumns ?? (configData.skipColumns = []);
235
+ configData.command ?? (configData.command = {});
236
+ configData.commands ?? (configData.commands = {});
237
+ configData.output ?? (configData.output = '');
238
+ configData.id ?? (configData.id = 'uuidShort: true, notNull: true, uuid: true');
239
+ configData.logicDeleteK ?? (configData.logicDeleteK = '');
240
+ configData.logicDeleteV ?? (configData.logicDeleteV = 0);
241
+ configData.NotlogicDeleteV ?? (configData.NotlogicDeleteV = 0);
242
+ if (!configData.host || !configData.port || !configData.user || !configData.password || !configData.database) {
243
+ error(`[错误]配置文件缺少必要的参数: host, port, user, password, database`);
244
+ }
245
+ if (!fs.existsSync(templatePath)) {
246
+ error(`[错误]模板文件夹不存在,请在项目根目录创建code-template文件夹`);
247
+ }
248
+ const templates = Object.fromEntries(fs.readdirSync(templatePath).map(r => [path.basename(r, '.mu'), fs.readFileSync(path.join(templatePath, r), { encoding: 'utf-8' }).toString()]));
249
+ const pool = createPool({
250
+ host: configData.host,
251
+ port: configData.port,
252
+ user: configData.user,
253
+ password: configData.password,
254
+ database: configData.database
255
+ });
256
+ async function getTables(tableName) {
257
+ const conn = await pool.getConnection();
258
+ const params = [configData.database];
277
259
  let sql = `
278
260
  SELECT TABLE_NAME tableName, IFNULL(TABLE_COMMENT, TABLE_NAME) title FROM information_schema.TABLES
279
- WHERE TABLE_SCHEMA= ? AND TABLE_TYPE = 'BASE TABLE'`;
280
- if (tableName !== '.') {
281
- sql += ` AND TABLE_NAME IN (?)`;
282
- params.push(tableName.split(/,|\s/).map(r => r.trim()));
283
- }
284
- const [result] = await conn.query<any[]>(sql, params);
285
- conn.release();
286
- return result;
287
- }
288
- async function getColumns(tableName: string) {
289
- const conn = await pool.getConnection();
290
- const [result] = await conn.query<any[]>(`
261
+ WHERE TABLE_SCHEMA= ? AND TABLE_TYPE = 'BASE TABLE'`;
262
+ if (tableName !== '.') {
263
+ sql += ` AND TABLE_NAME IN (?)`;
264
+ params.push(tableName.split(/,|\s/).map(r => r.trim()));
265
+ }
266
+ const [result] = await conn.query(sql, params);
267
+ conn.release();
268
+ return result;
269
+ }
270
+ async function getColumns(tableName) {
271
+ const conn = await pool.getConnection();
272
+ const [result] = await conn.query(`
291
273
  SELECT
292
274
  DATA_TYPE type,
293
275
  COLUMN_NAME \`name\`,
@@ -298,229 +280,238 @@ try {
298
280
  IF(IS_NULLABLE = 'NO', 1, 0) notNull,
299
281
  COLUMN_COMMENT \`comment\`
300
282
  FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=? AND TABLE_NAME = ?;
301
- `, [configData.database, tableName]);
302
- let logicDelete = '';
303
- const columns = result.map(r => {
304
- if (r.id === 1) { r.id = true; }
305
- else delete r.id;
306
- if (r.notNull === 1) { r.notNull = true; }
307
- else delete r.notNull;
308
- const fields = new Array<string>(`type:SqlType.${r.type}`);
309
- r.Type = lxMap[r.type];
310
- if (r.length !== null) { fields.push(`length:${r.length}`); }
311
- if (r.scale !== null) { fields.push(`scale:${r.scale}`); }
312
- if (r.def !== null) {
313
- r.def ??= '';
314
- if (r.def !== '') {
315
- if (isNaN(r.def)) {
316
- fields.push(`def:'${r.def}'`);
317
- } else {
318
- fields.push(`def:${r.def}`);
319
- }
320
- }
321
- }
322
- if (r.Type === 'string') {
323
- if (r.def === '' || r.def === null) {
324
- r.def = "''";
325
- } else {
326
- r.def = `'${r.def}'`;
327
- }
328
- } else if (r.Type === 'number') {
329
- r.def = 0;
330
- } else if (r.Type === 'Date') {
331
- r.def = 'new Date()';
332
- } else {
333
- r.def = 'null';
334
- }
335
- if (r.id === true) {
336
- fields.push(`id:true`);
337
- }
338
- if (r.notNull === true) { fields.push(`notNull:true`); }
339
- if (r.id === true && configData.id) {
340
- fields.push(configData.id);
341
- }
342
- if (r.name === configData.logicDeleteK) {
343
- fields.push(`logicDelete: ${configData.logicDeleteV}`);
344
- logicDelete = `AND ${r.name} = ${configData.NotlogicDeleteV}`;
345
- }
346
- if (r.comment) { fields.push(`comment: '${r.comment}'`); }
347
- r.comment = r.comment ?? '';
348
-
349
- r.Name = r.name.replace(/_(\w)/g, (a: string, b: string) => b.toUpperCase());
350
- r.NAME = r.Name.replace(/\w/, (v: string) => v.toUpperCase());
351
- r.Field = `@Field({${fields.join(',')}})`;
352
- r.JSField_name = `{${fields.join(',')}, P: '${r.name}'}`;
353
- r.JSFieldName = `{${fields.join(',')}, P: '${r.Name}'}`;
354
- if (configData.custom) {
355
- r.custom = configData.custom(r.name);
356
- }
357
- return r;
358
- });
359
- conn.release();
360
- return { columns, logicDelete };
361
- }
362
- async function excute(command: string, input: string, modelName: string) {
363
- const tables = await getTables(input);
364
- if (input !== '.') {
365
- const checkTable = input.split(/,|\s/).map(r => r.trim())
366
- if (checkTable.length !== tables.length) {
367
- error(`[错误] 输入的表与数据库查询返回表不符,数据库返回${tables.length}个:${tables.map(i => i.tableName).join(',')}`);
368
- return;
369
- }
370
- }
371
- modelName ??= '';
372
- const modelPath = modelName ? `/${modelName}/` : '';
373
- for (const { tableName, title } of tables) {
374
- const { columns, logicDelete } = await getColumns(tableName);
375
- const className = tableName.replace(/_(\w)/g, (a: string, b: string) => b.toUpperCase());
376
- const ClassName = className.replace(/\w/, (v: string) => v.toUpperCase());
377
- const vueName = tableName.replace(/_/g, '-');
378
- const splitVueName = tableName.replace(/_/, '/').replace(/_/g, '-');
379
- const modelName = tableName.substring(0, tableName.indexOf('_'));
380
- const splitName = tableName.replace(/_/g, '/');
381
- const SplitName = tableName.replace(/_/, '/').replace(/_(\w)/g, (a: string, b: string) => b.toUpperCase());
382
- const data = {
383
- title,
384
-
385
- tableName,
386
- className,
387
- ClassName,
388
- vueName,
389
- splitVueName,
390
- splitName,
391
- SplitName,
392
-
393
- columns,
394
-
395
- column_names: columns?.map(i => i.name),
396
- ColumnNames: columns?.map(i => i.Name),
397
-
398
- column_names_join: columns?.map(i => i.name).join(','),
399
- ColumnNames_join: columns?.map(i => `${i.name} ${i.Name}`).join(','),
400
-
401
- column_names_joinT: columns?.map(i => `t.${i.name}`).join(','),
402
- ColumnNames_joinT: columns?.map(i => `t.${i.name} ${i.Name}`).join(','),
403
-
404
- columns_no_id: columns?.filter(i => !i.id),
405
-
406
- column_names_no_id: columns?.filter(i => !i.id).map(i => i.name),
407
- ColumnNames_no_id: columns?.filter(i => !i.id).map(i => i.Name),
408
-
409
- column_names_no_id_join: columns?.filter(i => !i.id).map(i => i.name).join(','),
410
- ColumnNames_no_id_join: columns?.filter(i => !i.id).map(i => i.Name).join(','),
411
-
412
- columns_no_skip: columns?.filter(i => !configData.skipColumns.includes(i.name) && !i.id),
413
-
414
- column_names_no_skip: columns?.filter(i => !configData.skipColumns.includes(i.name) && !i.id).map(i => i.name),
415
- ColumnNames_no_skip: columns?.filter(i => !configData.skipColumns.includes(i.name) && !i.id).map(i => i.Name),
416
-
417
- column_names_no_skip_join: columns?.filter(i => !configData.skipColumns.includes(i.name) && !i.id).map(i => i.name).join(','),
418
- ColumnNames_no_skip_join: columns?.filter(i => !configData.skipColumns.includes(i.name) && !i.id).map(i => i.Name).join(','),
419
-
420
- ids: columns?.filter(i => i.id),
421
-
422
- id_names: columns?.filter(i => i.id).map(i => i.name),
423
- IdNames: columns?.filter(i => i.id).map(i => i.Name),
424
-
425
- id_names_join: columns?.filter(i => i.id).map(i => i.name).join(','),
426
- IdNames_join: columns?.filter(i => i.id).map(i => i.Name).join(','),
427
-
428
- modelName,
429
- modelPath,
430
-
431
- logicDelete
432
- };
433
- const template = templates[command];
434
- if (!template) {
435
- error(`[错误] ${command} 未定义模板!`);
436
- return;
437
- }
438
- const txt = mustache.render(template, data, {}, ['<%', '%>']);
439
- const _fileName = configData.command[command]!.replace(/{([a-zA-Z]+)}/g, (a: string, b: string) => data[b]);
440
- const fileNames = _fileName.split(',');
441
- for (const fileName of fileNames) {
442
- const filePath = path.join(basepath, fileName);
443
- const dirname = path.dirname(filePath);
444
- mkdir('-p', dirname);
445
- try {
446
- fs.statSync(filePath);
447
- if (force === false) {
448
- warn(`[跳过] ${filePath}`);
449
- return;
450
- } else {
451
- warn(`[覆盖] ${filePath}`);
452
- }
453
- } catch (error) {
454
- info(`[生成] ${filePath}`);
455
- }
456
- if (configData.output) {
457
- outputs.add(configData.output.replace(/{([a-zA-Z]+)}/g, (a: string, b: string) => data[b]));
458
- }
459
- fs.writeFileSync(path.join(basepath, fileName), txt);
460
- }
461
- }
462
- }
463
- const replServer = start();
464
- function defineCommand(command: string, comands?: string[]) {
465
- if (comands) {
466
- log(`\x1b[42m\x1b[30m[组合]${command}>${comands.join(',')}注册成功\x1b[0m`);
467
- } else {
468
- log(`\x1b[42m\x1b[30m[命令]${command}注册成功\x1b[0m`);
469
- }
470
- if (comands) {
471
- replServer.defineCommand(command, async input => {
472
- outputs.clear();
473
- const inputs = input.match(/([^:]+):{0,1}([a-zA-Z0-9]*)/);
474
- if (inputs?.length !== 3) {
475
- return error(`[错误]命令格式应为: table1,table2[:模块名]`);
476
- }
477
- const [_, tables, modelName] = inputs;
478
- for (const c of comands) {
479
- await excute(c, tables!, modelName ?? '');
480
- }
481
- info('执行完毕!下面打印生成的输出');
482
- info(Array.from(outputs).join(','));
483
- });
484
- } else {
485
- replServer.defineCommand(command, async input => {
486
- outputs.clear();
487
- const inputs = input.match(/([^:]+):{0,1}([a-zA-Z0-9]*)/);
488
- if (inputs?.length !== 3) {
489
- return error(`[错误]命令格式应为: table1,table2[:模块名]`);
490
- }
491
- const [_, tables, modelName] = inputs;
492
- await excute(command, tables!, modelName ?? '');
493
- info('执行完毕!下面打印生成的输出');
494
- info(Array.from(outputs).join(','));
495
- });
496
- }
497
- }
498
- replServer.defineCommand('force', () => {
499
- force = !force;
500
- log(force ? '覆盖生成' : '不覆盖生成!');
501
- });
502
- replServer.defineCommand('help1', help1);
503
- replServer.defineCommand('help2', help2);
504
- if (configData.command) {
505
- for (const command of Object.keys(configData.command)) {
506
- if (!templates[command]) {
507
- error(`命令:${command}没有定义模板,该命令不会生效`);
508
- } else {
509
- defineCommand(command);
510
- }
511
- }
512
- }
513
- if (configData.commands) {
514
- for (const [command, commands] of Object.entries(configData.commands)) {
515
- const keys = commands as string[];
516
- const e = keys.filter(k => !templates[k]).join(',');
517
- if (e) {
518
- error(`组合命令:${command}定义了${commands},但是${e}没有定义模板,该命令不会生效`);
519
- } else {
520
- defineCommand(command, keys);
521
- }
522
- }
523
- }
524
- } catch (error) {
525
- error(error);
526
- }
283
+ `, [configData.database, tableName]);
284
+ let logicDelete = '';
285
+ const columns = result.map(r => {
286
+ if (r.id === 1) {
287
+ r.id = true;
288
+ }
289
+ else
290
+ delete r.id;
291
+ if (r.notNull === 1) {
292
+ r.notNull = true;
293
+ }
294
+ else
295
+ delete r.notNull;
296
+ const fields = new Array(`type:SqlType.${r.type}`);
297
+ r.Type = lxMap[r.type];
298
+ if (r.length !== null) {
299
+ fields.push(`length:${r.length}`);
300
+ }
301
+ if (r.scale !== null) {
302
+ fields.push(`scale:${r.scale}`);
303
+ }
304
+ if (r.def !== null) {
305
+ r.def ?? (r.def = '');
306
+ if (r.def !== '') {
307
+ if (isNaN(r.def)) {
308
+ fields.push(`def:'${r.def}'`);
309
+ }
310
+ else {
311
+ fields.push(`def:${r.def}`);
312
+ }
313
+ }
314
+ }
315
+ if (r.Type === 'string') {
316
+ if (r.def === '' || r.def === null) {
317
+ r.def = "''";
318
+ }
319
+ else {
320
+ r.def = `'${r.def}'`;
321
+ }
322
+ }
323
+ else if (r.Type === 'number') {
324
+ r.def = 0;
325
+ }
326
+ else if (r.Type === 'Date') {
327
+ r.def = 'new Date()';
328
+ }
329
+ else {
330
+ r.def = 'null';
331
+ }
332
+ if (r.id === true) {
333
+ fields.push(`id:true`);
334
+ }
335
+ if (r.notNull === true) {
336
+ fields.push(`notNull:true`);
337
+ }
338
+ if (r.id === true && configData.id) {
339
+ fields.push(configData.id);
340
+ }
341
+ if (r.name === configData.logicDeleteK) {
342
+ fields.push(`logicDelete: ${configData.logicDeleteV}`);
343
+ logicDelete = `AND ${r.name} = ${configData.NotlogicDeleteV}`;
344
+ }
345
+ if (r.comment) {
346
+ fields.push(`comment: '${r.comment}'`);
347
+ }
348
+ r.comment = r.comment ?? '';
349
+ r.Name = r.name.replace(/_(\w)/g, (a, b) => b.toUpperCase());
350
+ r.NAME = r.Name.replace(/\w/, (v) => v.toUpperCase());
351
+ r.Field = `@Field({${fields.join(',')}})`;
352
+ r.JSField_name = `{${fields.join(',')}, P: '${r.name}'}`;
353
+ r.JSFieldName = `{${fields.join(',')}, P: '${r.Name}'}`;
354
+ if (configData.custom) {
355
+ r.custom = configData.custom(r.name);
356
+ }
357
+ return r;
358
+ });
359
+ conn.release();
360
+ return { columns, logicDelete };
361
+ }
362
+ async function excute(command, input, modelName) {
363
+ const tables = await getTables(input);
364
+ if (input !== '.') {
365
+ const checkTable = input.split(/,|\s/).map(r => r.trim());
366
+ if (checkTable.length !== tables.length) {
367
+ error(`[错误] 输入的表与数据库查询返回表不符,数据库返回${tables.length}个:${tables.map(i => i.tableName).join(',')}`);
368
+ return;
369
+ }
370
+ }
371
+ modelName ?? (modelName = '');
372
+ const modelPath = modelName ? `/${modelName}/` : '';
373
+ for (const { tableName, title } of tables) {
374
+ const { columns, logicDelete } = await getColumns(tableName);
375
+ const className = tableName.replace(/_(\w)/g, (a, b) => b.toUpperCase());
376
+ const ClassName = className.replace(/\w/, (v) => v.toUpperCase());
377
+ const vueName = tableName.replace(/_/g, '-');
378
+ const splitVueName = tableName.replace(/_/, '/').replace(/_/g, '-');
379
+ const modelName = tableName.substring(0, tableName.indexOf('_'));
380
+ const splitName = tableName.replace(/_/g, '/');
381
+ const SplitName = tableName.replace(/_/, '/').replace(/_(\w)/g, (a, b) => b.toUpperCase());
382
+ const data = {
383
+ title,
384
+ tableName,
385
+ className,
386
+ ClassName,
387
+ vueName,
388
+ splitVueName,
389
+ splitName,
390
+ SplitName,
391
+ columns,
392
+ column_names: columns?.map(i => i.name),
393
+ ColumnNames: columns?.map(i => i.Name),
394
+ column_names_join: columns?.map(i => i.name).join(','),
395
+ ColumnNames_join: columns?.map(i => `${i.name} ${i.Name}`).join(','),
396
+ column_names_joinT: columns?.map(i => `t.${i.name}`).join(','),
397
+ ColumnNames_joinT: columns?.map(i => `t.${i.name} ${i.Name}`).join(','),
398
+ columns_no_id: columns?.filter(i => !i.id),
399
+ column_names_no_id: columns?.filter(i => !i.id).map(i => i.name),
400
+ ColumnNames_no_id: columns?.filter(i => !i.id).map(i => i.Name),
401
+ column_names_no_id_join: columns?.filter(i => !i.id).map(i => i.name).join(','),
402
+ ColumnNames_no_id_join: columns?.filter(i => !i.id).map(i => i.Name).join(','),
403
+ columns_no_skip: columns?.filter(i => !configData.skipColumns.includes(i.name) && !i.id),
404
+ column_names_no_skip: columns?.filter(i => !configData.skipColumns.includes(i.name) && !i.id).map(i => i.name),
405
+ ColumnNames_no_skip: columns?.filter(i => !configData.skipColumns.includes(i.name) && !i.id).map(i => i.Name),
406
+ column_names_no_skip_join: columns?.filter(i => !configData.skipColumns.includes(i.name) && !i.id).map(i => i.name).join(','),
407
+ ColumnNames_no_skip_join: columns?.filter(i => !configData.skipColumns.includes(i.name) && !i.id).map(i => i.Name).join(','),
408
+ ids: columns?.filter(i => i.id),
409
+ id_names: columns?.filter(i => i.id).map(i => i.name),
410
+ IdNames: columns?.filter(i => i.id).map(i => i.Name),
411
+ id_names_join: columns?.filter(i => i.id).map(i => i.name).join(','),
412
+ IdNames_join: columns?.filter(i => i.id).map(i => i.Name).join(','),
413
+ modelName,
414
+ modelPath,
415
+ logicDelete
416
+ };
417
+ const template = templates[command];
418
+ if (!template) {
419
+ error(`[错误] ${command} 未定义模板!`);
420
+ return;
421
+ }
422
+ const txt = mustache.render(template, data, {}, ['<%', '%>']);
423
+ const _fileName = configData.command[command].replace(/{([a-zA-Z]+)}/g, (a, b) => data[b]);
424
+ const fileNames = _fileName.split(',');
425
+ for (const fileName of fileNames) {
426
+ const filePath = path.join(basepath, fileName);
427
+ const dirname = path.dirname(filePath);
428
+ mkdir('-p', dirname);
429
+ try {
430
+ fs.statSync(filePath);
431
+ if (force === false) {
432
+ warn(`[跳过] ${filePath}`);
433
+ return;
434
+ }
435
+ else {
436
+ warn(`[覆盖] ${filePath}`);
437
+ }
438
+ }
439
+ catch (error) {
440
+ info(`[生成] ${filePath}`);
441
+ }
442
+ if (configData.output) {
443
+ outputs.add(configData.output.replace(/{([a-zA-Z]+)}/g, (a, b) => data[b]));
444
+ }
445
+ fs.writeFileSync(path.join(basepath, fileName), txt);
446
+ }
447
+ }
448
+ }
449
+ const replServer = start();
450
+ function defineCommand(command, comands) {
451
+ if (comands) {
452
+ log(`\x1b[42m\x1b[30m[组合]${command}>${comands.join(',')}注册成功\x1b[0m`);
453
+ }
454
+ else {
455
+ log(`\x1b[42m\x1b[30m[命令]${command}注册成功\x1b[0m`);
456
+ }
457
+ if (comands) {
458
+ replServer.defineCommand(command, async (input) => {
459
+ outputs.clear();
460
+ const inputs = input.match(/([^:]+):{0,1}([a-zA-Z0-9]*)/);
461
+ if (inputs?.length !== 3) {
462
+ return error(`[错误]命令格式应为: table1,table2[:模块名]`);
463
+ }
464
+ const [_, tables, modelName] = inputs;
465
+ for (const c of comands) {
466
+ await excute(c, tables, modelName ?? '');
467
+ }
468
+ info('执行完毕!下面打印生成的输出');
469
+ info(Array.from(outputs).join(','));
470
+ });
471
+ }
472
+ else {
473
+ replServer.defineCommand(command, async (input) => {
474
+ outputs.clear();
475
+ const inputs = input.match(/([^:]+):{0,1}([a-zA-Z0-9]*)/);
476
+ if (inputs?.length !== 3) {
477
+ return error(`[错误]命令格式应为: table1,table2[:模块名]`);
478
+ }
479
+ const [_, tables, modelName] = inputs;
480
+ await excute(command, tables, modelName ?? '');
481
+ info('执行完毕!下面打印生成的输出');
482
+ info(Array.from(outputs).join(','));
483
+ });
484
+ }
485
+ }
486
+ replServer.defineCommand('force', () => {
487
+ force = !force;
488
+ log(force ? '覆盖生成' : '不覆盖生成!');
489
+ });
490
+ replServer.defineCommand('help1', help1);
491
+ replServer.defineCommand('help2', help2);
492
+ if (configData.command) {
493
+ for (const command of Object.keys(configData.command)) {
494
+ if (!templates[command]) {
495
+ error(`命令:${command}没有定义模板,该命令不会生效`);
496
+ }
497
+ else {
498
+ defineCommand(command);
499
+ }
500
+ }
501
+ }
502
+ if (configData.commands) {
503
+ for (const [command, commands] of Object.entries(configData.commands)) {
504
+ const keys = commands;
505
+ const e = keys.filter(k => !templates[k]).join(',');
506
+ if (e) {
507
+ error(`组合命令:${command}定义了${commands},但是${e}没有定义模板,该命令不会生效`);
508
+ }
509
+ else {
510
+ defineCommand(command, keys);
511
+ }
512
+ }
513
+ }
514
+ }
515
+ catch (error) {
516
+ error(error);
517
+ }