mm_machine 3.5.4 → 3.5.5
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/README.md +1 -1
- package/example/index.js +69 -0
- package/example/modules/hello/index.js +50 -0
- package/example/modules/hello/manager.json +24 -0
- package/index.js +3 -3
- package/lib/drive.js +144 -97
- package/lib/manager.js +122 -50
- package/lib/mod.js +693 -168
- package/lib/scanner.js +5 -5
- package/package.json +15 -5
- package/lib/auto_require.js +0 -174
package/lib/manager.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const { Manager: Core } = require('mm_mod');
|
|
2
|
+
const { hasDir, fullname } = require('mm_helper');
|
|
3
|
+
const { Mod, addModMethods } = require('./mod.js');
|
|
2
4
|
const { Drive, MODE } = require('./drive.js');
|
|
3
5
|
const { Scanner } = require('./scanner.js');
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* 管理器类
|
|
7
9
|
* @class
|
|
8
|
-
* @
|
|
10
|
+
* @augments Core
|
|
9
11
|
* @param {object} config 配置项
|
|
10
12
|
* @param {object} parent 父项对象
|
|
11
13
|
* @param {object} mods 模块对象列表
|
|
12
14
|
* @param {object} Drive 驱动对象
|
|
13
15
|
*/
|
|
14
|
-
class Manager extends
|
|
16
|
+
class Manager extends Core {
|
|
15
17
|
/**
|
|
16
18
|
* 配置项
|
|
17
19
|
* @type {object}
|
|
@@ -81,7 +83,7 @@ class Manager extends Mod {
|
|
|
81
83
|
* 作用域
|
|
82
84
|
* @type {string}
|
|
83
85
|
*/
|
|
84
|
-
scope:
|
|
86
|
+
scope: 'server',
|
|
85
87
|
/**
|
|
86
88
|
* 最大搜索深度 -1表示不限制深度
|
|
87
89
|
* @type {number}
|
|
@@ -98,10 +100,10 @@ class Manager extends Mod {
|
|
|
98
100
|
* 构造函数
|
|
99
101
|
* @param {object} config 配置项
|
|
100
102
|
* @param {object} parent 父项对象
|
|
101
|
-
* @param {object} mods
|
|
102
|
-
* @param {object} Drive 驱动对象
|
|
103
|
+
* @param {object} [mods] 模块对象列表,传入时作为受管记录的实例表(保持外部引用)
|
|
104
|
+
* @param {object} [Drive] 驱动对象
|
|
103
105
|
*/
|
|
104
|
-
constructor(config, parent, mods
|
|
106
|
+
constructor(config, parent, mods, Drive) {
|
|
105
107
|
super(
|
|
106
108
|
{
|
|
107
109
|
...Manager.config,
|
|
@@ -110,25 +112,27 @@ class Manager extends Mod {
|
|
|
110
112
|
parent
|
|
111
113
|
);
|
|
112
114
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
115
|
+
if (parent) {
|
|
116
|
+
this.getParent = function () {
|
|
117
|
+
return parent;
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
// 脚本方法
|
|
121
|
+
this._methods = {};
|
|
122
|
+
// 脚本数据
|
|
123
|
+
this._datas = {};
|
|
124
|
+
// 外部传入实例表时,直接作为受管记录的实例表,保证外部引用同步
|
|
125
|
+
if (mods) {
|
|
126
|
+
this._getRecord().mods = mods;
|
|
127
|
+
}
|
|
118
128
|
if (Drive) {
|
|
119
129
|
this.Drive = Drive;
|
|
120
130
|
}
|
|
131
|
+
// 监听事件
|
|
132
|
+
this._listen();
|
|
121
133
|
}
|
|
122
134
|
}
|
|
123
135
|
|
|
124
|
-
/**
|
|
125
|
-
* 获取事件触发器
|
|
126
|
-
* @returns {object} 事件触发器
|
|
127
|
-
*/
|
|
128
|
-
Manager.prototype.getEventer = function () {
|
|
129
|
-
return $.eventer;
|
|
130
|
-
};
|
|
131
|
-
|
|
132
136
|
/**
|
|
133
137
|
* 获取父项键名
|
|
134
138
|
* @returns {string} 父项键名
|
|
@@ -151,7 +155,7 @@ Manager.prototype.getInfos = function () {
|
|
|
151
155
|
* @returns {object|null} 模块配置对象
|
|
152
156
|
*/
|
|
153
157
|
Manager.prototype.getInfo = function (name) {
|
|
154
|
-
return this.
|
|
158
|
+
return this._getInfos().find((o) => o.name === name) || null;
|
|
155
159
|
};
|
|
156
160
|
|
|
157
161
|
/**
|
|
@@ -199,25 +203,86 @@ Manager.prototype.getTplDir = function () {
|
|
|
199
203
|
*/
|
|
200
204
|
Manager.prototype.Drive = Drive;
|
|
201
205
|
|
|
206
|
+
/**
|
|
207
|
+
* 创建受管记录
|
|
208
|
+
* @description 在受管实例表之外,额外记录模块条目信息与插件集合
|
|
209
|
+
* @returns {object} 受管记录
|
|
210
|
+
*/
|
|
211
|
+
Manager.prototype._createManagedRecord = function () {
|
|
212
|
+
let record = Core.prototype._createManagedRecord.call(this);
|
|
213
|
+
record.infos = [];
|
|
214
|
+
record.plugins = {};
|
|
215
|
+
return record;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 获取插件集合
|
|
220
|
+
* @description 兼容旧记录,缺失时惰性补建
|
|
221
|
+
* @returns {object} 插件集合(应用名 => 插件对象)
|
|
222
|
+
*/
|
|
223
|
+
Manager.prototype._getPlugins = function () {
|
|
224
|
+
let record = this._getRecord();
|
|
225
|
+
if (!record.plugins) {
|
|
226
|
+
record.plugins = {};
|
|
227
|
+
}
|
|
228
|
+
return record.plugins;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 注册插件对象
|
|
233
|
+
* @param {string} name 应用名称
|
|
234
|
+
* @param {object} obj 插件对象
|
|
235
|
+
* @returns {Manager} 当前管理器
|
|
236
|
+
*/
|
|
237
|
+
Manager.prototype.usePlugin = function (name, obj) {
|
|
238
|
+
this._getPlugins()[name] = obj;
|
|
239
|
+
return this;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 插件集合
|
|
244
|
+
* @type {object}
|
|
245
|
+
*/
|
|
246
|
+
Object.defineProperty(Manager.prototype, 'plugins', {
|
|
247
|
+
/**
|
|
248
|
+
* 获取插件集合
|
|
249
|
+
* @returns {object} 插件集合
|
|
250
|
+
*/
|
|
251
|
+
get: function () {
|
|
252
|
+
return this._getPlugins();
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* 获取文件扫描器
|
|
258
|
+
* @returns {Scanner} 文件扫描器
|
|
259
|
+
*/
|
|
260
|
+
Manager.prototype._getScanner = function () {
|
|
261
|
+
return new Scanner(this.config.ignore, this.config.max_depth);
|
|
262
|
+
};
|
|
263
|
+
|
|
202
264
|
/**
|
|
203
265
|
* 获取模块配置列表
|
|
204
266
|
* @returns {Array} 排序后的模块配置数组
|
|
205
267
|
*/
|
|
206
268
|
Manager.prototype._getInfos = function () {
|
|
207
|
-
|
|
269
|
+
let record = this._getRecord();
|
|
270
|
+
if (!record.infos) {
|
|
271
|
+
record.infos = [];
|
|
272
|
+
}
|
|
273
|
+
return record.infos;
|
|
208
274
|
};
|
|
209
275
|
|
|
210
276
|
// ==== 模块管理接口 ====
|
|
211
277
|
/**
|
|
212
278
|
* 清除所有模块
|
|
279
|
+
* @returns {Promise<void>} 无返回值
|
|
213
280
|
*/
|
|
214
|
-
Manager.prototype.clearMods = function () {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
this.infos = [];
|
|
220
|
-
}
|
|
281
|
+
Manager.prototype.clearMods = async function () {
|
|
282
|
+
// 统一由受管记录释放模块实例
|
|
283
|
+
await this.clear();
|
|
284
|
+
let infos = this._getInfos();
|
|
285
|
+
infos.length = 0;
|
|
221
286
|
};
|
|
222
287
|
|
|
223
288
|
/**
|
|
@@ -226,11 +291,14 @@ Manager.prototype.clearMods = function () {
|
|
|
226
291
|
* @returns {object|null} 删除的模块对象
|
|
227
292
|
*/
|
|
228
293
|
Manager.prototype.delMod = function (name) {
|
|
229
|
-
let
|
|
294
|
+
let record = this._getRecord();
|
|
295
|
+
let mod = record.mods[name];
|
|
230
296
|
if (mod) {
|
|
231
|
-
delete
|
|
232
|
-
|
|
233
|
-
|
|
297
|
+
delete record.mods[name];
|
|
298
|
+
let infos = this._getInfos();
|
|
299
|
+
let index = infos.findIndex((o) => o.name === name);
|
|
300
|
+
if (index >= 0) {
|
|
301
|
+
infos.splice(index, 1);
|
|
234
302
|
}
|
|
235
303
|
}
|
|
236
304
|
return mod;
|
|
@@ -242,7 +310,7 @@ Manager.prototype.delMod = function (name) {
|
|
|
242
310
|
* @param {object} mod 模块对象
|
|
243
311
|
*/
|
|
244
312
|
Manager.prototype.setMod = function (name, mod) {
|
|
245
|
-
this.mods[name] = mod;
|
|
313
|
+
this._getRecord().mods[name] = mod;
|
|
246
314
|
};
|
|
247
315
|
|
|
248
316
|
/**
|
|
@@ -251,7 +319,7 @@ Manager.prototype.setMod = function (name, mod) {
|
|
|
251
319
|
* @returns {object} 模块对象
|
|
252
320
|
*/
|
|
253
321
|
Manager.prototype.getMod = function (name) {
|
|
254
|
-
return this.mods[name];
|
|
322
|
+
return this._getRecord().mods[name];
|
|
255
323
|
};
|
|
256
324
|
|
|
257
325
|
/**
|
|
@@ -259,7 +327,7 @@ Manager.prototype.getMod = function (name) {
|
|
|
259
327
|
* @returns {object[]} 模块对象数组
|
|
260
328
|
*/
|
|
261
329
|
Manager.prototype.getMods = function () {
|
|
262
|
-
return this.mods;
|
|
330
|
+
return this._getRecord().mods;
|
|
263
331
|
};
|
|
264
332
|
|
|
265
333
|
/**
|
|
@@ -317,7 +385,7 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
|
|
|
317
385
|
if (!mod) return null;
|
|
318
386
|
this.setMod(mod.config.name, mod);
|
|
319
387
|
let info = { ...mod.config, config_file: config_file };
|
|
320
|
-
this.
|
|
388
|
+
this._getInfos().push(info);
|
|
321
389
|
return mod;
|
|
322
390
|
};
|
|
323
391
|
|
|
@@ -431,7 +499,7 @@ Manager.prototype.updateAfter = function () {
|
|
|
431
499
|
* 排序模块信息
|
|
432
500
|
*/
|
|
433
501
|
Manager.prototype._sort = function () {
|
|
434
|
-
this.
|
|
502
|
+
this._getInfos().sort((a, b) => {
|
|
435
503
|
// 先按 sort 排序
|
|
436
504
|
let cha = a.sort - b.sort;
|
|
437
505
|
if (cha !== 0) {
|
|
@@ -450,7 +518,7 @@ Manager.prototype._sort = function () {
|
|
|
450
518
|
Manager.prototype.update = async function (dir, clear = true) {
|
|
451
519
|
try {
|
|
452
520
|
if (clear) {
|
|
453
|
-
this.clearMods();
|
|
521
|
+
await this.clearMods();
|
|
454
522
|
}
|
|
455
523
|
|
|
456
524
|
let search_dir = dir || this.config.dir;
|
|
@@ -460,7 +528,7 @@ Manager.prototype.update = async function (dir, clear = true) {
|
|
|
460
528
|
if (!this.config.lazy_load) {
|
|
461
529
|
await this.loadMods();
|
|
462
530
|
}
|
|
463
|
-
this.
|
|
531
|
+
await this.isLoaded(true);
|
|
464
532
|
} catch (err) {
|
|
465
533
|
this.log('error', '更新配置失败', err);
|
|
466
534
|
}
|
|
@@ -477,13 +545,13 @@ Manager.prototype._findFiles = async function (dir) {
|
|
|
477
545
|
this.log('warn', '目录为空,跳过扫描');
|
|
478
546
|
return [];
|
|
479
547
|
}
|
|
480
|
-
if (!
|
|
548
|
+
if (!hasDir(dir)) {
|
|
481
549
|
return [];
|
|
482
550
|
}
|
|
483
551
|
if (this.config.search_way === 'dir') {
|
|
484
|
-
return await this.
|
|
552
|
+
return await this._getScanner().scan(dir, this.config.filename, this.getDirName());
|
|
485
553
|
}
|
|
486
|
-
return await this.
|
|
554
|
+
return await this._getScanner().scanFile(dir, this.config.filename);
|
|
487
555
|
};
|
|
488
556
|
|
|
489
557
|
// ==== 高性能调用接口 ====
|
|
@@ -684,7 +752,7 @@ Manager.prototype._loadCore = async function () {
|
|
|
684
752
|
* 初始化公共属性
|
|
685
753
|
*/
|
|
686
754
|
Manager.prototype._initCore = async function () {
|
|
687
|
-
if (!this.isLoaded()) {
|
|
755
|
+
if (!(await this.isLoaded())) {
|
|
688
756
|
await this.do('load');
|
|
689
757
|
}
|
|
690
758
|
// 初始化AI客户端
|
|
@@ -738,14 +806,14 @@ Manager.prototype._ensureModLoaded = async function (mod, method) {
|
|
|
738
806
|
throw new TypeError(`模块(mod)参数不能为空`);
|
|
739
807
|
}
|
|
740
808
|
if (!['load', 'init'].includes(method)) {
|
|
741
|
-
if (!mod.isLoaded()) {
|
|
809
|
+
if (!(await mod.isLoaded())) {
|
|
742
810
|
try {
|
|
743
811
|
await mod.do('load');
|
|
744
812
|
} catch (error) {
|
|
745
813
|
throw new Error(`模块${mod.config.name}加载失败,无法执行方法 ${method}`);
|
|
746
814
|
}
|
|
747
815
|
}
|
|
748
|
-
if (!mod.isInited()) {
|
|
816
|
+
if (!(await mod.isInited())) {
|
|
749
817
|
try {
|
|
750
818
|
await mod.do('init');
|
|
751
819
|
} catch (error) {
|
|
@@ -983,7 +1051,7 @@ Manager.prototype.createFile = function (config, file) {
|
|
|
983
1051
|
f = `${dir_name}/${f}`;
|
|
984
1052
|
}
|
|
985
1053
|
}
|
|
986
|
-
f =
|
|
1054
|
+
f = fullname(f, this.config.dir);
|
|
987
1055
|
mod.createConfigFile(f, config);
|
|
988
1056
|
return f;
|
|
989
1057
|
};
|
|
@@ -1023,6 +1091,10 @@ Manager.prototype._logModError = function (name, method, error) {
|
|
|
1023
1091
|
this.log('error', '模块' + name + '执行' + method + '方法失败', error);
|
|
1024
1092
|
};
|
|
1025
1093
|
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1094
|
+
/**
|
|
1095
|
+
* 复用模块类的调度与生命周期能力
|
|
1096
|
+
* @description Manager 继承 mm_mod.Manager,需要将 mm_machine 的 Mod 原型成员复制到 Manager 原型
|
|
1097
|
+
*/
|
|
1098
|
+
addModMethods(Manager);
|
|
1099
|
+
|
|
1100
|
+
module.exports = Manager;
|