mm_machine 2.0.2 → 2.0.4

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.
@@ -1,30 +1,30 @@
1
- var i = 0;
2
-
3
- function test() {
4
- console.log("你好123", i++)
5
- }
6
-
7
- function main() {
8
- test();
9
- return i;
10
- }
11
-
12
- exports.main = main;
13
-
14
- exports.main_before = function() {
15
- console.log("test1请求前")
16
- }
17
-
18
- exports.main_after = async function(ret) {
19
- console.log("test1请求后", ret)
20
- }
21
-
22
- exports.load = function() {
23
- console.log("加载test1");
24
- }
25
-
26
- exports.init = function() {
27
- console.log("初始化test1");
28
- }
29
-
1
+ var i = 0;
2
+
3
+ function test() {
4
+ console.log("你好123", i++)
5
+ }
6
+
7
+ function main() {
8
+ test();
9
+ return i;
10
+ }
11
+
12
+ exports.main = main;
13
+
14
+ exports.main_before = function() {
15
+ console.log("test1请求前")
16
+ }
17
+
18
+ exports.main_after = async function(ret) {
19
+ console.log("test1请求后", ret)
20
+ }
21
+
22
+ exports.load = function() {
23
+ console.log("加载test1");
24
+ }
25
+
26
+ exports.init = function() {
27
+ console.log("初始化test1");
28
+ }
29
+
30
30
  console.log("引用了test1")
@@ -1,15 +1,15 @@
1
- class Demo {
2
- constructor() {}
3
- }
4
- var i = 0;
5
- Demo.prototype.main = function() {
6
- console.log("我很好123", i++);
7
- return i;
8
- }
9
- Demo.prototype.init = function(){
10
- console.log("初始化test2");
11
- }
12
-
13
- module.exports = new Demo();
14
-
1
+ class Demo {
2
+ constructor() {}
3
+ }
4
+ var i = 0;
5
+ Demo.prototype.main = function() {
6
+ console.log("我很好123", i++);
7
+ return i;
8
+ }
9
+ Demo.prototype.init = function(){
10
+ console.log("初始化test2");
11
+ }
12
+
13
+ module.exports = new Demo();
14
+
15
15
  console.log("引用了test2 after")
