mm_machine 3.1.8 → 3.2.0
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/manager.js +39 -39
- package/package.json +1 -1
- package/registry.js +22 -14
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 读取/写入模块配置
|
|
@@ -348,42 +343,43 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
|
|
|
348
343
|
|
|
349
344
|
var mod = this.newMod(config_file, cg, script);
|
|
350
345
|
if (!mod) return null;
|
|
351
|
-
this.setMod(mod.config.name, mod);
|
|
352
346
|
|
|
347
|
+
// 模块已存在时不替换内存实例,避免破坏已初始化的模块
|
|
348
|
+
if (!this.mods[mod.config.name]) {
|
|
349
|
+
this.setMod(mod.config.name, mod);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
var entry = {
|
|
353
|
+
name: mod.config.name,
|
|
354
|
+
title: mod.config.title,
|
|
355
|
+
description: mod.config.description,
|
|
356
|
+
sort: mod.config.sort,
|
|
357
|
+
state: mod.config.state,
|
|
358
|
+
config_file: config_file,
|
|
359
|
+
auto_start: mod.config.auto_start || 'auto',
|
|
360
|
+
main: mod.config.main || ''
|
|
361
|
+
};
|
|
362
|
+
// 获取触发键
|
|
363
|
+
let trigger = this.config.trigger;
|
|
364
|
+
if (trigger && trigger.length > 0) {
|
|
365
|
+
for (let t of trigger) {
|
|
366
|
+
entry.trigger[t] = mod.config[t];
|
|
367
|
+
}
|
|
368
|
+
}
|
|
353
369
|
// 启用注册表时,自动写入 $.registry 供后续实例复用
|
|
354
370
|
if (this.config.use_registry) {
|
|
355
371
|
// 直接通过 registry 的 getMod 判断是否已存在,避免 _getInfos 重复
|
|
356
372
|
if (!$.registry.getMod(this.getParentKey(), mod.config.name)) {
|
|
357
|
-
|
|
358
|
-
name
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
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
|
-
}
|
|
373
|
+
try {
|
|
374
|
+
// 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
|
|
375
|
+
$.registry.setMod(this.getParentKey(), mod.config.name, entry);
|
|
376
|
+
} catch (err) {
|
|
377
|
+
throw new Error(
|
|
378
|
+
'注册模块失败: ' + config_file + ' - ' + err.message
|
|
379
|
+
);
|
|
372
380
|
}
|
|
373
|
-
// 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
|
|
374
|
-
$.registry.setMod(this.getParentKey(), mod.config.name, entry);
|
|
375
381
|
}
|
|
376
382
|
} 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
383
|
this.infos.push(entry);
|
|
388
384
|
}
|
|
389
385
|
|
|
@@ -453,7 +449,11 @@ Manager.prototype.unregMod = async function (name) {
|
|
|
453
449
|
*/
|
|
454
450
|
Manager.prototype.regMods = function (files, config = {}, script = {}) {
|
|
455
451
|
for (let file of files) {
|
|
456
|
-
|
|
452
|
+
try {
|
|
453
|
+
this.regMod(file, config, script);
|
|
454
|
+
} catch (err) {
|
|
455
|
+
this.log('error', '批量注册模块失败: ' + file, err);
|
|
456
|
+
}
|
|
457
457
|
}
|
|
458
458
|
};
|
|
459
459
|
|
|
@@ -707,27 +707,27 @@ Manager.prototype._loadSources = async function () {
|
|
|
707
707
|
// 只有从注册表成功加载到模块时才跳过文件系统扫描,否则回退到文件系统
|
|
708
708
|
if (this._loadFromRegistry()) {
|
|
709
709
|
// 后台异步扫描文件系统,确保物理最新
|
|
710
|
-
setImmediate(() => this._scanForUpdate());
|
|
710
|
+
setImmediate(() => this._scanForUpdate(false));
|
|
711
711
|
return;
|
|
712
712
|
}
|
|
713
713
|
}
|
|
714
714
|
|
|
715
715
|
// 注册表无匹配,走文件系统扫描
|
|
716
|
-
await this._scanForUpdate();
|
|
716
|
+
await this._scanForUpdate(false);
|
|
717
717
|
};
|
|
718
718
|
|
|
719
719
|
/**
|
|
720
720
|
* 后台扫描文件系统并同步到注册表
|
|
721
721
|
* 不阻塞启动,让注册表始终与磁盘保持同步
|
|
722
722
|
*/
|
|
723
|
-
Manager.prototype._scanForUpdate = async function () {
|
|
723
|
+
Manager.prototype._scanForUpdate = async function (clear = false) {
|
|
724
724
|
var base_dir = this.getBaseDir();
|
|
725
725
|
var dir = this.getDir();
|
|
726
726
|
try {
|
|
727
727
|
if (base_dir) {
|
|
728
|
-
await Promise.all([this.call('update', base_dir,
|
|
728
|
+
await Promise.all([this.call('update', base_dir, clear), this.call('update', dir, clear)]);
|
|
729
729
|
} else {
|
|
730
|
-
await this.call('update', dir,
|
|
730
|
+
await this.call('update', dir, clear);
|
|
731
731
|
}
|
|
732
732
|
if (this.config.use_registry) {
|
|
733
733
|
$.registry.save();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_machine",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
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
|
@@ -28,7 +28,6 @@ class Registry {
|
|
|
28
28
|
*/
|
|
29
29
|
Registry.prototype.newModel = function () {
|
|
30
30
|
return {
|
|
31
|
-
parent: '',
|
|
32
31
|
name: '',
|
|
33
32
|
title: '',
|
|
34
33
|
description: '',
|
|
@@ -68,8 +67,8 @@ Registry.prototype.setMod = function (parent, name, config) {
|
|
|
68
67
|
else if (typeof config !== 'object') {
|
|
69
68
|
throw new TypeError('模块配置项必须为对象');
|
|
70
69
|
}
|
|
71
|
-
|
|
72
|
-
cg
|
|
70
|
+
this.checkConfig(config);
|
|
71
|
+
let cg = this._fullValue(config);
|
|
73
72
|
|
|
74
73
|
// 按 parent 分组存储到 mod_dict
|
|
75
74
|
if (!this.mod_dict[parent]) {
|
|
@@ -146,22 +145,15 @@ Registry.prototype.getMods = function (parent) {
|
|
|
146
145
|
/**
|
|
147
146
|
* 检查配置项
|
|
148
147
|
* @param {object} config 模块配置项
|
|
149
|
-
* @returns {object} 模块配置项
|
|
150
148
|
* @throws {Error} 模块配置项错误
|
|
151
149
|
*/
|
|
152
150
|
Registry.prototype.checkConfig = function (config) {
|
|
153
|
-
let cg = { ...this.newModel(), ...config };
|
|
154
151
|
let {
|
|
155
152
|
name,
|
|
156
153
|
title,
|
|
157
|
-
description,
|
|
158
|
-
version,
|
|
159
|
-
state,
|
|
160
154
|
auto_start,
|
|
161
|
-
config_file
|
|
162
|
-
|
|
163
|
-
dependencies
|
|
164
|
-
} = cg;
|
|
155
|
+
config_file
|
|
156
|
+
} = config;
|
|
165
157
|
if (!name) {
|
|
166
158
|
throw new TypeError('模块名称不能为空');
|
|
167
159
|
}
|
|
@@ -172,11 +164,27 @@ Registry.prototype.checkConfig = function (config) {
|
|
|
172
164
|
throw new TypeError('模块配置文件不能为空');
|
|
173
165
|
}
|
|
174
166
|
var valid_auto = ['true', 'false', 'auto'];
|
|
175
|
-
if (valid_auto.indexOf(auto_start) === -1
|
|
167
|
+
if (valid_auto.indexOf(auto_start) === -1) {
|
|
176
168
|
throw new TypeError('模块自动启动配置错误,只能为true、false、auto');
|
|
177
169
|
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 补全配置项
|
|
174
|
+
* @param {object} config 模块配置项
|
|
175
|
+
* @returns {object} 模块配置项
|
|
176
|
+
*/
|
|
177
|
+
Registry.prototype._fullValue = function (config) {
|
|
178
|
+
let cg = { ...this.newModel(), ...config };
|
|
179
|
+
let {
|
|
180
|
+
description,
|
|
181
|
+
version,
|
|
182
|
+
state,
|
|
183
|
+
main,
|
|
184
|
+
dependencies
|
|
185
|
+
} = cg;
|
|
178
186
|
if (!description) {
|
|
179
|
-
description = '';
|
|
187
|
+
cg.description = '';
|
|
180
188
|
}
|
|
181
189
|
if (!version) {
|
|
182
190
|
cg.version = '1.0.0';
|