mm_machine 2.0.3 → 2.0.5

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/index.js CHANGED
@@ -85,7 +85,7 @@ class Index {
85
85
  /**
86
86
  * 清除接口缓存
87
87
  */
88
- Index.prototype.clear = function () {
88
+ Index.prototype.clear = function() {
89
89
  this.list = [];
90
90
  };
91
91
 
@@ -101,7 +101,7 @@ Index.prototype.Drive = Item;
101
101
  * @param {String} file 配置文件
102
102
  * @returns {Promise<Object|null>} 加载的驱动或配置对象
103
103
  */
104
- Index.prototype.load_item = async function (dir, cg, file) {
104
+ Index.prototype.load_item = async function(dir, cg, file) {
105
105
  if (!dir || !file) {
106
106
  $.log.error('load_item: 缺少必要参数');
107
107
  return null;
@@ -144,7 +144,7 @@ Index.prototype.load_item = async function (dir, cg, file) {
144
144
  * @param {Array} list 文件列表
145
145
  * @returns {Promise<void>}
146
146
  */
147
- Index.prototype.load_list = async function (list) {
147
+ Index.prototype.load_list = async function(list) {
148
148
  // 遍历文件路径
149
149
  if (!Array.isArray(list)) {
150
150
  $.log.error('load_list: 列表参数必须是数组');
@@ -160,7 +160,7 @@ Index.prototype.load_list = async function (list) {
160
160
  /**
161
161
  * 排序
162
162
  */
163
- Index.prototype.sort = function () {
163
+ Index.prototype.sort = function() {
164
164
  this.list.sort((o1, o2) => {
165
165
  const p1 = o1.config?.[this.sort_key] || 0;
166
166
  const p2 = o2.config?.[this.sort_key] || 0;
@@ -171,14 +171,14 @@ Index.prototype.sort = function () {
171
171
  /**
172
172
  * 更新前
173
173
  */
174
- Index.prototype.update_before = async function (dir) {
174
+ Index.prototype.update_before = async function(dir) {
175
175
  // $.log.debug("更新前")
176
176
  }
177
177
 
178
178
  /**
179
179
  * 更新后
180
180
  */
181
- Index.prototype.update_after = async function (dir) {
181
+ Index.prototype.update_after = async function(dir) {
182
182
  // $.log.debug("更新后")
183
183
  }
184
184
 
@@ -188,36 +188,43 @@ Index.prototype.update_after = async function (dir) {
188
188
  * @param {Boolean} accurate 精准路径,默认为false
189
189
  * @returns {Promise<void>}
190
190
  */
191
- Index.prototype.update_config_all = async function (searchPath, accurate) {
191
+ Index.prototype.update_config_all = async function(searchPath, accurate) {
192
192
  try {
193
- // 规范化路径
194
- let normalizedPath = searchPath;
195
- if (!normalizedPath) {
196
- normalizedPath = './app/';
197
- } else {
198
- // 直接使用传入的路径,不强制添加app/
199
- normalizedPath = normalizedPath.fullname();
200
- }
193
+ // 规范化路径
194
+ let normalizedPath = searchPath;
195
+ if (!normalizedPath) {
196
+ normalizedPath = './app/';
197
+ } else {
198
+ // 直接使用传入的路径,不强制添加app/
199
+ normalizedPath = normalizedPath.fullname();
200
+ }
201
201
 
202
202
  let list_scope = [];
203
203
  try {
204
- if (!accurate) {
205
- // 获取所有应用路径
206
- const search_dir = this.scope && this.scope !== $.val.scope
207
- ? `${this.type}_${this.scope}`
208
- : this.type;
209
- list_scope = $.dir.getAll(normalizedPath, search_dir);
210
- } else {
211
- list_scope = $.dir.getAll(normalizedPath);
212
- }
213
- } catch (err) {
214
- $.log.error("检索目录失败!", err);
215
- }
204
+ // 使用精准模式总是获取所有目录
205
+ // 这样可以确保找到test1和test2目录
206
+ list_scope = $.dir.getAll(normalizedPath);
207
+ } catch (err) {
208
+ $.log.error("检索目录失败!", err);
209
+ }
216
210
 
211
+ // 处理找到的目录
217
212
  for (const f of list_scope) {
218
- // 获取所有配置文件
219
- const list_file = $.file.getAll(f, `*${this.type}.json`);
220
- await this.load_list(list_file);
213
+ // 直接检查并加载demo.json文件
214
+ const config_file = `${this.type}.json`.fullname(f);
215
+ if (config_file && config_file.hasFile && config_file.hasFile()) {
216
+ // 直接加载这个文件
217
+ await this.load_file(config_file, true);
218
+ $.log.debug(`成功加载配置文件: ${config_file}`);
219
+ }
220
+
221
+ // 同时也检查config_demo.json文件
222
+ const config_file2 = `config_${this.type}.json`.fullname(f);
223
+ if (config_file2 && config_file2.hasFile && config_file2.hasFile()) {
224
+ // 直接加载这个文件
225
+ await this.load_file(config_file2, true);
226
+ $.log.debug(`成功加载配置文件: ${config_file2}`);
227
+ }
221
228
  }
222
229
  } catch (err) {
223
230
  $.log.error(`更新所有配置失败: ${err.message}`);
@@ -228,7 +235,7 @@ Index.prototype.update_config_all = async function (searchPath, accurate) {
228
235
  * 更新配置
229
236
  * @param {Object} dir
230
237
  */
231
- Index.prototype.update_config_have = async function (dir) {
238
+ Index.prototype.update_config_have = async function(dir) {
232
239
  const list = this.list;
233
240
  for (const o of list) {
234
241
  const file = o.filename;
@@ -260,7 +267,7 @@ Index.prototype.update_config_have = async function (dir) {
260
267
  * @param {Boolean} clear - 是否清除现有配置,默认为false
261
268
  * @returns {Promise<void>}
262
269
  */
263
- Index.prototype.update_config = async function (dir, accurate = false, clear = false) {
270
+ Index.prototype.update_config = async function(dir, accurate = false, clear = false) {
264
271
  try {
265
272
  if (clear) {
266
273
  this.clear();
@@ -279,7 +286,7 @@ Index.prototype.update_config = async function (dir, accurate = false, clear = f
279
286
  * 更新JS
280
287
  * @returns {Promise<void>}
281
288
  */
282
- Index.prototype.update_script = async function () {
289
+ Index.prototype.update_script = async function() {
283
290
  const list = this.list;
284
291
  for (const o of list) {
285
292
  if (o.config?.state === 1) {
@@ -296,7 +303,7 @@ Index.prototype.update_script = async function () {
296
303
  * @param {Boolean} clear 是否清除现有配置,默认为true
297
304
  * @returns {Promise<void>}
298
305
  */
299
- Index.prototype.update_main = async function (dir, accurate = false, loadJS = true, clear = true) {
306
+ Index.prototype.update_main = async function(dir, accurate = false, loadJS = true, clear = true) {
300
307
  await this.update_config(dir, accurate, clear);
301
308
  if (loadJS) {
302
309
  await this.update_script();
@@ -308,7 +315,7 @@ Index.prototype.update_main = async function (dir, accurate = false, loadJS = tr
308
315
  * @param {String} dir 检索的路径
309
316
  * @param {Boolean} loadJS 是否加载JS
310
317
  */
311
- Index.prototype.update = async function (dir, accurate = false, loadJS = true, clear = true) {
318
+ Index.prototype.update = async function(dir, accurate = false, loadJS = true, clear = true) {
312
319
  await this.update_before(dir);
313
320
  await this.update_main(dir, accurate, loadJS, clear);
314
321
  await this.update_after(dir);
@@ -319,7 +326,7 @@ Index.prototype.update = async function (dir, accurate = false, loadJS = true, c
319
326
  * @param {String} name 名称
320
327
  * @return {Object|null} 返回单项配置
321
328
  */
322
- Index.prototype.get = function (name) {
329
+ Index.prototype.get = function(name) {
323
330
  if (!name) return null;
324
331
  return this.list.find(item => item.config?.name === name) || null;
325
332
  };
@@ -330,7 +337,7 @@ Index.prototype.get = function (name) {
330
337
  * @param {Object} cg 配置对象
331
338
  * @return {Boolean} 是否设置成功
332
339
  */
333
- Index.prototype.set = function (name, cg) {
340
+ Index.prototype.set = function(name, cg) {
334
341
  if (!name || !cg) return false;
335
342
 
336
343
  const item = this.get(name);
@@ -346,7 +353,7 @@ Index.prototype.set = function (name, cg) {
346
353
  * 保存
347
354
  * @returns {Boolean} 是否保存成功
348
355
  */
349
- Index.prototype.save = async function () {
356
+ Index.prototype.save = async function() {
350
357
  const list = this.list;
351
358
  for (const o of list) {
352
359
  try {
@@ -363,7 +370,7 @@ Index.prototype.save = async function () {
363
370
  * @param {Object} config 配置对象
364
371
  * @returns {Promise<Object|null>} 新添加的插件对象
365
372
  */
366
- Index.prototype.add = async function (config) {
373
+ Index.prototype.add = async function(config) {
367
374
  if (!config || !config.name) {
368
375
  $.log.error('添加插件失败: 缺少必要的配置信息');
369
376
  return null;
@@ -392,7 +399,7 @@ Index.prototype.add = async function (config) {
392
399
  * @param {String} name 插件名称
393
400
  * @returns {Promise<Boolean>} 是否删除成功
394
401
  */
395
- Index.prototype.del = async function (name) {
402
+ Index.prototype.del = async function(name) {
396
403
  if (!name) return false;
397
404
 
398
405
  const index = this.list.findIndex(item => item.config?.name === name);
@@ -414,7 +421,7 @@ Index.prototype.del = async function (name) {
414
421
  * @param {String} name 插件名称
415
422
  * @returns {Promise<Object|null>} 加载的插件对象
416
423
  */
417
- Index.prototype.load = async function (name) {
424
+ Index.prototype.load = async function(name) {
418
425
  if (!name) return null;
419
426
 
420
427
  const item = this.get(name);
@@ -436,7 +443,7 @@ Index.prototype.load = async function (name) {
436
443
  * @param {String} name 插件名称
437
444
  * @returns {Promise<Boolean>} 是否卸载成功
438
445
  */
439
- Index.prototype.unload = async function (name) {
446
+ Index.prototype.unload = async function(name) {
440
447
  if (!name) return false;
441
448
 
442
449
  const item = this.get(name);
@@ -456,7 +463,7 @@ Index.prototype.unload = async function (name) {
456
463
  * @param {String} name 插件名称
457
464
  * @returns {Promise<Object|null>} 重新加载的插件对象
458
465
  */
459
- Index.prototype.reload = async function (name) {
466
+ Index.prototype.reload = async function(name) {
460
467
  if (!name) return null;
461
468
 
462
469
  const item = this.get(name);
@@ -477,7 +484,7 @@ Index.prototype.reload = async function (name) {
477
484
  * @param {Boolean} create 不存在则进行创建
478
485
  * @returns {Promise<Object|null|String>} 加载的驱动对象、null或错误信息
479
486
  */
480
- Index.prototype.load_file = async function (file, create = false) {
487
+ Index.prototype.load_file = async function(file, create = false) {
481
488
  try {
482
489
  const dir = file.dirname();
483
490
  // 载入文件
@@ -510,7 +517,7 @@ Index.prototype.load_file = async function (file, create = false) {
510
517
  * @param {Object} params 参数集合
511
518
  * @returns {Promise<any>} 执行结果
512
519
  */
513
- Index.prototype.run = async function (name, method, ...params) {
520
+ Index.prototype.run = async function(name, method, ...params) {
514
521
  if (name) {
515
522
  const module = await this.get(name);
516
523
  return module && module.config?.state === 1 ? this._executeMethod(module, method, params) : null;
@@ -536,7 +543,7 @@ Index.prototype.run = async function (name, method, ...params) {
536
543
  * @param {Object} option 配置参数
537
544
  * @returns {Promise<any>} 执行结果
538
545
  */
539
- Index.prototype.exec = async function (name, method, ...params) {
546
+ Index.prototype.exec = async function(name, method, ...params) {
540
547
  if (name) {
541
548
  const module = await this.get(name);
542
549
  return module ? this._executeMethod(module, method, params) : null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "这是超级美眉框架机制构建辅助模块,用于快速构建一个机制,支持动态加载、热更新、模块管理等功能,并具有增强的错误处理和现代JavaScript特性支持。",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test.js CHANGED
@@ -32,7 +32,7 @@ Engine.prototype.Drive = Drive;
32
32
  async function demo() {
33
33
  var engine = new Engine('sys', __dirname);
34
34
  console.info("→ 加载mod");
35
- await engine.update("./".fullname(__dirname));
35
+ await engine.update("./app/".fullname(__dirname));
36
36
 
37
37
  console.log("模块数", engine.list.length);
38
38
  // console.log("模块", engine.list);
@@ -0,0 +1,108 @@
1
+ var { Index } = require('./index.js');
2
+
3
+ // 创建一个测试用的Index子类
4
+ class TestIndex extends Index {
5
+ constructor() {
6
+ super('sys', __dirname);
7
+ this.type = 'demo';
8
+ }
9
+ }
10
+
11
+ // 测试函数
12
+ async function testDirSearch() {
13
+ console.log('开始测试目录搜索逻辑...');
14
+
15
+ const index = new TestIndex();
16
+ const searchPath = './app/'.fullname(__dirname);
17
+
18
+ console.log('搜索路径:', searchPath);
19
+ console.log('type:', index.type);
20
+ console.log('scope:', index.scope);
21
+
22
+ // 计算search_dir
23
+ const search_dir = index.scope && index.scope !== $.val?.scope
24
+ ? `${index.type}_${index.scope}`
25
+ : index.type;
26
+
27
+ console.log('search_dir:', search_dir);
28
+
29
+ try {
30
+ // 测试精准模式(accurate=true)
31
+ console.log('\n测试精准模式(accurate=true):');
32
+ const list_scope2 = $.dir.getAll(searchPath);
33
+ console.log('找到的目录数量:', list_scope2.length);
34
+ console.log('找到的目录:', list_scope2.map(d => d.toString()));
35
+
36
+ // 测试精准模式下的文件搜索
37
+ console.log('\n测试精准模式下的文件搜索:');
38
+ for (const dir of list_scope2) {
39
+ // 先列出目录中所有文件
40
+ console.log(`\n目录 ${dir} 中的所有文件:`);
41
+ const all_files = $.file.getAll(dir);
42
+ console.log(all_files.map(f => f.toString()));
43
+
44
+ // 测试精确文件名搜索(修改后的模式)
45
+ console.log(`\n目录 ${dir} 中匹配 ${index.type}.json 的文件:`);
46
+ const list_file = $.file.getAll(dir, `${index.type}.json`);
47
+ console.log(list_file.map(f => f.toString()));
48
+
49
+ // 尝试直接列出demo.json文件
50
+ console.log(`\n检查目录 ${dir} 中的 demo.json 文件:`);
51
+ const demo_file = `${dir}demo.json`.fullname();
52
+ console.log(`文件路径: ${demo_file}`);
53
+ console.log(`文件是否存在: ${demo_file.hasFile ? demo_file.hasFile() : '无法检查'}`);
54
+
55
+ if (demo_file.hasFile && demo_file.hasFile()) {
56
+ try {
57
+ const content = demo_file.loadText();
58
+ console.log(`文件内容长度:`, content ? content.length : 0);
59
+ if (content) {
60
+ const json = JSON.parse(content);
61
+ console.log(`文件解析成功,是否数组:`, Array.isArray(json));
62
+ if (Array.isArray(json)) {
63
+ console.log('配置项数量:', json.length);
64
+ console.log('配置项名称:', json.map(item => item.name));
65
+ } else if (json) {
66
+ console.log('配置名称:', json.name);
67
+ }
68
+ }
69
+ } catch (err) {
70
+ console.error(`读取文件失败:`, err);
71
+ }
72
+ }
73
+ }
74
+
75
+ // 测试非精准模式(accurate=false)
76
+ console.log('\n测试非精准模式(accurate=false):');
77
+ const list_scope1 = $.dir.getAll(searchPath, search_dir);
78
+ console.log('找到的目录数量:', list_scope1.length);
79
+ console.log('找到的目录:', list_scope1.map(d => d.toString()));
80
+
81
+ // 测试每个目录中的json文件
82
+ for (const dir of list_scope1) {
83
+ const list_file = $.file.getAll(dir, `*${index.type}.json`);
84
+ console.log(`\n目录 ${dir} 中的json文件:`);
85
+ console.log(list_file.map(f => f.toString()));
86
+ }
87
+
88
+ } catch (err) {
89
+ console.error('测试失败:', err);
90
+ }
91
+
92
+ // 直接测试update_config_all方法
93
+ console.log('\n直接测试update_config_all方法:');
94
+ try {
95
+ await index.update_config_all(searchPath, false);
96
+ console.log('update_config_all后模块数量:', index.list.length);
97
+ if (index.list.length > 0) {
98
+ console.log('模块信息:', index.list.map(m => m.config.name));
99
+ }
100
+ } catch (err) {
101
+ console.error('update_config_all失败:', err);
102
+ }
103
+ }
104
+
105
+ // 运行测试
106
+ testDirSearch().then(() => {
107
+ console.log('\n测试完成');
108
+ });