baja-lite 1.0.4 → 1.0.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 (85) hide show
  1. package/cjs/boot-remote.d.ts +2 -0
  2. package/cjs/boot-remote.js +35 -0
  3. package/cjs/boot.d.ts +2 -0
  4. package/cjs/boot.js +152 -0
  5. package/cjs/code.d.ts +1 -0
  6. package/cjs/code.js +359 -1
  7. package/cjs/convert-xml.d.ts +10 -0
  8. package/cjs/convert-xml.js +414 -0
  9. package/cjs/enum.d.ts +10 -0
  10. package/cjs/enum.js +32 -0
  11. package/cjs/error.js +1 -1
  12. package/cjs/fn.js +3 -3
  13. package/cjs/index.d.ts +3 -0
  14. package/cjs/index.js +3 -0
  15. package/cjs/list.d.ts +10 -0
  16. package/cjs/list.js +36 -0
  17. package/cjs/object.d.ts +7 -1
  18. package/cjs/object.js +36 -2
  19. package/cjs/set-ex.d.ts +41 -15
  20. package/cjs/set-ex.js +68 -52
  21. package/cjs/sql.d.ts +760 -305
  22. package/cjs/sql.js +1702 -1041
  23. package/cjs/sqlite.d.ts +38 -0
  24. package/cjs/sqlite.js +194 -0
  25. package/cjs/test-mysql.d.ts +2 -1
  26. package/cjs/test-mysql.js +85 -63
  27. package/cjs/test-sqlite.d.ts +1 -1
  28. package/cjs/test-sqlite.js +3 -1
  29. package/cjs/test-xml.d.ts +1 -0
  30. package/cjs/test-xml.js +75 -0
  31. package/es/boot-remote.d.ts +2 -0
  32. package/es/boot-remote.js +31 -0
  33. package/es/boot.d.ts +2 -0
  34. package/es/boot.js +125 -0
  35. package/es/code.d.ts +1 -0
  36. package/es/code.js +355 -2
  37. package/es/convert-xml.d.ts +10 -0
  38. package/es/convert-xml.js +410 -0
  39. package/es/enum.d.ts +10 -0
  40. package/es/enum.js +28 -0
  41. package/es/error.js +1 -1
  42. package/es/index.d.ts +3 -0
  43. package/es/index.js +3 -0
  44. package/es/list.d.ts +10 -0
  45. package/es/list.js +32 -0
  46. package/es/object.d.ts +7 -1
  47. package/es/object.js +28 -1
  48. package/es/set-ex.d.ts +41 -15
  49. package/es/set-ex.js +68 -52
  50. package/es/sql.d.ts +760 -305
  51. package/es/sql.js +1573 -917
  52. package/es/sqlite.d.ts +38 -0
  53. package/es/sqlite.js +164 -0
  54. package/es/test-mysql.d.ts +2 -1
  55. package/es/test-mysql.js +85 -64
  56. package/es/test-sqlite.d.ts +1 -1
  57. package/es/test-sqlite.js +3 -1
  58. package/es/test-xml.d.ts +1 -0
  59. package/es/test-xml.js +70 -0
  60. package/package.json +15 -10
  61. package/src/boot-remote.ts +31 -0
  62. package/src/boot.ts +129 -0
  63. package/src/code.ts +342 -1
  64. package/src/convert-xml.ts +462 -0
  65. package/src/enum.ts +31 -0
  66. package/src/error.ts +1 -1
  67. package/src/index.ts +4 -1
  68. package/src/list.ts +31 -0
  69. package/src/object.ts +48 -14
  70. package/src/set-ex.ts +91 -70
  71. package/src/sql.ts +1652 -965
  72. package/src/sqlite.ts +161 -0
  73. package/src/test-mysql.ts +93 -65
  74. package/src/test-sqlite.ts +3 -1
  75. package/src/test-xml.ts +70 -0
  76. package/cjs/constant.d.ts +0 -13
  77. package/cjs/constant.js +0 -19
  78. package/cjs/redis.d.ts +0 -0
  79. package/cjs/redis.js +0 -1
  80. package/es/constant.d.ts +0 -13
  81. package/es/constant.js +0 -16
  82. package/es/redis.d.ts +0 -0
  83. package/es/redis.js +0 -1
  84. package/src/constant.ts +0 -14
  85. package/src/redis.ts +0 -0