@@ -1,68 +1,68 @@
1
- [{
2
- /**
3
- * 名称, 由中英文和下“_”组成, 用于卸载接口 例如: demo
4
- */
5
- "name": "demo2",
6
- /**
7
- * 标题, 介绍作用
8
- */
9
- "title": "示例脚本2",
10
- /**
11
- * 描述, 用于描述该有什么用的
12
- */
13
- "description": "用于测试动态加载、更新、卸载、删除脚本",
14
- /**
15
- * 文件路径, 当调用函数不存在时,会先从文件中加载
16
- */
17
- "func_file": "./main.js",
18
- /**
19
- * 回调函数名 用于决定调用脚本的哪个函数
20
- */
21
- "func_name": "",
22
- /**
23
- * 排序
24
- */
25
- "sort": 10,
26
- /**
27
- * 状态, 0表示未启用, 1表示启用
28
- */
29
- "state": 1,
30
- /**
31
- * 显示, 0表示不显示, 1表示显示
32
- */
33
- "show": 0
34
- },
35
- {
36
- /**
37
- * 名称, 由中英文和下“_”组成, 用于卸载接口 例如: demo
38
- */
39
- "name": "demo3",
40
- /**
41
- * 标题, 介绍作用
42
- */
43
- "title": "示例脚本3",
44
- /**
45
- * 描述, 用于描述该有什么用的
46
- */
47
- "description": "用于测试动态加载、更新、卸载、删除脚本",
48
- /**
49
- * 文件路径, 当调用函数不存在时,会先从文件中加载
50
- */
51
- "func_file": "./after.js",
52
- /**
53
- * 回调函数名 用于决定调用脚本的哪个函数
54
- */
55
- "func_name": "main",
56
- /**
57
- * 排序
58
- */
59
- "sort": 10,
60
- /**
61
- * 状态, 0表示未启用, 1表示启用
62
- */
63
- "state": 1,
64
- /**
65
- * 显示, 0表示不显示, 1表示显示
66
- */
67
- "show": 0
1
+ [{
2
+ /**
3
+ * 名称, 由中英文和下“_”组成, 用于卸载接口 例如: demo
4
+ */
5
+ "name": "demo2",
6
+ /**
7
+ * 标题, 介绍作用
8
+ */
9
+ "title": "示例脚本2",
10
+ /**
11
+ * 描述, 用于描述该有什么用的
12
+ */
13
+ "description": "用于测试动态加载、更新、卸载、删除脚本",
14
+ /**
15
+ * 文件路径, 当调用函数不存在时,会先从文件中加载
16
+ */
17
+ "func_file": "./main.js",
18
+ /**
19
+ * 回调函数名 用于决定调用脚本的哪个函数
20
+ */
21
+ "func_name": "",
22
+ /**
23
+ * 排序
24
+ */
25
+ "sort": 10,
26
+ /**
27
+ * 状态, 0表示未启用, 1表示启用
28
+ */
29
+ "state": 1,
30
+ /**
31
+ * 显示, 0表示不显示, 1表示显示
32
+ */
33
+ "show": 0
34
+ },
35
+ {
36
+ /**
37
+ * 名称, 由中英文和下“_”组成, 用于卸载接口 例如: demo
38
+ */
39
+ "name": "demo3",
40
+ /**
41
+ * 标题, 介绍作用
42
+ */
43
+ "title": "示例脚本3",
44
+ /**
45
+ * 描述, 用于描述该有什么用的
46
+ */
47
+ "description": "用于测试动态加载、更新、卸载、删除脚本",
48
+ /**
49
+ * 文件路径, 当调用函数不存在时,会先从文件中加载
50
+ */
51
+ "func_file": "./after.js",
52
+ /**
53
+ * 回调函数名 用于决定调用脚本的哪个函数
54
+ */
55
+ "func_name": "main",
56
+ /**
57
+ * 排序
58
+ */
59
+ "sort": 10,
60
+ /**
61
+ * 状态, 0表示未启用, 1表示启用
62
+ */
63
+ "state": 1,
64
+ /**
65
+ * 显示, 0表示不显示, 1表示显示
66
+ */
67
+ "show": 0
68
68
  }]
@@ -1,16 +1,16 @@
1
- class Demo {
2
- constructor() {}
3
- }
4
- var i = 0;
5
- Demo.prototype.main = function() {
6
- console.log("test2 main", i++);
7
- return i;
8
- }
9
-
10
- Demo.prototype.init = function() {
11
- console.log("test2 init");
12
- }
13
-
14
- module.exports = new Demo();
15
-
1
+ class Demo {
2
+ constructor() {}
3
+ }
4
+ var i = 0;
5
+ Demo.prototype.main = function() {
6
+ console.log("test2 main", i++);
7
+ return i;
8
+ }
9
+
10
+ Demo.prototype.init = function() {
11
+ console.log("test2 init");
12
+ }
13
+
14
+ module.exports = new Demo();
15
+
16
16
  console.log("引用了test2 main")
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,9 +101,9 @@ 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
- console.error('load_item: 缺少必要参数');
106
+ $.log.error('load_item: 缺少必要参数');
107
107
  return null;
108
108
  }
109
109
 
@@ -134,7 +134,7 @@ Index.prototype.load_item = async function (dir, cg, file) {
134
134
  return json;
135
135
  }
136
136
  } catch (err) {
137
- console.error('加载项失败:', err);
137
+ $.log.error('加载项失败:', err);
138
138
  return null;
139
139
  }
140
140
  };
@@ -144,10 +144,10 @@ 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
- console.error('load_list: 列表参数必须是数组');
150
+ $.log.error('load_list: 列表参数必须是数组');
151
151
  return;
152
152
  }
153
153
 
