mm_machine 3.2.4 → 3.2.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.
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
  };
@@ -37,7 +37,13 @@ class Drive extends Mod {
37
37
  // 是否结束,可选,默认false
38
38
  end: false
39
39
  };
40
+ // 当前配置文件路径
41
+ config_file = '';
40
42
 
43
+ // 当前脚本文件路径
44
+ script_file = '';
45
+ // 目录缓存
46
+ _dir = undefined;
41
47
  /**
42
48
  * 构造函数
43
49
  * @param {object} config 配置项
@@ -46,12 +52,6 @@ class Drive extends Mod {
46
52
  */
47
53
  constructor(config, parent) {
48
54
  super({ ...Drive.config, ...config }, parent);
49
- // 当前配置文件路径
50
- this.config_file = '';
51
-
52
- // 当前脚本文件路径
53
- this.script_file = '';
54
-
55
55
  // 模式: 1.生产模式 2.热更新模式 3.热重载模式 4.热更新+重载模式 5.重载模式
56
56
  this.mode = 1;
57
57
  }
@@ -116,7 +116,7 @@ Drive.prototype.setConfig = function (config) {
116
116
  if (config) {
117
117
  // 如果main改变,重置加载状态
118
118
  if (config.main !== this.config.main) {
119
- this._is_loaded = false;
119
+ this.isLoaded(false);
120
120
  }
121
121
 
122
122
  if (this.config_file) {
@@ -132,6 +132,10 @@ Drive.prototype.setConfig = function (config) {
132
132
  Object.assign(this.config, config);
133
133
  }
134
134
  }
135
+ let file = this.config.main;
136
+ if (file) {
137
+ this.script_file = file.fullname(this.getDir());
138
+ }
135
139
  this._preset();
136
140
  return this.config;
137
141
  };
@@ -203,8 +207,7 @@ Drive.prototype._remove = function () {
203
207
  */
204
208
  Drive.prototype.unloadScript = function () {
205
209
  this._remove();
206
- this._script_keys = null;
207
- this._is_loaded = false;
210
+ this.isLoaded(false);
208
211
  };
209
212
 
210
213
  /**
@@ -348,22 +351,6 @@ Drive.prototype._setMainMethod = function (mod) {
348
351
  if (name && typeof mod[name] === 'function') {
349
352
  this.main = mod[name];
350
353
  }
351
- var new_keys = Object.keys(mod);
352
- // 热重载时如果脚本未变化则跳过重建
353
- if (this._script_keys && this._script_keys.length === new_keys.length) {
354
- var same = true;
355
- for (var i = 0; i < new_keys.length; i++) {
356
- if (this._script_keys[i] !== new_keys[i]) {
357
- same = false;
358
- break;
359
- }
360
- }
361
- if (same) {
362
- this._is_loaded = true;
363
- return;
364
- }
365
- }
366
- this._script_keys = new_keys;
367
354
  for (var key in mod) {
368
355
  var obj = mod[key];
369
356
  if (typeof obj === 'function') {
@@ -372,7 +359,7 @@ Drive.prototype._setMainMethod = function (mod) {
372
359
  this.setData(key, obj);
373
360
  }
374
361
  }
375
- this._is_loaded = true;
362
+ this.isLoaded(true);
376
363
  };
377
364
 
378
365
  /**
@@ -665,11 +652,11 @@ Drive.prototype.loadBefore = function () {
665
652
  try {
666
653
  let mod = this.loadScript();
667
654
  if (mod) {
668
- this._is_loaded = true;
655
+ this.isLoaded(true);
669
656
  }
670
657
  } catch (err) {
671
658
  this.log('error', `加载前处理失败`, this._wrapError(err));
672
- this._is_loaded = false;
659
+ this.isLoaded(false);
673
660
  }
674
661
  };
675
662
 
@@ -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}
@@ -144,18 +137,18 @@ Manager.prototype.getInfos = function () {
144
137
  * @returns {object[]} 模块对象数组
145
138
  */
146
139
  Manager.prototype.getList = function (keyword) {
147
- let entries = this._getInfos();
140
+ let infos = this._getInfos();
148
141
 
149
142
  // 先用 entry 做关键词过滤,避免无效实例化
150
143
  if (keyword) {
151
- entries = entries.filter((o) => {
144
+ infos = infos.filter((o) => {
152
145
  return o.name.includes(keyword) || o.title.includes(keyword) || o.description.includes(keyword);
153
146
  });
154
147
  }
155
148
 
156
149
  var list = [];
157
- for (var i = 0; i < entries.length; i++) {
158
- var mod = this.getMod(entries[i].name);
150
+ for (var i = 0; i < infos.length; i++) {
151
+ var mod = this.getMod(infos[i].name);
159
152
  if (!mod) continue;
160
153
  list.push(mod);
161
154
  }
@@ -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 entries = this._getInfos();
258
- for (var i = 0; i < entries.length; i++) {
259
- if (entries[i].name === name) {
260
- mod = this.newMod(entries[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);
@@ -357,20 +323,7 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
357
323
  entry[key] = mod.config[key];
358
324
  }
359
325
  }
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
- }
370
- }
371
- } else {
372
- this.infos.push(entry);
373
- }
326
+ this.infos.push(entry);
374
327
 
375
328
  return mod;
376
329
  };
@@ -388,6 +341,14 @@ Manager.prototype.loadMod = async function (name) {
388
341
  return null;
389
342
  };
390
343
 
344
+ /**
345
+ * 初始化模块
346
+ * @param {object} mod 模块对象
347
+ */
348
+ Manager.prototype._initMod = async function (mod) {
349
+ await mod.do('init', this.getParent());
350
+ };
351
+
391
352
  /**
392
353
  * 卸载模块
393
354
  * @param {string} name 模块名
@@ -516,28 +477,6 @@ Manager.prototype.update = async function (dir, clear = true) {
516
477
  }
517
478
  };
518
479
 
519
- /**
520
- * 从 registry 缓存加载模块(按 parent 字段匹配)
521
- * 从 registry.mod_dict 中按 parent 过滤并创建模块实例
522
- * @returns {boolean} 是否加载到模块
523
- */
524
- Manager.prototype._loadFromRegistry = function () {
525
- let lt = this._getInfos();
526
- if (!lt.length) return false;
527
-
528
- for (var i = 0; i < lt.length; i++) {
529
- var entry = lt[i];
530
- if (!this.config.lazy_load) {
531
- let mod = this.newMod(entry.config_file);
532
- if (mod) {
533
- this.setMod(mod.config.name, mod);
534
- }
535
- }
536
- }
537
-
538
- return true;
539
- };
540
-
541
480
  /**
542
481
  * 检索目录下所有文件
543
482
  * @private
@@ -588,9 +527,9 @@ Manager.prototype.main = async function (name, method, ...params) {
588
527
  */
589
528
  Manager.prototype._runAllEnabled = async function (method, ...params) {
590
529
  var result;
591
- var entries = this._getInfos();
592
- for (var i = 0; i < entries.length; i++) {
593
- var entry = entries[i];
530
+ var infos = this._getInfos();
531
+ for (var i = 0; i < infos.length; i++) {
532
+ var entry = infos[i];
594
533
  // 直接用 entry.state 过滤,避免无效实例化
595
534
  if (entry.state !== 1) continue;
596
535
 
@@ -638,6 +577,11 @@ Manager.prototype._runMod = async function (mod, method, ...params) {
638
577
  }
639
578
  }
640
579
 
580
+ // 确保模块已初始化
581
+ if (!mod.isInited() && method !== 'init') {
582
+ await mod.do('init');
583
+ }
584
+
641
585
  // 执行方法
642
586
  let ret = await mod.call(method, ...params);
643
587
  // 根据模式决定是否重载
@@ -664,26 +608,13 @@ Manager.prototype.getDir = function () {
664
608
 
665
609
  /**
666
610
  * 加载资源
667
- * 启用注册表时优先从注册表读取,快速启动;
668
- * 同时在后台异步扫描文件系统,确保注册表数据为物理最新。
669
611
  */
670
612
  Manager.prototype._loadSources = async function () {
671
- // 启用注册表且有缓存数据时,走内存快路径,快速启动;
672
- // 后台异步扫描文件系统更新注册表,确保物理最新。
673
- if (this.config.use_registry) {
674
- if (this._loadFromRegistry()) {
675
- setImmediate(() => this._scanForUpdate(false));
676
- return;
677
- }
678
- }
679
-
680
- // 注册表无缓存,同步扫描文件系统
681
613
  await this._scanForUpdate();
682
614
  };
683
615
 
684
616
  /**
685
- * 后台扫描文件系统并同步到注册表
686
- * 不阻塞启动,让注册表始终与磁盘保持同步
617
+ * 后台扫描文件系统
687
618
  */
688
619
  Manager.prototype._scanForUpdate = async function (clear = true) {
689
620
  var base_dir = this.getBaseDir();
@@ -694,9 +625,6 @@ Manager.prototype._scanForUpdate = async function (clear = true) {
694
625
  } else {
695
626
  await this.call('update', dir, clear);
696
627
  }
697
- if (this.config.use_registry) {
698
- $.registry.save();
699
- }
700
628
  } catch (err) {
701
629
  this.log('error', '后台扫描同步失败', err);
702
630
  }
@@ -786,10 +714,10 @@ Manager.prototype._ensureModLoaded = async function (name) {
786
714
  */
787
715
  Manager.prototype._ensureAllLoaded = async function () {
788
716
  var promises = [];
789
- var entries = this._getInfos();
717
+ var infos = this._getInfos();
790
718
 
791
- for (var i = 0; i < entries.length; i++) {
792
- promises.push(this._ensureModLoaded(entries[i].name));
719
+ for (var i = 0; i < infos.length; i++) {
720
+ promises.push(this._ensureModLoaded(infos[i].name));
793
721
  }
794
722
  await Promise.all(promises);
795
723
  };
@@ -806,10 +734,10 @@ Manager.prototype.runWait = async function (method, ...params) {
806
734
  }
807
735
 
808
736
  let result;
809
- var entries = this._getInfos();
737
+ var infos = this._getInfos();
810
738
 
811
- for (var i = 0; i < entries.length; i++) {
812
- let mod = await this._ensureModLoaded(entries[i].name);
739
+ for (var i = 0; i < infos.length; i++) {
740
+ let mod = await this._ensureModLoaded(infos[i].name);
813
741
  if (!mod) continue;
814
742
 
815
743
  try {
@@ -818,7 +746,7 @@ Manager.prototype.runWait = async function (method, ...params) {
818
746
  result = ret;
819
747
  }
820
748
  } catch (error) {
821
- this._logModError(entries[i].name, method, error);
749
+ this._logModError(infos[i].name, method, error);
822
750
  }
823
751
  }
824
752
 
@@ -839,14 +767,14 @@ Manager.prototype.runAsync = async function (method, ...params) {
839
767
  // 确保所有模块都已加载
840
768
  await this._ensureAllLoaded();
841
769
 
842
- var entries = this._getInfos();
770
+ var infos = this._getInfos();
843
771
 
844
- for (var i = 0; i < entries.length; i++) {
845
- var mod = this.mods[entries[i].name];
772
+ for (var i = 0; i < infos.length; i++) {
773
+ var mod = this.mods[infos[i].name];
846
774
  if (!mod) continue;
847
775
 
848
776
  mod.do(method, ...params).catch((error) => {
849
- this._logModError(entries[i].name, method, error);
777
+ this._logModError(infos[i].name, method, error);
850
778
  });
851
779
  }
852
780
 
@@ -868,10 +796,10 @@ Manager.prototype.runAll = async function (method, ...params) {
868
796
  await this._ensureAllLoaded();
869
797
 
870
798
  const promises = [];
871
- var entries = this._getInfos();
799
+ var infos = this._getInfos();
872
800
 
873
- for (var i = 0; i < entries.length; i++) {
874
- var mod = this.mods[entries[i].name];
801
+ for (var i = 0; i < infos.length; i++) {
802
+ var mod = this.mods[infos[i].name];
875
803
  if (!mod) continue;
876
804
 
877
805
  promises.push(mod.do(method, ...params));
@@ -895,10 +823,10 @@ Manager.prototype.runRace = async function (method, ...params) {
895
823
  await this._ensureAllLoaded();
896
824
 
897
825
  const promises = [];
898
- var entries = this._getInfos();
826
+ var infos = this._getInfos();
899
827
 
900
- for (var i = 0; i < entries.length; i++) {
901
- var mod = this.mods[entries[i].name];
828
+ for (var i = 0; i < infos.length; i++) {
829
+ var mod = this.mods[infos[i].name];
902
830
  if (!mod) continue;
903
831
 
904
832
  promises.push(
@@ -923,10 +851,10 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
923
851
  throw new TypeError('方法名称必须是字符串');
924
852
  }
925
853
 
926
- var entries = this._getInfos();
854
+ var infos = this._getInfos();
927
855
 
928
- for (var i = 0; i < entries.length; i++) {
929
- var mod = await this._ensureModLoaded(entries[i].name);
856
+ for (var i = 0; i < infos.length; i++) {
857
+ var mod = await this._ensureModLoaded(infos[i].name);
930
858
  if (!mod) continue;
931
859
 
932
860
  try {
@@ -935,7 +863,7 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
935
863
  result = ret;
936
864
  }
937
865
  } catch (error) {
938
- this._logModError(entries[i].name, method, error);
866
+ this._logModError(infos[i].name, method, error);
939
867
  }
940
868
  }
941
869
 
@@ -75,6 +75,9 @@ class Mod extends Base {
75
75
  cache_file: ''
76
76
  };
77
77
 
78
+ _is_loaded = false;
79
+ _is_inited = false;
80
+
78
81
  /**
79
82
  * 构造函数
80
83
  * @param {object} config 配置对象
@@ -91,17 +94,37 @@ class Mod extends Base {
91
94
  this._methods = {};
92
95
  // 脚本数据
93
96
  this._datas = {};
94
- this._is_loaded = false;
95
97
  }
96
- }
97
98
 
98
- /**
99
- * 检查模块是否已加载
100
- * @returns {boolean} 是否已加载
101
- */
102
- Mod.prototype.isLoaded = function () {
103
- return this._is_loaded;
104
- };
99
+ /**
100
+ * 是否已加载
101
+ * @param {boolean} bl 是否已加载
102
+ * @returns {boolean} 是否已加载
103
+ */
104
+ isLoaded(bl) {
105
+ if (bl === undefined) {
106
+ return this._is_loaded;
107
+ }
108
+ this._is_loaded = bl;
109
+ if (!bl) {
110
+ this._is_inited = false;
111
+ }
112
+ return bl;
113
+ }
114
+
115
+ /**
116
+ * 是否已初始化
117
+ * @param {boolean} bl 是否已初始化
118
+ * @returns {boolean} 是否已初始化
119
+ */
120
+ isInited(bl) {
121
+ if (bl === undefined) {
122
+ return this._is_inited;
123
+ }
124
+ this._is_inited = bl;
125
+ return bl;
126
+ }
127
+ }
105
128
 
106
129
  /**
107
130
  * 日志输出
@@ -176,7 +199,24 @@ Mod.prototype._onLoad = async function (ctx) {
176
199
  });
177
200
  this.on('load:after', (ctx) => {
178
201
  this.setState('loaded');
179
- this._is_loaded = true;
202
+ this.isLoaded(true);
203
+ });
204
+ };
205
+
206
+ /**
207
+ * 监听初始化事件
208
+ */
209
+ Base.prototype._onInit = function () {
210
+ // 初始化
211
+ this.on('init:before', async (ctx) => {
212
+ this.setState('initing');
213
+ await this._initCore(...ctx.params);
214
+ });
215
+ this.on('init:success', (ctx) => {
216
+ this.isInited(true);
217
+ });
218
+ this.on('init:after', (ctx) => {
219
+ this.setState('inited');
180
220
  });
181
221
  };
182
222
 
@@ -269,7 +309,7 @@ Mod.prototype._onUnload = async function (ctx) {
269
309
  this.on('unload:after', async (ctx) => {
270
310
  this.setState('unloaded');
271
311
  await this._unloadCore(...ctx.params);
272
- this._is_loaded = false;
312
+ this.isLoaded(false);
273
313
  });
274
314
  };
275
315
 
@@ -297,7 +337,9 @@ Mod.prototype.load = async function (...args) {
297
337
  * @private
298
338
  * @param {...any} args 加载参数
299
339
  */
300
- Mod.prototype._loadCore = async function (...args) {};
340
+ Mod.prototype._loadCore = async function (...args) {
341
+
342
+ };
301
343
 
302
344
  /**
303
345
  * 启动 - 内部方法
@@ -323,7 +365,7 @@ Mod.prototype.start = async function (...args) {
323
365
  * @private
324
366
  * @param {...any} args 初始化参数
325
367
  */
326
- Mod.prototype._startCore = async function (...args) {};
368
+ Mod.prototype._startCore = async function (...args) { };
327
369
 
328
370
  /**
329
371
  * 停止 - 内部方法
@@ -346,7 +388,7 @@ Mod.prototype.stop = async function () {
346
388
  * 停止核心
347
389
  * @private
348
390
  */
349
- Mod.prototype._stopCore = async function () {};
391
+ Mod.prototype._stopCore = async function () { };
350
392
 
351
393
  /**
352
394
  * 销毁核心
@@ -401,8 +443,7 @@ Mod.prototype._unloadCore = async function () {
401
443
  Mod.prototype.reload = async function () {
402
444
  try {
403
445
  this._unloadCore();
404
- this._is_loaded = false;
405
- this._script_keys = null;
446
+ this.isLoaded(false);
406
447
  } catch (err) {
407
448
  this.log('error', '重载失败', err);
408
449
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.2.4",
3
+ "version": "3.2.6",
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": {
@@ -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": {
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