@@ -0,0 +1,2 @@
1
+ import { GlobalSqlOptionForWeb } from './sql';
2
+ export declare const BootRomote: (options: GlobalSqlOptionForWeb) => Promise<void>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BootRomote = void 0;
4
+ const sql_1 = require("./sql");
5
+ const BootRomote = async function (options) {
6
+ globalThis[sql_1._GlobalSqlOption] = Object.assign({}, sql_1._defOption, options);
7
+ globalThis[sql_1._Hump] = globalThis[sql_1._GlobalSqlOption].columnMode === sql_1.ColumnMode.HUMP;
8
+ sql_1.logger.level = options.log ?? 'info';
9
+ globalThis[sql_1._sqlCache] = new sql_1.SqlCache();
10
+ if (options.sqlMap) {
11
+ await globalThis[sql_1._sqlCache].init(options);
12
+ }
13
+ globalThis[sql_1._dao] = {
14
+ [sql_1.DBType.SqliteRemote]: {},
15
+ };
16
+ if (options.SqliteRemote) {
17
+ if (typeof options.SqliteRemote.db === 'string') {
18
+ await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
19
+ globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][sql_1._primaryDB] = new sql_1.SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
20
+ }
21
+ else {
22
+ let flag = false;
23
+ for (const [key, fileName] of Object.entries(options.SqliteRemote.db)) {
24
+ await options.SqliteRemote.service.initDB(fileName);
25
+ const db = new sql_1.SqliteRemote(options.SqliteRemote.service, fileName);
26
+ if (flag === false) {
27
+ globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][sql_1._primaryDB] = db;
28
+ flag = true;
29
+ }
30
+ globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][key] = db;
31
+ }
32
+ }
33
+ }
34
+ };
35
+ exports.BootRomote = BootRomote;
package/cjs/boot.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { GlobalSqlOption } from './sql';
2
+ export declare const Boot: (options: GlobalSqlOption) => Promise<void>;
package/cjs/boot.js ADDED
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.Boot = void 0;
27
+ const sql_1 = require("./sql");
28
+ const Boot = async function (options) {
29
+ globalThis[sql_1._GlobalSqlOption] = Object.assign({}, sql_1._defOption, options);
30
+ globalThis[sql_1._Hump] = globalThis[sql_1._GlobalSqlOption].columnMode === sql_1.ColumnMode.HUMP;
31
+ if (options.sqlDir) {
32
+ globalThis[sql_1._path] = await Promise.resolve().then(() => __importStar(require('path')));
33
+ globalThis[sql_1._fs] = await Promise.resolve().then(() => __importStar(require('fs')));
34
+ }
35
+ sql_1.logger.level = options.log ?? 'info';
36
+ globalThis[sql_1._sqlCache] = new sql_1.SqlCache();
37
+ if (options.sqlMap || options.sqlDir) {
38
+ await globalThis[sql_1._sqlCache].init(options);
39
+ }
40
+ globalThis[sql_1._dao] = {
41
+ [sql_1.DBType.Mongo]: {},
42
+ [sql_1.DBType.Mysql]: {},
43
+ [sql_1.DBType.Sqlite]: {},
44
+ [sql_1.DBType.SqliteRemote]: {},
45
+ [sql_1.DBType.Redis]: {}
46
+ };
47
+ if (options.Mysql) {
48
+ const { createPool } = await Promise.resolve().then(() => __importStar(require('mysql2/promise')));
49
+ if (options.Mysql['host']) {
50
+ globalThis[sql_1._dao][sql_1.DBType.Mysql][sql_1._primaryDB] = new sql_1.Mysql(createPool({
51
+ ...options.Mysql,
52
+ multipleStatements: true,
53
+ decimalNumbers: true,
54
+ supportBigNumbers: true
55
+ }));
56
+ }
57
+ else {
58
+ let flag = false;
59
+ for (const [key, option] of Object.entries(options.Mysql)) {
60
+ const db = new sql_1.Mysql(createPool({
61
+ ...option,
62
+ multipleStatements: true,
63
+ decimalNumbers: true,
64
+ supportBigNumbers: true
65
+ }));
66
+ if (flag === false) {
67
+ globalThis[sql_1._dao][sql_1.DBType.Mysql][sql_1._primaryDB] = db;
68
+ flag = true;
69
+ }
70
+ globalThis[sql_1._dao][sql_1.DBType.Mysql][key] = db;
71
+ }
72
+ }
73
+ }
74
+ if (options.Sqlite) {
75
+ const BetterSqlite3 = await Promise.resolve().then(() => __importStar(require('better-sqlite3')));
76
+ if (typeof options.Sqlite === 'string') {
77
+ globalThis[sql_1._dao][sql_1.DBType.Sqlite][sql_1._primaryDB] = new sql_1.Sqlite(new BetterSqlite3.default(options.Sqlite, { fileMustExist: false }));
78
+ }
79
+ else {
80
+ let flag = false;
81
+ for (const [key, fileName] of Object.entries(options.Sqlite)) {
82
+ const db = new sql_1.Sqlite(new BetterSqlite3.default(fileName, { fileMustExist: false }));
83
+ if (flag === false) {
84
+ globalThis[sql_1._dao][sql_1.DBType.Sqlite][sql_1._primaryDB] = db;
85
+ flag = true;
86
+ }
87
+ globalThis[sql_1._dao][sql_1.DBType.Sqlite][key] = db;
88
+ }
89
+ }
90
+ }
91
+ if (options.SqliteRemote) {
92
+ if (typeof options.SqliteRemote.db === 'string') {
93
+ await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
94
+ globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][sql_1._primaryDB] = new sql_1.SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
95
+ }
96
+ else {
97
+ let flag = false;
98
+ for (const [key, fileName] of Object.entries(options.SqliteRemote.db)) {
99
+ await options.SqliteRemote.service.initDB(fileName);
100
+ const db = new sql_1.SqliteRemote(options.SqliteRemote.service, fileName);
101
+ if (flag === false) {
102
+ globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][sql_1._primaryDB] = db;
103
+ flag = true;
104
+ }
105
+ globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][key] = db;
106
+ }
107
+ }
108
+ }
109
+ if (options.Redis) {
110
+ const { Redis } = await Promise.resolve().then(() => __importStar(require('ioredis')));
111
+ if (options.Redis['host']) {
112
+ globalThis[sql_1._dao][sql_1.DBType.Redis][sql_1._primaryDB] = new Redis(options.Redis);
113
+ }
114
+ else {
115
+ let flag = false;
116
+ for (const [key, option] of Object.entries(options.Redis)) {
117
+ const db = new Redis(option);
118
+ if (flag === false) {
119
+ globalThis[sql_1._dao][sql_1.DBType.Redis][sql_1._primaryDB] = db;
120
+ flag = true;
121
+ }
122
+ globalThis[sql_1._dao][sql_1.DBType.Redis][key] = db;
123
+ }
124
+ }
125
+ const clients = Object.values(globalThis[sql_1._dao][sql_1.DBType.Redis]);
126
+ const Redlock = await Promise.resolve().then(() => __importStar(require('redlock')));
127
+ globalThis[sql_1._dao][sql_1.DBType.RedisLock] = new Redlock.default(clients, {
128
+ // The expected clock drift; for more details see:
129
+ // http://redis.io/topics/distlock
130
+ driftFactor: 0.01, // multiplied by lock ttl to determine drift time
131
+ // The max number of times Redlock will attempt to lock a resource
132
+ // before erroring.
133
+ retryCount: 10,
134
+ // the time in ms between attempts
135
+ retryDelay: 200, // time in ms
136
+ // the max time in ms randomly added to retries
137
+ // to improve performance under high contention
138
+ // see https://www.awsarchitectureblog.com/2015/03/backoff.html
139
+ retryJitter: 200, // time in ms
140
+ // The minimum remaining time on a lock before an extension is automatically
141
+ // attempted with the `using` API.
142
+ automaticExtensionThreshold: 500, // time in ms
143
+ });
144
+ const { EventEmitter } = await Promise.resolve().then(() => __importStar(require('events')));
145
+ const event = new EventEmitter({ captureRejections: true });
146
+ event.on('error', error => {
147
+ sql_1.logger.error('event-bus', error);
148
+ });
149
+ globalThis[sql_1._EventBus] = event;
150
+ }
151
+ };
152
+ exports.Boot = Boot;
package/cjs/code.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  #!/usr/bin/env node
2
+ export {};
package/cjs/code.js CHANGED
@@ -1,3 +1,361 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
- console.log(__dirname);
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ const path_1 = __importDefault(require("path"));
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const repl_1 = require("repl");
10
+ const promise_1 = require("mysql2/promise");
11
+ const mustache_1 = __importDefault(require("mustache"));
12
+ const lxMap = {
13
+ tinyint: "number",
14
+ smallint: "number",
15
+ mediumint: "number",
16
+ int: "number",
17
+ integer: "number",
18
+ bigint: "BigInt",
19
+ bit: "boolean",
20
+ double: "number",
21
+ real: "number",
22
+ float: "number",
23
+ decimal: "number",
24
+ numeric: "number",
25
+ char: "string",
26
+ varchar: "string",
27
+ date: "Date",
28
+ time: "string",
29
+ year: "string",
30
+ timestamp: "BigInt",
31
+ datetime: "Date",
32
+ tinyblob: "string",
33
+ blob: "string",
34
+ mediumblob: "string",
35
+ longblob: "string",
36
+ tinytext: "string",
37
+ text: "string",
38
+ mediumtext: "string",
39
+ longtext: "string",
40
+ enum: "string",
41
+ set: "string",
42
+ binary: "string",
43
+ varbinary: "string",
44
+ point: "Object",
45
+ linestring: "Object",
46
+ polygon: "Object",
47
+ geometry: "string",
48
+ multipoint: "Object",
49
+ multilinestring: "Object",
50
+ multipolygon: "Object",
51
+ geometrycollection: "Object"
52
+ };
53
+ let force = false;
54
+ const basepath = path_1.default.join(__dirname, '..', '..', '..');
55
+ const config = path_1.default.join(basepath, 'baja.code.json');
56
+ const templatePath = path_1.default.join(basepath, 'code-template');
57
+ console.log(`
58
+ **********************-----------
59
+ 配置文件:
60
+ 请在项目根目录添加文件:baja.code.json!配置如下:
61
+ {
62
+ "host": "",
63
+ "port": "",
64
+ "user": "",
65
+ "password": "",
66
+ "database": "",
67
+
68
+ "command": {
69
+ "entity": "src/{vueName}/{vueName}.entity.ts",
70
+ "controller": "src/{vueName}/{vueName}.controller.ts",
71
+ "service": "src/{vueName}/{vueName}.service.ts",
72
+ "sql": "src/sql/{vueName}.mu",
73
+ "module": "src/{vueName}/{vueName}.module.ts"
74
+ },
75
+ "commands": {
76
+ "s": ["entity", "controller", "service", "sql", "module"]
77
+ }
78
+ "commands": {
79
+ "s": ["entity", "controller", "service", "sql", "module"]
80
+ },
81
+ "output": "{ClassName}Module",
82
+ "id"? : "uuidShort: true, notNull: true, uuid: true"
83
+ }
84
+ command是生成命令,这里声明命令同时定义文件生成路径:可用下面的变量替换.同时必须有同名模板.
85
+ 路径是相对于项目根目录的
86
+ commands是组合命令,上面表示输入s,同时生成:"entity", "controller", "service", "sql", "module"
87
+ output 是生成后打印出的内容
88
+ 模板转义:<%& 变量 %>
89
+ **********************-----------
90
+ **********************-----------
91
+ **********************-----------
92
+ 模板文件
93
+ 请在项目根目录的code-template添加模板文件, 按照mustache(标签定义成了[ '<%', '%>' ])进行格式化,变量:
94
+ title,
95
+
96
+ tableName, event_main_info
97
+ className, eventMainInfo
98
+ ClassName, EventMainInfo
99
+ vueName, event-main-info
100
+ splitName, event/main/info
101
+ SplitName, event/mainInfo
102
+
103
+ columns: [
104
+ comment:
105
+ name: sku_id
106
+ Name: skuId
107
+ NAME: SkuId
108
+ Field
109
+ Type
110
+ ], Field(类型string),表示字段的注解, Type 表示JS类型
111
+ columnNames_join: 'sku_id, sku_name'
112
+ ColumnNames_join: 'sku_id skuId, sku_name skuName'
113
+
114
+ columns_no_id: [], 同columns,没有ID而已
115
+ columnNames_no_id: [],
116
+
117
+ ids: [], 参见 FieldOption,多一个参数Field,表示字段的注解, Type 表示JS类型
118
+ idNames: [], 主键字段数组
119
+ idNames_join: []
120
+
121
+ modelName: 模块名称,可能为空字符串
122
+ modelPath: 模块名称实际就影响访问路径,所以这里会直接拼好controller的模块访问路径,如果模块为空,则该属性就是空字符串,否则是 /模块名称/
123
+ -----
124
+ 命令 table1,table2,table3:模块名称
125
+ table=. 表示扫描全库表
126
+ :模块名称 可以省略
127
+ -----
128
+ force: 切换是否覆盖
129
+ `);
130
+ try {
131
+ const outputs = new Set();
132
+ const _configData = fs_1.default.readFileSync(config, { encoding: 'utf-8' }).toString();
133
+ const configData = JSON.parse(_configData);
134
+ const templates = Object.fromEntries(fs_1.default.readdirSync(templatePath).map(r => [path_1.default.basename(r, '.mu'), fs_1.default.readFileSync(path_1.default.join(templatePath, r), { encoding: 'utf-8' }).toString()]));
135
+ const pool = (0, promise_1.createPool)({
136
+ host: configData.host,
137
+ port: configData.port,
138
+ user: configData.user,
139
+ password: configData.password,
140
+ database: configData.database
141
+ });
142
+ async function getTables(tableName) {
143
+ const conn = await pool.getConnection();
144
+ const params = [configData.database];
145
+ let sql = `
146
+ SELECT TABLE_NAME tableName, IFNULL(TABLE_COMMENT, TABLE_NAME) title FROM information_schema.TABLES
147
+ WHERE TABLE_SCHEMA= ? AND TABLE_TYPE = 'BASE TABLE'`;
148
+ if (tableName !== '.') {
149
+ sql += ` AND TABLE_NAME IN (?)`;
150
+ params.push(tableName.split(/,|\s/).map(r => r.trim()));
151
+ }
152
+ const [result] = await conn.query(sql, params);
153
+ conn.release();
154
+ return result;
155
+ }
156
+ async function getColumns(tableName) {
157
+ const conn = await pool.getConnection();
158
+ const [result] = await conn.query(`
159
+ SELECT
160
+ DATA_TYPE type,
161
+ COLUMN_NAME \`name\`,
162
+ IFNULL(IFNULL(CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION),DATETIME_PRECISION) \`length\`,
163
+ NUMERIC_SCALE scale,
164
+ COLUMN_DEFAULT def,
165
+ IF(COLUMN_KEY = 'PRI', 1, 0) id,
166
+ IF(IS_NULLABLE = 'NO', 1, 0) notNull,
167
+ COLUMN_COMMENT \`comment\`
168
+ FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=? AND TABLE_NAME = ?;
169
+ `, [configData.database, tableName]);
170
+ const columns = result.map(r => {
171
+ if (r.id === 1) {
172
+ r.id = true;
173
+ }
174
+ else
175
+ delete r.id;
176
+ if (r.notNull === 1) {
177
+ r.notNull = true;
178
+ }
179
+ else
180
+ delete r.notNull;
181
+ r.comment = `* ${r.comment ?? ''}(\`${tableName}.${r.name}\`)`;
182
+ const fields = new Array(`type:SqlType.${r.type}`);
183
+ if (r.length !== null) {
184
+ fields.push(`length:${r.length}`);
185
+ }
186
+ if (r.scale !== null) {
187
+ fields.push(`scale:${r.scale}`);
188
+ }
189
+ if (r.def !== null) {
190
+ r.def ?? (r.def = '');
191
+ if (isNaN(r.def) || r.def === '') {
192
+ fields.push(`def:'${r.def}'`);
193
+ }
194
+ else {
195
+ fields.push(`def:${r.def}`);
196
+ }
197
+ }
198
+ if (r.id === true) {
199
+ fields.push(`id:true`);
200
+ }
201
+ if (r.notNull === true) {
202
+ fields.push(`notNull:true`);
203
+ }
204
+ if (r.id === true && configData.id) {
205
+ fields.push(configData.id);
206
+ }
207
+ r.Type = lxMap[r.type];
208
+ r.Field = `@Field({${fields.join(',')}})`;
209
+ r.Name = r.name.replace(/_(\w)/g, (a, b) => b.toUpperCase());
210
+ r.NAME = r.Name.replace(/\w/, (v) => v.toUpperCase());
211
+ return r;
212
+ });
213
+ conn.release();
214
+ return columns;
215
+ }
216
+ async function excute(command, input, modelName) {
217
+ const tables = await getTables(input);
218
+ if (input !== '.') {
219
+ const checkTable = input.split(/,|\s/).map(r => r.trim());
220
+ if (checkTable.length !== tables.length) {
221
+ console.error(`[错误] 输入的表与数据库查询返回表不符,数据库返回${tables.length}个:${tables.map(i => i.tableName).join(',')}`);
222
+ return;
223
+ }
224
+ }
225
+ modelName ?? (modelName = '');
226
+ const modelPath = modelName ? `/${modelName}/` : '';
227
+ for (const { tableName, title } of tables) {
228
+ const columns = await getColumns(tableName);
229
+ const className = tableName.replace(/_(\w)/g, (a, b) => b.toUpperCase());
230
+ const ClassName = className.replace(/\w/, (v) => v.toUpperCase());
231
+ const vueName = tableName.replace(/_/g, '-');
232
+ const splitName = tableName.replace(/_/g, '/');
233
+ const SplitName = tableName.replace(/_/, '/').replace(/_(\w)/g, (a, b) => b.toUpperCase());
234
+ const data = {
235
+ title,
236
+ tableName,
237
+ className,
238
+ ClassName,
239
+ vueName,
240
+ splitName,
241
+ SplitName,
242
+ columns,
243
+ columnNames: columns?.map(i => i.name),
244
+ ColumnNames: columns?.map(i => i.Name),
245
+ columnNames_join: columns?.map(i => i.name).join(','),
246
+ ColumnNames_join: columns?.map(i => `${i.name} ${i.Name}`).join(','),
247
+ columns_no_id: columns?.filter(i => !i.id),
248
+ columnNames_no_id: columns?.filter(i => !i.id).map(i => i.name),
249
+ ColumnNames_no_id: columns?.filter(i => !i.id).map(i => i.Name),
250
+ columnNames_no_id_join: columns?.filter(i => !i.id).map(i => i.name).join(','),
251
+ ColumnNames_no_id_join: columns?.filter(i => !i.id).map(i => i.Name).join(','),
252
+ ids: columns?.filter(i => i.id),
253
+ idNames: columns?.filter(i => i.id).map(i => i.name),
254
+ IdNames: columns?.filter(i => i.id).map(i => i.Name),
255
+ idNames_join: columns?.filter(i => i.id).map(i => i.name).join(','),
256
+ IdNames_join: columns?.filter(i => i.id).map(i => i.Name).join(','),
257
+ modelName,
258
+ modelPath
259
+ };
260
+ const template = templates[command];
261
+ if (!template) {
262
+ console.error(`[错误] ${command} 未定义模板!`);
263
+ return;
264
+ }
265
+ const txt = mustache_1.default.render(template, data, {}, ['<%', '%>']);
266
+ const fileName = configData.command[command].replace(/{([a-zA-Z]+)}/g, (a, b) => data[b]);
267
+ const filePath = path_1.default.join(basepath, fileName);
268
+ const dirname = path_1.default.dirname(filePath);
269
+ try {
270
+ fs_1.default.statSync(dirname);
271
+ }
272
+ catch (error) {
273
+ fs_1.default.mkdirSync(dirname);
274
+ console.info(`[生成] ${dirname}`);
275
+ }
276
+ try {
277
+ fs_1.default.statSync(filePath);
278
+ if (force === false) {
279
+ console.warn(`[跳过] ${filePath}`);
280
+ return;
281
+ }
282
+ else {
283
+ console.warn(`[覆盖] ${filePath}`);
284
+ }
285
+ }
286
+ catch (error) {
287
+ console.info(`[生成] ${filePath}`);
288
+ }
289
+ if (configData.output) {
290
+ outputs.add(configData.output.replace(/{([a-zA-Z]+)}/g, (a, b) => data[b]));
291
+ }
292
+ fs_1.default.writeFileSync(path_1.default.join(basepath, fileName), txt);
293
+ }
294
+ }
295
+ const replServer = (0, repl_1.start)();
296
+ function defineCommand(command, comands) {
297
+ if (comands) {
298
+ console.log(`[组合]${command}>${comands.join(',')}注册成功`);
299
+ }
300
+ else {
301
+ console.log(`[命令]${command}注册成功`);
302
+ }
303
+ if (comands) {
304
+ replServer.defineCommand(command, async (input) => {
305
+ outputs.clear();
306
+ const inputs = input.match(/([^:]+):{0,1}([a-zA-Z0-9]*)/);
307
+ if (inputs?.length !== 3) {
308
+ return console.error(`[错误]命令格式应为: table1,table2[:模块名]`);
309
+ }
310
+ const [_, tables, modelName] = inputs;
311
+ for (const c of comands) {
312
+ await excute(c, tables, modelName ?? '');
313
+ }
314
+ console.info('执行完毕!下面打印生成的输出');
315
+ console.info(Array.from(outputs).join(','));
316
+ });
317
+ }
318
+ else {
319
+ replServer.defineCommand(command, async (input) => {
320
+ outputs.clear();
321
+ const inputs = input.match(/([^:]+):{0,1}([a-zA-Z0-9]*)/);
322
+ if (inputs?.length !== 3) {
323
+ return console.error(`[错误]命令格式应为: table1,table2[:模块名]`);
324
+ }
325
+ const [_, tables, modelName] = inputs;
326
+ await excute(command, tables, modelName ?? '');
327
+ console.info('执行完毕!下面打印生成的输出');
328
+ console.info(Array.from(outputs).join(','));
329
+ });
330
+ }
331
+ }
332
+ replServer.defineCommand('force', () => {
333
+ force = !force;
334
+ console.log(force ? '覆盖生成' : '不覆盖生成!');
335
+ });
336
+ if (configData.command) {
337
+ for (const command of Object.keys(configData.command)) {
338
+ if (!templates[command]) {
339
+ console.error(`命令:${command}没有定义模板,该命令不会生效`);
340
+ }
341
+ else {
342
+ defineCommand(command);
343
+ }
344
+ }
345
+ }
346
+ if (configData.commands) {
347
+ for (const [command, commands] of Object.entries(configData.commands)) {
348
+ const keys = commands;
349
+ const error = keys.filter(k => !templates[k]).join(',');
350
+ if (error) {
351
+ console.error(`组合命令:${command}定义了${commands},但是${error}没有定义模板,该命令不会生效`);
352
+ }
353
+ else {
354
+ defineCommand(command, keys);
355
+ }
356
+ }
357
+ }
358
+ }
359
+ catch (error) {
360
+ console.error(error);
361
+ }
@@ -0,0 +1,10 @@
1
+ export interface XML {
2
+ type: 'tag' | 'text';
3
+ name: string;
4
+ id?: string;
5
+ voidElement: boolean;
6
+ attrs: Record<string, string>;
7
+ children: XML[];
8
+ content: string;
9
+ }
10
+ export declare const convert: (childrens: XML[], param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) => string;