mm_machine 3.2.5 → 3.2.7

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
@@ -1,56 +1,7 @@
1
- const { Mod } = require('./mod');
2
- const { Drive } = require('./drive.js');
3
- const { Scanner } = require('./scanner.js');
4
- const { Registry } = require('./registry.js');
5
- const { Manager } = require('./manager.js');
6
-
7
- /**
8
- * 索引类 - 唯一入口
9
- * 作为模块系统的顶层入口,内部持有 Registry(注册表)和 Manager(运行时管理器)
10
- */
11
- class Index {
12
- /**
13
- * 构造函数
14
- * @param {object} config 配置对象
15
- * @param {object} parent 父对象
16
- */
17
- constructor(config, parent) {
18
- this.config = {
19
- name: 'default',
20
- ...config
21
- };
22
-
23
- if (parent) {
24
- this.getParent = function () {
25
- return parent;
26
- };
27
- }
28
-
29
- // 注册表:管理模块配置持久化
30
- if (!$.registry) {
31
- $.registry = new Registry({
32
- name: this.constructor.name + '_' + this.config.name
33
- });
34
- }
35
-
36
- // 运行时管理器:管理模块生命周期和方法派发
37
- this._manager = new Manager({}, this);
38
- }
39
- }
40
-
41
- /**
42
- * 加载
43
- * 读取注册数据并初始化
44
- */
45
- Index.prototype.load = function () {
46
- $.registry.load();
47
- };
1
+ const { Mod } = require('./lib/mod.js');
2
+ const { Drive } = require('./lib/drive.js');
3
+ const { Manager } = require('./lib/manager.js');
48
4
 
49
5
  module.exports = {
50
- Index,
51
- Mod,
52
- Drive,
53
- Scanner,
54
- Registry,
55
- Manager
6
+ Mod, Drive, Manager
56
7
  };
@@ -57,6 +57,23 @@ class Drive extends Mod {
57
57
  }
58
58
  }
59
59
 
60
+ /**
61
+ * 监听加载事件
62
+ * @param {object} ctx 上下文对象
63
+ */
64
+ Drive.prototype._onLoad = async function (ctx) {
65
+ // 1. 加载
66
+ this.on('load:before', async (ctx) => {
67
+ this.setState('loading');
68
+ await this.call('loadScript');
69
+ await this._loadCore(...ctx.params);
70
+ });
71
+ this.on('load:after', (ctx) => {
72
+ this.setState('loaded');
73
+ this.isLoaded(true);
74
+ });
75
+ };
76
+
60
77
  /**
61
78
  * 获取事件触发器
62
79
  * @returns {object} 事件触发器
@@ -664,8 +681,6 @@ Drive.prototype.loadBefore = function () {
664
681
  * 加载处理
665
682
  */
666
683
  Drive.prototype._loadCore = function () {
667
- // this.loadConfig();
668
- // this.loadScript();
669
684
  };
670
685
 
671
686
  /**
@@ -1,7 +1,6 @@
1
- const { Mod } = require('./mod');
1
+ const { Mod } = require('./mod.js');
2
2
  const { Drive } = require('./drive.js');
3
3
  const { Scanner } = require('./scanner.js');
4
- const { Registry } = require('./registry.js');
5
4
 
6
5
  /**
7
6
  * 管理器类
@@ -72,12 +71,6 @@ class Manager extends Mod {
72
71
  * @type {number}
73
72
  */
74
73
  mode: 3,
75
- /**
76
- * 是否启用注册表集成
77
- * true 时自动从 $.registry 读取/写入模块配置
78
- * @type {boolean}
79
- */
80
- use_registry: true,
81
74
  /**
82
75
  * 作用域
83
76
  * @type {string}
@@ -146,7 +139,7 @@ Manager.prototype.getInfos = function () {
146
139
  Manager.prototype.getList = function (keyword) {
147
140
  let infos = this._getInfos();
148
141
 
149
- // 先用 entry 做关键词过滤,避免无效实例化
142
+ // 先用 info 做关键词过滤,避免无效实例化
150
143
  if (keyword) {
151
144
  infos = infos.filter((o) => {
152
145
  return o.name.includes(keyword) || o.title.includes(keyword) || o.description.includes(keyword);
@@ -187,18 +180,13 @@ Manager.prototype.Drive = Drive;
187
180
  * 更新模块信息
188
181
  */
