mm_machine 3.1.8 → 3.1.9

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 (3) hide show
  1. package/manager.js +29 -33
  2. package/package.json +1 -1
  3. package/registry.js +22 -13
package/manager.js CHANGED
@@ -72,11 +72,6 @@ class Manager extends Mod {
72
72
  * @type {number}
73
73
  */
74
74
  mode: 3,
75
- /**
76
- * 排序项
77
- * @type {string}
78
- */
79
- sort_key: 'sort',
80
75
  /**
81
76
  * 是否启用注册表集成
82
77
  * true 时自动从 $.registry 读取/写入模块配置
@@ -350,40 +345,37 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
350
345
  if (!mod) return null;
351
346
  this.setMod(mod.config.name, mod);
352
347
 
348
+ var entry = {
349
+ name: mod.config.name,
350
+ title: mod.config.title,
351
+ description: mod.config.description,
352
+ sort: mod.config.sort,
353
+ state: mod.config.state,
354
+ config_file: config_file,
355
+ auto_start: mod.config.auto_start || 'auto',
356
+ main: mod.config.main || ''
357
+ };
358
+ // 获取触发键
359
+ let trigger = this.config.trigger;
360
+ if (trigger && trigger.length > 0) {
361
+ for (let t of trigger) {
362
+ entry.trigger[t] = mod.config[t];
363
+ }
364
+ }
353
365
  // 启用注册表时,自动写入 $.registry 供后续实例复用
354
366
  if (this.config.use_registry) {
355
367
  // 直接通过 registry 的 getMod 判断是否已存在,避免 _getInfos 重复
356
368
  if (!$.registry.getMod(this.getParentKey(), mod.config.name)) {
357
- var entry = {
358
- name: mod.config.name,
359
- title: mod.config.title,
360
- description: mod.config.description,
361
- sort: mod.config.sort,
362
- state: mod.config.state,
363
- config_file: config_file,
364
- main: mod.config.main || ''
365
- };
366
- // 获取触发键
367
- let trigger = this.config.trigger;
368
- if (trigger && trigger.length > 0) {
369
- for (let t of trigger) {
370
- entry.trigger[t] = mod.config[t];
371
- }
369
+ try {
370
+ // 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
371
+ $.registry.setMod(this.getParentKey(), mod.config.name, entry);
372
+ } catch (err) {
373
+ throw new Error(
374
+ '注册模块失败: ' + config_file + ' - ' + err.message
375
+ );
372
376
  }
373
- // 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
374
- $.registry.setMod(this.getParentKey(), mod.config.name, entry);
375
377
  }
376
378
  } else {
377
- // 不使用注册表时,写入内部缓存
378
- var entry = {
379
- name: mod.config.name,
380
- title: mod.config.title,
381
- description: mod.config.description,
382
- sort: mod.config.sort,
383
- state: mod.config.state,
384
- config_file: config_file,
385
- main: mod.config.main || ''
386
- };
387
379
  this.infos.push(entry);
388
380
  }
389
381
 
@@ -453,7 +445,11 @@ Manager.prototype.unregMod = async function (name) {
453
445
  */
454
446
  Manager.prototype.regMods = function (files, config = {}, script = {}) {
455
447
  for (let file of files) {
456
- this.regMod(file, config, script);
448
+ try {
449
+ this.regMod(file, config, script);
450
+ } catch (err) {
451
+ this.log('error', '批量注册模块失败: ' + file, err);
452
+ }
457
453
  }
458
454
  };
459
455
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.1.8",
3
+ "version": "3.1.9",
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": {
package/registry.js CHANGED
@@ -68,8 +68,8 @@ Registry.prototype.setMod = function (parent, name, config) {
68
68
  else if (typeof config !== 'object') {
69
69
  throw new TypeError('模块配置项必须为对象');
70
70
  }
71
- let cg = this.checkConfig(config);
72
- cg.parent = parent;
71
+ this.checkConfig(config);
72
+ let cg = this._fullValue(config);
73
73
 
74
74
  // 按 parent 分组存储到 mod_dict
75
75
  if (!this.mod_dict[parent]) {
@@ -146,22 +146,15 @@ Registry.prototype.getMods = function (parent) {
146
146
  /**
147
147
  * 检查配置项
148
148
  * @param {object} config 模块配置项
149
- * @returns {object} 模块配置项
150
149
  * @throws {Error} 模块配置项错误
151
150
  */
152
151
  Registry.prototype.checkConfig = function (config) {
153
- let cg = { ...this.newModel(), ...config };
154
152
  let {
155
153
  name,
156
154
  title,
157
- description,
158
- version,
159
- state,
160
155
  auto_start,
161
- config_file,
162
- main,
163
- dependencies
164
- } = cg;
156
+ config_file
157
+ } = config;
165
158
  if (!name) {
166
159
  throw new TypeError('模块名称不能为空');
167
160
  }
@@ -172,11 +165,27 @@ Registry.prototype.checkConfig = function (config) {
172
165
  throw new TypeError('模块配置文件不能为空');
173
166
  }
174
167
  var valid_auto = ['true', 'false', 'auto'];
175
- if (valid_auto.indexOf(auto_start) === -1 && ![true, false].includes(auto_start)) {
168
+ if (valid_auto.indexOf(auto_start) === -1) {
176
169
  throw new TypeError('模块自动启动配置错误,只能为true、false、auto');
177
170
  }
171
+ };
172
+
173
+ /**
174
+ * 补全配置项
175
+ * @param {object} config 模块配置项
176
+ * @returns {object} 模块配置项
177
+ */
178
+ Registry.prototype._fullValue = function (config) {
179
+ let cg = { ...this.newModel(), ...config };
180
+ let {
181
+ description,
182
+ version,
183
+ state,
184
+ main,
185
+ dependencies
186
+ } = cg;
178
187
  if (!description) {
179
- description = '';
188
+ cg.description = '';
180
189
  }
181
190
  if (!version) {
182
191
  cg.version = '1.0.0';