@@ -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,15 +171,15 @@ Index.prototype.sort = function () {
171
171
  /**
172
172
  * 更新前
173
173
  */
174
- Index.prototype.update_before = async function (dir) {
175
- // console.log("更新前")
174
+ Index.prototype.update_before = async function(dir) {
175
+ // $.log.debug("更新前")
176
176
  }
177
177
 
178
178
  /**
179
179
  * 更新后
180
180
  */
181
- Index.prototype.update_after = async function (dir) {
182
- // console.log("更新后")
181
+ Index.prototype.update_after = async function(dir) {
182
+ // $.log.debug("更新后")
183
183
  }
184
184
 
185
185
  /**
@@ -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;
@@ -247,7 +254,7 @@ Index.prototype.update_config_have = async function (dir) {
247
254
  }
248
255
  }
249
256
  } catch (err) {
250
- console.error(`更新配置失败: ${err.message}`);
257
+ $.log.error(`更新配置失败: ${err.message}`);
251
258
  }
252
259
  }
253
260
  }
@@ -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();
@@ -271,7 +278,7 @@ Index.prototype.update_config = async function (dir, accurate = false, clear = f
271
278
  }
272
279
  this.sort();
273
280
  } catch (err) {
274
- console.error(`配置更新失败: ${err.message}`);
281
+ $.log.error(`配置更新失败: ${err.message}`);
275
282
  }
276
283
  };
277
284
 
@@ -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,13 +353,13 @@ 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 {
353
360
  await o.exec('save');
354
361
  } catch (err) {
355
- console.error(`保存失败: ${err.message}`);
362
+ $.log.error(`保存失败: ${err.message}`);
356
363
  }
357
364
  }
358
365
  return true;
@@ -363,9 +370,9 @@ 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
- console.error('添加插件失败: 缺少必要的配置信息');
375
+ $.log.error('添加插件失败: 缺少必要的配置信息');
369
376
  return null;
370
377
  }
371
378
 
@@ -373,7 +380,7 @@ Index.prototype.add = async function (config) {
373
380
  // 检查是否已存在
374
381
  const existing = this.get(config.name);
375
382
  if (existing) {
376
- console.warn(`插件 ${config.name} 已存在`);
383
+ $.log.warn(`插件 ${config.name} 已存在`);
377
384
  return existing;
378
385
  }
379
386
 
@@ -382,7 +389,7 @@ Index.prototype.add = async function (config) {
382
389
  this.sort();
383
390
  return item;
384
391
  } catch (err) {
385
- console.error(`添加插件失败: ${err.message}`);
392
+ $.log.error(`添加插件失败: ${err.message}`);
386
393
  return null;
387
394
  }
388
395
  };
@@ -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);
@@ -404,7 +411,7 @@ Index.prototype.del = async function (name) {
404
411
  this.list.splice(index, 1);
405
412
  return true;
406
413
  } catch (err) {
407
- console.error(`删除插件失败: ${err.message}`);
414
+ $.log.error(`删除插件失败: ${err.message}`);
408
415
  return false;
409
416
  }
410
417
  };
@@ -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);
@@ -426,7 +433,7 @@ Index.prototype.load = async function (name) {
426
433
  }
427
434
  return item;
428
435
  } catch (err) {
429
- console.error(`加载插件 ${name} 失败: ${err.message}`);
436
+ $.log.error(`加载插件 ${name} 失败: ${err.message}`);
430
437
  return null;
431
438
  }
432
439
  };
@@ -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);
@@ -446,7 +453,7 @@ Index.prototype.unload = async function (name) {
446
453
  await item.exec('unload');
447
454
  return true;
448
455
  } catch (err) {
449
- console.error(`卸载插件 ${name} 失败: ${err.message}`);
456
+ $.log.error(`卸载插件 ${name} 失败: ${err.message}`);
450
457
  return false;
451
458
  }
452
459
  };
@@ -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);
@@ -466,7 +473,7 @@ Index.prototype.reload = async function (name) {
466
473
  await item.exec('reload');
467
474
  return item;
468
475
  } catch (err) {
469
- console.error(`重新加载插件 ${name} 失败: ${err.message}`);
476
+ $.log.error(`重新加载插件 ${name} 失败: ${err.message}`);
470
477
  return null;
471
478
  }
472
479
  };
@@ -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
  // 载入文件
@@ -498,7 +505,7 @@ Index.prototype.load_file = async function (file, create = false) {
498
505
  }
499
506
  return null;
500
507
  } catch (err) {
501
- console.error(`加载文件失败: ${err.message}`);
508
+ $.log.error(`加载文件失败: ${err.message}`);
502
509
  return `加载文件失败: ${err.message}`;
503
510
  }
504
511
  };
@@ -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.2",
3
+ "version": "2.0.4",
4
4
  "description": "这是超级美眉框架机制构建辅助模块,用于快速构建一个机制,支持动态加载、热更新、模块管理等功能,并具有增强的错误处理和现代JavaScript特性支持。",
5
5
  "main": "index.js",
6
6
  "scripts": {