189
182
  Manager.prototype.updateInfo = function () {
190
- // 由 registry 管理模块信息,无需额外操作
191
183
  };
192
184
 
193
185
  /**
194
186
  * 获取模块配置列表
195
- * 启用注册表时从 registry 获取;否则从内部缓存 infos 读取
196
187
  * @returns {Array} 排序后的模块配置数组
197
188
  */
198
189
  Manager.prototype._getInfos = function () {
199
- if (this.config.use_registry) {
200
- return $.registry.getMods(this.getParentKey());
201
- }
202
190
  return this.infos || [];
203
191
  };
204
192
 
@@ -210,8 +198,7 @@ Manager.prototype.clearMods = function () {
210
198
  for (let name in this.mods) {
211
199
  delete this.mods[name];
212
200
  }
213
- // 同步清理内部缓存
214
- if (!this.config.use_registry && this.infos) {
201
+ if (this.infos) {
215
202
  this.infos = [];
216
203
  }
217
204
  };
@@ -225,8 +212,7 @@ Manager.prototype.delMod = function (name) {
225
212
  let mod = this.mods[name];
226
213
  if (mod) {
227
214
  delete this.mods[name];
228
- // 同步清理内部缓存
229
- if (!this.config.use_registry && this.infos) {
215
+ if (this.infos) {
230
216
  this.infos = this.infos.filter((o) => { return o.name !== name; });
231
217
  }
232
218
  }
@@ -244,30 +230,11 @@ Manager.prototype.setMod = function (name, mod) {
244
230
 
245
231
  /**
246
232
  * 获取模块
247
- * 内存不存在时,若启用注册表则从注册表配置按需实例化
248
233
  * @param {string} name 模块名
249
234
  * @returns {object|null} 模块对象
250
235
  */
251
236
  Manager.prototype.getMod = function (name) {
252
- var mod = this.mods[name];
253
- if (mod) return mod;
254
-
255
- // 注册表回退:通过 _getInfos 查找配置并按需实例化
256
- if (this.config.use_registry) {
257
- var infos = this._getInfos();
258
- for (var i = 0; i < infos.length; i++) {
259
- if (infos[i].name === name) {
260
- mod = this.newMod(infos[i].config_file);
261
- // 保存到内存集合,避免后续访问时再次创建
262
- if (mod) {
263
- this.setMod(mod.config.name, mod);
264
- }
265
- return mod;
266
- }
267
- }
268
- }
269
-
270
- return null;
237
+ return this.mods[name] || null;
271
238
  };
272
239
 
273
240
  /**
@@ -299,7 +266,6 @@ Manager.prototype.newMod = function (config_file, config, script) {
299
266
  mod.mode = this.config.mode;
300
267
 
301
268
  if (config && config.name) {
302
- // config 已从 registry 缓存获取,跳过磁盘读
303
269
  mod.config_file = config_file;
304
270
  } else {
305
271
  mod.loadConfig(config_file);
@@ -340,7 +306,7 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
340
306
  this.setMod(mod.config.name, mod);
341
307
  }
342
308
 
343
- var entry = {
309
+ var info = {
344
310
  name: mod.config.name,
345
311
  title: mod.config.title,
346
312
  description: mod.config.description,
@@ -354,24 +320,10 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
354
320
  let keys = this.config.trigger_key;
355
321
  if (keys && keys.length > 0) {
356
322
  for (let key in keys) {
357
- entry[key] = mod.config[key];
358
- }
359
- }
360
- // 启用注册表时,自动写入 $.registry 供后续实例复用
361
- if (this.config.use_registry) {
362
- // 直接通过 registry 的 getMod 判断是否已存在,避免 _getInfos 重复
363
- if (!$.registry.getMod(this.getParentKey(), mod.config.name)) {
364
- try {
365
- // 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
366
- $.registry.setMod(this.getParentKey(), mod.config.name, entry);
367
- } catch (err) {
368
- throw new Error('注册模块失败: ' + config_file + ' - ' + err.message);
369
- }
323
+ info[key] = mod.config[key];
370
324
  }
371
- } else {
372
- this.infos.push(entry);
373
325
  }
374
-
326
+ this.infos.push(info);
375
327
  return mod;
376
328
  };
377
329
 
@@ -477,19 +429,6 @@ Manager.prototype.getDirName = function () {
477
429
  return prefix + '_' + name;
478
430
  };
479
431
 
480
- /**
481
- * 加载所有模块的脚本
482
- */
483
- Manager.prototype.loadScripts = async function () {
484
- var promises = [];
485
- let mods = this.getMods();
486
- for (let name in mods) {
487
- let mod = mods[name];
488
- promises.push(mod.call('loadScript'));
489
- }
490
- await Promise.all(promises);
491
- };
492
-
493
432
  /**
494
433
  * 更新配置前的钩子函数
495
434
  */
@@ -516,7 +455,7 @@ Manager.prototype.update = async function (dir, clear = true) {
516
455
  this.regMods(files);
517
456
 
518
457
  if (!this.config.lazy_load) {
519
- await this.loadScripts();
458
+ await this.loadMods();
520
459
  }
521
460
  this._is_loaded = true;
522
461
  } catch (err) {
@@ -524,28 +463,6 @@ Manager.prototype.update = async function (dir, clear = true) {
524
463
  }
525
464
  };
526
465
 
527
- /**
528
- * 从 registry 缓存加载模块(按 parent 字段匹配)
529
- * 从 registry.mod_dict 中按 parent 过滤并创建模块实例
530
- * @returns {boolean} 是否加载到模块
531
- */
532
- Manager.prototype._loadFromRegistry = function () {
533
- let lt = this._getInfos();
534
- if (!lt.length) return false;
535
-
536
- for (var i = 0; i < lt.length; i++) {
537
- var entry = lt[i];
538
- if (!this.config.lazy_load) {
539
- let mod = this.newMod(entry.config_file);
540
- if (mod) {
541
- this.setMod(mod.config.name, mod);
542
- }
543
- }
544
- }
545
-
546
- return true;
547
- };
548
-
549
466
  /**
550
467
  * 检索目录下所有文件
551
468
  * @private
@@ -598,11 +515,11 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
598
515
  var result;
599
516
  var infos = this._getInfos();
600
517
  for (var i = 0; i < infos.length; i++) {
601
- var entry = infos[i];
602
- // 直接用 entry.state 过滤,避免无效实例化
603
- if (entry.state !== 1) continue;
518
+ var info = infos[i];
519
+ // 直接用 info.state 过滤,避免无效实例化
520
+ if (info.state !== 1) continue;
604
521
 
605
- var mod = this.getMod(entry.name);
522
+ var mod = this.getMod(info.name);
606
523
  if (!mod) continue;
607
524
 
608
525
  var ret = null;
@@ -636,7 +553,7 @@ Manager.prototype._runMod = async function (mod, method, ...params) {
636
553
  // 确保模块已加载
637
554
  if (!mod.isLoaded()) {
638
555
  try {
639
- await mod.call('loadScript');
556
+ await mod.do('load');
640
557
  } catch (err) {
641
558
  this.log('error', `加载模块脚本失败: `, err);
642
559
  }
@@ -648,7 +565,15 @@ Manager.prototype._runMod = async function (mod, method, ...params) {
648
565
 
649
566
  // 确保模块已初始化
650
567
  if (!mod.isInited() && method !== 'init') {
651
- await mod.do('init');
568
+ try {
569
+ await mod.do('init');
570
+ } catch (err) {
571
+ this.log('error', `初始化模块脚本失败: `, err);
572
+ }
573
+ // 初始化失败则不再执行,避免无方法调用
574
+ if (!mod.isInited()) {
575
+ return null;
576
+ }
652
577
  }
653
578
 
654
579
  // 执行方法
@@ -677,26 +602,13 @@ Manager.prototype.getDir = function () {
677
602
 
678
603
  /**
679
604
  * 加载资源
680
- * 启用注册表时优先从注册表读取,快速启动;
681
- * 同时在后台异步扫描文件系统,确保注册表数据为物理最新。
682
605
  */
683
606
  Manager.prototype._loadSources = async function () {
684
- // 启用注册表且有缓存数据时,走内存快路径,快速启动;
685
- // 后台异步扫描文件系统更新注册表,确保物理最新。
686
- if (this.config.use_registry) {
687
- if (this._loadFromRegistry()) {
688
- // setImmediate(() => this._scanForUpdate(false));
689
- return;
690
- }
691
- }
692
-
693
- // 注册表无缓存,同步扫描文件系统
694
607
  await this._scanForUpdate();
695
608
  };
696
609
 
697
610
  /**
698
- * 后台扫描文件系统并同步到注册表
699
- * 不阻塞启动,让注册表始终与磁盘保持同步
611
+ * 后台扫描文件系统
700
612
  */
701
613
  Manager.prototype._scanForUpdate = async function (clear = true) {
702
614
  var base_dir = this.getBaseDir();
@@ -707,9 +619,6 @@ Manager.prototype._scanForUpdate = async function (clear = true) {
707
619
  } else {
708
620
  await this.call('update', dir, clear);
709
621
  }
710
- if (this.config.use_registry) {
711
- $.registry.save();
712
- }
713
622
  } catch (err) {
714
623
  this.log('error', '后台扫描同步失败', err);
715
624
  }
@@ -722,20 +631,25 @@ Manager.prototype._initSources = async function () {
722
631
  // 通过
723
632
  };
724
633
 
634
+ /**
635
+ * 加载核心资源
636
+ */
637
+ Manager.prototype._loadCore = async function () {
638
+ // 初始化管理器
639
+ this._initManager();
640
+ // 加载资源
641
+ await this._loadSources();
642
+ };
643
+
725
644
  /**
726
645
  * 初始化公共属性
727
646
  */
728
647
  Manager.prototype._initCore = async function () {
729
- try {
730
- // 初始化管理器
731
- this._initManager();
732
- // 加载资源
733
- await this._loadSources();
734
- // 初始化AI客户端
735
- await this._initSources();
736
- } catch (err) {
737
- this.log('error', '管理器初始化失败', err);
648
+ if (this.isLoaded()) {
649
+ await this.do('load');
738
650
  }
651
+ // 初始化AI客户端
652
+ await this._initSources();
739
653
  };
740
654
 
741
655
  /**
@@ -775,18 +689,26 @@ Manager.prototype.exec = async function (name, method, ...params) {
775
689
  };
776
690
 
777
691
  /**
778
- * 确保单个模块已加载
779
- * @param {string} name 模块名
692
+ * 确保模块已加载
693
+ * @param {object} mod 模块对象
694
+ * @param {string} method 方法名称
780
695
  * @returns {Promise<object|null>} 模块实例或 null
781
696
  */
782
- Manager.prototype._ensureModLoaded = async function (name) {
783
- let mod = this.getMod(name);
697
+ Manager.prototype._ensureModLoaded = async function (mod, method) {
784
698
  if (!mod) return null;
785
- if (!mod.isLoaded()) {
699
+ if (!mod.isLoaded() && method !== 'load') {
700
+ try {
701
+ await mod.do('load');
702
+ } catch (error) {
703
+ this.log('error', `模块${mod.config.name}加载失败`, error);
704
+ return null;
705
+ }
706
+ }
707
+ if (!mod.isInited() && method !== 'init') {
786
708
  try {
787
- await mod.call('loadScript');
709
+ await mod.do('init');
788
710
  } catch (error) {
789
- this.log('error', `模块${name}加载失败`, error);
711
+ this.log('error', `模块${mod.config.name}初始化失败`, error);
790
712
  return null;
791
713
  }
792
714
  }
@@ -797,13 +719,16 @@ Manager.prototype._ensureModLoaded = async function (name) {
797
719
  * 确保所有模块都已加载
798
720
  * @returns {Promise} 加载完成
799
721
  */
800
- Manager.prototype._ensureAllLoaded = async function () {
722
+ Manager.prototype._ensureAllLoaded = async function (method) {
801
723
  var promises = [];
802
724
  var infos = this._getInfos();
803
725
 
804
726
  for (var i = 0; i < infos.length; i++) {
805
- promises.push(this._ensureModLoaded(infos[i].name));
727
+ let mod = this.getMod(infos[i].name);
728
+ if (!mod) continue;
729
+ promises.push(this._ensureModLoaded(mod, method));
806
730
  }
731
+
807
732
  await Promise.all(promises);
808
733
  };
809
734
 
@@ -822,9 +747,9 @@ Manager.prototype.runWait = async function (method, ...params) {
822
747
  var infos = this._getInfos();
823
748
 
824
749
  for (var i = 0; i < infos.length; i++) {
825
- let mod = await this._ensureModLoaded(infos[i].name);
750
+ let mod = this.getMod(infos[i].name);
826
751
  if (!mod) continue;
827
-
752
+ mod = await this._ensureModLoaded(mod, method);
828
753
  try {
829
754
  let ret = await mod.do(method, ...params);
830
755
  if (ret !== null && ret !== undefined) {
@@ -850,7 +775,7 @@ Manager.prototype.runAsync = async function (method, ...params) {
850
775
  }
851
776
 
852
777
  // 确保所有模块都已加载
853
- await this._ensureAllLoaded();
778
+ await this._ensureAllLoaded(method);
854
779
 
855
780
  var infos = this._getInfos();
856
781
 
@@ -878,7 +803,7 @@ Manager.prototype.runAll = async function (method, ...params) {
878
803
  }
879
804
 
880
805
  // 确保所有模块都已加载
881
- await this._ensureAllLoaded();
806
+ await this._ensureAllLoaded(method);
882
807
 
883
808
  const promises = [];
884
809
  var infos = this._getInfos();
@@ -905,7 +830,7 @@ Manager.prototype.runRace = async function (method, ...params) {
905
830
  }
906
831
 
907
832
  // 确保所有模块都已加载
908
- await this._ensureAllLoaded();
833
+ await this._ensureAllLoaded(method);
909
834
 
910
835
  const promises = [];
911
836
  var infos = this._getInfos();
@@ -939,9 +864,9 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
939
864
  var infos = this._getInfos();
940
865
 
941
866
  for (var i = 0; i < infos.length; i++) {
942
- var mod = await this._ensureModLoaded(infos[i].name);
867
+ let mod = this.getMod(infos[i].name);
943
868
  if (!mod) continue;
944
-
869
+ mod = await this._ensureModLoaded(mod, method);
945
870
  try {
946
871
  let ret = await mod.do(method, result, ...params);
947
872
  if (ret !== null && ret !== undefined) {
@@ -966,10 +891,11 @@ Manager.prototype.runSubMod = async function (name, method, ...params) {
966
891
  if (typeof method !== 'string') {
967
892
  throw new TypeError('方法名称必须是字符串');
968
893
  }
969
- let mod = await this._ensureModLoaded(name);
894
+ let mod = this.getMod(name);
970
895
  if (!mod) {
971
896
  throw new Error(`模块${name}不存在或加载失败`);
972
897
  }
898
+ mod = await this._ensureModLoaded(mod, method);
973
899
  return await mod.do(method, ...params);
974
900
  };
975
901
 
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.2.5",
3
+ "version": "3.2.7",
4
4
  "description": "A flexible Node.js plugin mechanism system for dynamic loading, management and execution of modules. Supports hot reload, lifecycle management, and modern JavaScript features.",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "node ./tests/run.js",
7
+ "test": "node ./test.js",
8
8
  "dev": "nodemon ./test.js"
9
9
  },
10
10
  "repository": {
@@ -27,12 +27,8 @@
27
27
  "node": ">=12.0.0"
28
28
  },
29
29
  "files": [
30
- "drive.js",
31
30
  "index.js",
32
- "manager.js",
33
- "mod.js",
34
- "registry.js",
35
- "scanner.js",
31
+ "lib/*",
36
32
  "README.md"
37
33
  ],
38
34
  "dependencies": {
@@ -43,7 +39,7 @@
43
39
  },
44
40
  "devDependencies": {
45
41
  "eslint": "^10.7.0",
46
- "eslint-plugin-jsdoc": "^63.0.13",
42
+ "eslint-plugin-jsdoc": "^63.0.14",
47
43
  "mm_eslint": "^1.7.5"
48
44
  }
49
45
  }
package/registry.js DELETED
@@ -1,273 +0,0 @@
1
- require('mm_expand');
2
-
3
- /**
4
- * 注册类 - 注册表模式
5
- * 管理模块配置的持久化、模块配置的 CRUD
6
- */
7
- class Registry {
8
- /**
9
- * 构造函数
10
- * @param {object} config 配置项
11
- */
12
- constructor(config) {
13
- this.config = {
14
- name: 'default',
15
- dir: '/data/registry'.fullname(),
16
- ...config
17
- };
18
- this.mod_dict = {};
19
- this.config.dir.addDir();
20
- }
21
- }
22
-
23
- // ==== 配置项 CRUD ====
24
-
25
- /**
26
- * 创建新模块配置项
27
- * @returns {object} 模块配置项
28
- */
29
- Registry.prototype.newModel = function () {
30
- return {
31
- name: '',
32
- title: '',
33
- description: '',
34
- version: '1.0.0',
35
- dependencies: {},
36
- sort: 100,
37
- state: 1,
38
- auto_start: 'auto',
39
- config_file: '',
40
- main: ''
41
- };
42
- };
43
-
44
- /**
45
- * 设置模块
46
- * @param {string} parent 父级名称
47
- * @param {string} name 模块名称
48
- * @param {object} config 模块配置项
49
- * @returns {object} 模块配置项
50
- */
51
- Registry.prototype.setMod = function (parent, name, config) {
52
- if (!parent) {
53
- throw new TypeError('父级名称(parent)不能为空');
54
- }
55
- else if (typeof parent !== 'string') {
56
- throw new TypeError('父级名称(parent)必须为字符串');
57
- }
58
- if (!name) {
59
- throw new TypeError('模块名称(name)不能为空');
60
- }
61
- else if (typeof name !== 'string') {
62
- throw new TypeError('模块名称(name)必须为字符串');
63
- }
64
- if (!config) {
65
- throw new TypeError('模块配置项不能为空');
66
- }
67
- else if (typeof config !== 'object') {
68
- throw new TypeError('模块配置项必须为对象');
69
- }
70
- this.checkConfig(config);
71
- let cg = this.fullConfig(config);
72
-
73
- // 按 parent 分组存储到 mod_dict
74
- if (!this.mod_dict[parent]) {
75
- this.mod_dict[parent] = [];
76
- }
77
- var lt = this.mod_dict[parent];
78
-
79
- let has = false;
80
- for (let i = 0; i < lt.length; i++) {
81
- let o = lt[i];
82
- if (o.name === name) {
83
- lt[i] = cg;
84
- has = true;
85
- break;
86
- }
87
- }
88
- if (!has) {
89
- lt.push(cg);
90
- }
91
- return cg;
92
- };
93
-
94
- /**
95
- * 获取模块
96
- * @param {string} parent 父级名称
97
- * @param {string} name 模块名称
98
- * @returns {object} 模块配置项
99
- */
100
- Registry.prototype.getMod = function (parent, name) {
101
- var lt = this.mod_dict[parent];
102
- if (lt) {
103
- for (let i = 0; i < lt.length; i++) {
104
- if (lt[i].name === name) {
105
- return lt[i];
106
- }
107
- }
108
- }
109
- return null;
110
- };
111
-
112
- /**
113
- * 删除模块
114
- * @param {string} parent 父级名称
115
- * @param {string} name 模块名称
116
- * @returns {object} 模块配置项
117
- */
118
- Registry.prototype.delMod = function (parent, name) {
119
- var lt = this.mod_dict[parent];
120
- if (!lt) return null;
121
-
122
- var obj = null;
123
- for (let i = 0; i < lt.length; i++) {
124
- if (lt[i].name === name) {
125
- obj = lt[i];
126
- lt.splice(i, 1);
127
- break;
128
- }
129
- }
130
- if (lt.length === 0) {
131
- delete this.mod_dict[parent];
132
- }
133
- return obj;
134
- };
135
-
136
- /**
137
- * 获取模块列表
138
- * @param {string} parent 父级名称
139
- * @returns {Array} 模块配置项数组
140
- */
141
- Registry.prototype.getMods = function (parent) {
142
- return this.mod_dict[parent] || [];
143
- };
144
-
145
- /**
146
- * 检查配置项
147
- * @param {object} config 模块配置项
148
- * @throws {Error} 模块配置项错误
149
- */
150
- Registry.prototype.checkConfig = function (config) {
151
- let {
152
- name,
153
- title,
154
- auto_start,
155
- config_file
156
- } = config;
157
- if (!name) {
158
- throw new TypeError('模块名称不能为空');
159
- }
160
- if (!title) {
161
- throw new TypeError('模块标题不能为空');
162
- }
163
- if (!config_file) {
164
- throw new TypeError('模块配置文件不能为空');
165
- }
166
- var valid_auto = ['true', 'false', 'auto'];
167
- if (valid_auto.indexOf(auto_start) === -1) {
168
- throw new TypeError('模块自动启动配置错误,只能为true、false、auto');
169
- }
170
- };
171
-
172
- /**
173
- * 补全配置项
174
- * @param {object} config 模块配置项
175
- * @returns {object} 模块配置项
176
- */
177
- Registry.prototype.fullConfig = function (config) {
178
- let cg = { ...this.newModel(), ...config };
179
- let {
180
- description,
181
- version,
182
- state,
183
- main,
184
- dependencies
185
- } = cg;
186
- if (!description) {
187
- cg.description = '';
188
- }
189
- if (!version) {
190
- cg.version = '1.0.0';
191
- }
192
- if (!main) {
193
- cg.main = '';
194
- }
195
- if (state) {
196
- cg.state = 1;
197
- }
198
- else {
199
- cg.state = 0;
200
- }
201
- if (!dependencies) {
202
- cg.dependencies = {};
203
- }
204
- return cg;
205
- };
206
-
207
- /**
208
- * 获取注册表目录
209
- * @returns {string} 注册表目录
210
- */
211
- Registry.prototype.getDir = function () {
212
- return this.config.dir.fullname();
213
- };
214
-
215
- /**
216
- * 获取模块文件
217
- * @returns {string} 模块文件路径
218
- */
219
- Registry.prototype.getModFile = function () {
220
- let file_name = this.config.name + '_module.json';
221
- let file = file_name.fullname(this.getDir());
222
- return file;
223
- };
224
-
225
- /**
226
- * 加载注册数据
227
- * 从 JSON 文件读取持久化数据
228
- */
229
- Registry.prototype.load = function () {
230
- this.read();
231
- };
232
-
233
- /**
234
- * 读取模块
235
- * @returns {Array} 模块配置项数组
236
- */
237
- Registry.prototype.read = function () {
238
- this.mod_dict = this.getModFile().loadJson() || {};
239
- };
240
-
241
- /**
242
- * 保存模块
243
- * @param {object} config 模块配置项
244
- * @returns {object} 模块配置项
245
- */
246
- Registry.prototype.save = function () {
247
- try {
248
- this.getModFile().saveJson(this.mod_dict);
249
- } catch (err) {
250
- console.error('保存注册数据失败: ' + err.message);
251
- }
252
- };
253
-
254
- /**
255
- * 排序模块
256
- */
257
- Registry.prototype.sort = function () {
258
- for (var parent in this.mod_dict) {
259
- this.mod_dict[parent].sort((a, b) => {
260
- if (a.sort !== b.sort) {
261
- return a.sort - b.sort;
262
- }
263
- if (a.name !== b.name) {
264
- return a.name.localeCompare(b.name);
265
- }
266
- return 0;
267
- });
268
- }
269
- };
270
-
271
- module.exports = {
272
- Registry
273
- };
File without changes
File without changes