mm_machine 5.0.1 → 6.0.1
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 +120 -545
- package/lib/drive.js +13 -21
- package/lib/manager.js +49 -33
- package/lib/mod.js +7 -64
- package/lib/script_protocol.js +43 -40
- package/package.json +12 -12
- package/tool/machine/index.js +51 -0
- package/tool/machine/tool.json +60 -0
package/lib/drive.js
CHANGED
|
@@ -1,40 +1,34 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
2
|
const { getTpl } = require('mm_tpl');
|
|
3
|
+
const { string, push } = require('mm_helper');
|
|
3
4
|
const {
|
|
4
|
-
string,
|
|
5
5
|
fullname,
|
|
6
6
|
hasFile,
|
|
7
7
|
loadText,
|
|
8
8
|
saveText,
|
|
9
9
|
loadJson,
|
|
10
10
|
saveJson,
|
|
11
|
-
push,
|
|
12
11
|
addDir,
|
|
13
12
|
delFile,
|
|
14
13
|
delDir,
|
|
15
14
|
dirname,
|
|
16
15
|
basename
|
|
17
|
-
} = require('
|
|
16
|
+
} = require('mm_files');
|
|
18
17
|
const { Mod } = require('./mod');
|
|
19
18
|
|
|
20
|
-
const MODE = {
|
|
21
|
-
// 生产模式
|
|
22
|
-
PRODUCTION: 1,
|
|
23
|
-
// 热更新配置模式
|
|
24
|
-
HOT_CONFIG: 2,
|
|
25
|
-
// 热更新脚本模式
|
|
26
|
-
HOT_RELOAD: 3,
|
|
27
|
-
// 热更新配置+脚本模式
|
|
28
|
-
HOT_BOTH: 4,
|
|
29
|
-
// 始终重载模式
|
|
30
|
-
RELOAD_ALWAYS: 5
|
|
31
|
-
};
|
|
32
|
-
|
|
33
19
|
/**
|
|
34
20
|
* 驱动基础类
|
|
35
21
|
* @class
|
|
36
22
|
*/
|
|
37
23
|
class Drive extends Mod {
|
|
24
|
+
static MODE = {
|
|
25
|
+
PRODUCTION: 1,
|
|
26
|
+
HOT_CONFIG: 2,
|
|
27
|
+
HOT_RELOAD: 3,
|
|
28
|
+
HOT_BOTH: 4,
|
|
29
|
+
RELOAD_ALWAYS: 5
|
|
30
|
+
};
|
|
31
|
+
|
|
38
32
|
// 配置项
|
|
39
33
|
static config = {
|
|
40
34
|
// 标题, 介绍作用
|
|
@@ -701,7 +695,8 @@ Drive.prototype._applyConfigUpdate = function (config) {
|
|
|
701
695
|
* @param {string} old_script 旧的脚本文件路径
|
|
702
696
|
*/
|
|
703
697
|
Drive.prototype._reloadIfNeeded = function (old_script) {
|
|
704
|
-
|
|
698
|
+
const mode = Drive.MODE;
|
|
699
|
+
if (this.mode === mode.HOT_RELOAD || this.mode === mode.HOT_BOTH) {
|
|
705
700
|
// 只在 main 发生变化时才重载脚本
|
|
706
701
|
if (!old_script || old_script !== this.script_file) {
|
|
707
702
|
this.reloadScript();
|
|
@@ -850,7 +845,4 @@ Drive.prototype.remove = function () {
|
|
|
850
845
|
/**
|
|
851
846
|
* @module 导出Drive类
|
|
852
847
|
*/
|
|
853
|
-
module.exports = {
|
|
854
|
-
Drive,
|
|
855
|
-
MODE
|
|
856
|
-
};
|
|
848
|
+
module.exports = { Drive, MODE: Drive.MODE };
|
package/lib/manager.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
2
|
const { Manager: Core } = require('mm_mod');
|
|
3
|
-
const {
|
|
3
|
+
const { string } = require('mm_helper');
|
|
4
|
+
const { hasDir, hasFile, fullname, basename, dirname } = require('mm_files');
|
|
4
5
|
const { loadAdmin } = require('mm_loader');
|
|
6
|
+
const { typeStore } = require('mm_scanner');
|
|
5
7
|
const { Mod, addMethods } = require('./mod.js');
|
|
6
8
|
const { Drive, MODE } = require('./drive.js');
|
|
7
9
|
const { resolveScriptFile, reloadScript } = require('./script_protocol.js');
|
|
@@ -16,6 +18,19 @@ function _normPath(file) {
|
|
|
16
18
|
return path.resolve(String(file)).toLowerCase();
|
|
17
19
|
}
|
|
18
20
|
|
|
21
|
+
/**
|
|
22
|
+
* 判断文件是否位于指定目录内(含子目录)
|
|
23
|
+
* @description 加载器按应用根目录扫描,管理器只处理自己目录范围内的清单
|
|
24
|
+
* @param {string} file 文件路径
|
|
25
|
+
* @param {string} dir 目录绝对路径
|
|
26
|
+
* @returns {boolean} 位于目录内返回 true
|
|
27
|
+
* @private
|
|
28
|
+
*/
|
|
29
|
+
function _inDir(file, dir) {
|
|
30
|
+
const rel = path.relative(dir, file);
|
|
31
|
+
return rel !== '' && !rel.startsWith('..') && !path.isAbsolute(rel);
|
|
32
|
+
}
|
|
33
|
+
|
|
19
34
|
/**
|
|
20
35
|
* 失效单个脚本文件的 require 缓存
|
|
21
36
|
* @description 文件不存在或从未加载时静默跳过
|
|
@@ -107,10 +122,10 @@ class Manager extends Core {
|
|
|
107
122
|
*/
|
|
108
123
|
scope: 'server',
|
|
109
124
|
/**
|
|
110
|
-
*
|
|
125
|
+
* 共享加载器作用域,取用 loadAdmin 实例时使用,缺省 'app'
|
|
111
126
|
* @type {string}
|
|
112
127
|
*/
|
|
113
|
-
|
|
128
|
+
load_scope: 'app',
|
|
114
129
|
/**
|
|
115
130
|
* 忽略目录数组
|
|
116
131
|
* @type {Array}
|
|
@@ -122,7 +137,7 @@ class Manager extends Core {
|
|
|
122
137
|
* 构造函数
|
|
123
138
|
* @param {object} config 配置项
|
|
124
139
|
* @param {object} parent 父项对象
|
|
125
|
-
* @param {object} [mods]
|
|
140
|
+
* @param {object} [mods] 模块对象列表,经父类构造注入为受管实例表(保持外部引用)
|
|
126
141
|
*/
|
|
127
142
|
constructor(config, parent, mods) {
|
|
128
143
|
super(
|
|
@@ -130,7 +145,8 @@ class Manager extends Core {
|
|
|
130
145
|
...Manager.config,
|
|
131
146
|
...config
|
|
132
147
|
},
|
|
133
|
-
parent
|
|
148
|
+
parent,
|
|
149
|
+
mods
|
|
134
150
|
);
|
|
135
151
|
|
|
136
152
|
if (parent) {
|
|
@@ -142,10 +158,6 @@ class Manager extends Core {
|
|
|
142
158
|
this._loaded = false;
|
|
143
159
|
// 目录监听器,键为目录路径,值为该目录的配置加载器
|
|
144
160
|
this._dir_watchers = new Map();
|
|
145
|
-
// 外部传入实例表时,直接作为受管记录的实例表,保证外部引用同步
|
|
146
|
-
if (mods) {
|
|
147
|
-
this._getRecord().mods = mods;
|
|
148
|
-
}
|
|
149
161
|
// 监听事件
|
|
150
162
|
this._listen();
|
|
151
163
|
}
|
|
@@ -223,27 +235,27 @@ Manager.prototype.getTplDir = function () {
|
|
|
223
235
|
|
|
224
236
|
/**
|
|
225
237
|
* 获取配置加载器
|
|
226
|
-
* @description 直接取 mm_loader 实例池中的共享 Loader(键为
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
238
|
+
* @description 直接取 mm_loader 实例池中的共享 Loader(键为 load_scope,缺省 'app'),
|
|
239
|
+
* 同一加载器名天然是同一个实例,本管理器不再另存一份引用;
|
|
240
|
+
* 一个加载器只负责一个目录,多目录请为每个目录配置不同的 load_scope;
|
|
241
|
+
* load_scope 即加载器实例键(Loader.name),与内核创建的实例同名即共享。
|
|
230
242
|
* 并把本管理器清单类型登记进该实例:类型键为 filename 去掉 .json 后缀
|
|
231
243
|
* (manager.json → manager,与默认 app → app.json 同构),已存在的类型不覆盖;
|
|
232
244
|
* 脚本监听由 _watchDir 是否传 script_callback 决定(回调驱动),不再配置实例
|
|
233
|
-
* @param {string} [dir]
|
|
245
|
+
* @param {string} [dir] 检索目录,用于首次创建实例时确定扫描根目录,缺省回退 config.dir
|
|
234
246
|
* @returns {Loader} 配置加载器
|
|
235
247
|
* @private
|
|
236
248
|
*/
|
|
237
249
|
Manager.prototype._getLoader = function (dir) {
|
|
238
|
-
const scope = this.config.
|
|
250
|
+
const scope = this.config.load_scope || 'app';
|
|
239
251
|
const target = dir || this.config.dir || '';
|
|
240
252
|
const key = target ? path.resolve(target) : '';
|
|
241
|
-
const loader = loadAdmin(scope, { dir: key
|
|
253
|
+
const loader = loadAdmin(scope, key ? { dir: key } : {});
|
|
242
254
|
const filename = this.config.filename;
|
|
243
255
|
if (filename) {
|
|
244
256
|
const type = filename.replace(/\.json$/i, '');
|
|
245
257
|
if (type && !loader.config.resources[type]) {
|
|
246
|
-
loader.addType(type, filename
|
|
258
|
+
loader.addType(type, filename);
|
|
247
259
|
}
|
|
248
260
|
}
|
|
249
261
|
return loader;
|
|
@@ -276,10 +288,9 @@ Manager.prototype.clearMods = async function () {
|
|
|
276
288
|
* @returns {object|null} 删除的模块对象
|
|
277
289
|
*/
|
|
278
290
|
Manager.prototype.delMod = function (name) {
|
|
279
|
-
let
|
|
280
|
-
let mod = record.mods[name];
|
|
291
|
+
let mod = this.mods[name];
|
|
281
292
|
if (mod) {
|
|
282
|
-
delete
|
|
293
|
+
delete this.mods[name];
|
|
283
294
|
let infos = this.configs;
|
|
284
295
|
let index = infos.findIndex((o) => o.name === name);
|
|
285
296
|
if (index >= 0) {
|
|
@@ -295,7 +306,7 @@ Manager.prototype.delMod = function (name) {
|
|
|
295
306
|
* @param {object} mod 模块对象
|
|
296
307
|
*/
|
|
297
308
|
Manager.prototype.setMod = function (name, mod) {
|
|
298
|
-
this.
|
|
309
|
+
this.mods[name] = mod;
|
|
299
310
|
};
|
|
300
311
|
|
|
301
312
|
/**
|
|
@@ -304,7 +315,7 @@ Manager.prototype.setMod = function (name, mod) {
|
|
|
304
315
|
* @returns {object} 模块对象
|
|
305
316
|
*/
|
|
306
317
|
Manager.prototype.getMod = function (name) {
|
|
307
|
-
return this.
|
|
318
|
+
return this.mods[name];
|
|
308
319
|
};
|
|
309
320
|
|
|
310
321
|
/**
|
|
@@ -312,7 +323,7 @@ Manager.prototype.getMod = function (name) {
|
|
|
312
323
|
* @returns {object[]} 模块对象数组
|
|
313
324
|
*/
|
|
314
325
|
Manager.prototype.getMods = function () {
|
|
315
|
-
return this.
|
|
326
|
+
return this.mods;
|
|
316
327
|
};
|
|
317
328
|
|
|
318
329
|
/**
|
|
@@ -565,6 +576,8 @@ Manager.prototype.update = async function (dir, clear = true) {
|
|
|
565
576
|
|
|
566
577
|
/**
|
|
567
578
|
* 检索目录下所有文件
|
|
579
|
+
* @description 扫描只做一次:加载器已扫描时直接复用类型 Store 中的分类结果,
|
|
580
|
+
* 再按管理器的清单文件名精确过滤,不再重复遍历目录
|
|
568
581
|
* @private
|
|
569
582
|
* @param {string} dir 目录路径
|
|
570
583
|
* @returns {Array} 文件路径数组
|
|
@@ -577,14 +590,17 @@ Manager.prototype._findFiles = async function (dir) {
|
|
|
577
590
|
if (!hasDir(dir)) {
|
|
578
591
|
return [];
|
|
579
592
|
}
|
|
580
|
-
|
|
581
|
-
// update 语义要求看到新增/删除的文件
|
|
582
|
-
let files = this._getLoader(dir).scan();
|
|
593
|
+
let loader = this._getLoader(dir).ensureScanned();
|
|
583
594
|
let filename = this.config.filename;
|
|
584
595
|
if (!filename) {
|
|
585
|
-
return files;
|
|
596
|
+
return loader.files;
|
|
586
597
|
}
|
|
587
|
-
|
|
598
|
+
let type = filename.replace(/\.json$/i, '');
|
|
599
|
+
let scope = path.resolve(dir);
|
|
600
|
+
return typeStore(type)
|
|
601
|
+
.list({ root: loader.config.dir })
|
|
602
|
+
.filter((one) => one.name === filename && _inDir(one.file, scope))
|
|
603
|
+
.map((one) => one.file);
|
|
588
604
|
};
|
|
589
605
|
|
|
590
606
|
// ==== 目录监听 ====
|
|
@@ -603,11 +619,11 @@ Manager.prototype._watchDir = function (dir) {
|
|
|
603
619
|
let hot = this._isScriptHot();
|
|
604
620
|
let loader = this._getLoader(dir);
|
|
605
621
|
let config_callback = (changes) => {
|
|
606
|
-
this._applyChanges(changes);
|
|
622
|
+
this._applyChanges(changes.filter((one) => _inDir(one.file, dir)));
|
|
607
623
|
};
|
|
608
624
|
let script_callback = hot
|
|
609
625
|
? (changes) => {
|
|
610
|
-
this._applyScriptChanges(changes);
|
|
626
|
+
this._applyScriptChanges(changes.filter((one) => _inDir(one.file, dir)));
|
|
611
627
|
}
|
|
612
628
|
: undefined;
|
|
613
629
|
let success = loader.watch(config_callback, script_callback);
|
|
@@ -800,7 +816,7 @@ Manager.prototype._getConfigByScript = function (file) {
|
|
|
800
816
|
* @returns {void}
|
|
801
817
|
*/
|
|
802
818
|
Manager.prototype.unwatch = function () {
|
|
803
|
-
for (
|
|
819
|
+
for (const entry of this._dir_watchers.values()) {
|
|
804
820
|
entry.loader.unwatch(entry.config_callback, entry.script_callback);
|
|
805
821
|
}
|
|
806
822
|
this._dir_watchers.clear();
|
|
@@ -1070,12 +1086,12 @@ Manager.prototype._ensureModLoaded = async function (mod, method) {
|
|
|
1070
1086
|
if (is_running) {
|
|
1071
1087
|
try {
|
|
1072
1088
|
await mod.do('load');
|
|
1073
|
-
} catch
|
|
1089
|
+
} catch {
|
|
1074
1090
|
throw new Error(`模块${name}加载失败,无法执行方法 ${method}`);
|
|
1075
1091
|
}
|
|
1076
1092
|
try {
|
|
1077
1093
|
await mod.do('init');
|
|
1078
|
-
} catch
|
|
1094
|
+
} catch {
|
|
1079
1095
|
throw new Error(`模块${name}初始化失败,无法执行方法 ${method}`);
|
|
1080
1096
|
}
|
|
1081
1097
|
// 记录受管模块已完成初始化
|
package/lib/mod.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const { Mod: CoreMod } = require('mm_mod');
|
|
2
|
-
const { saveJson, loadJson } = require('
|
|
2
|
+
const { saveJson, loadJson } = require('mm_files');
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* 通用模块类
|
|
@@ -44,69 +44,12 @@ class Mod extends CoreMod {
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* 初始化
|
|
57
|
-
* @returns {Promise<Mod>} 当前实例
|
|
58
|
-
*/
|
|
59
|
-
Mod.prototype.init = async function () {
|
|
60
|
-
return await this._init();
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 启动(兼容 mm_expand 行为,生命周期由 do 流水线驱动)
|
|
65
|
-
* @returns {Promise<Mod>} 当前实例
|
|
66
|
-
*/
|
|
67
|
-
Mod.prototype._start = async function () {
|
|
68
|
-
return this;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* 启动
|
|
73
|
-
* @returns {Promise<Mod>} 当前实例
|
|
74
|
-
*/
|
|
75
|
-
Mod.prototype.start = async function () {
|
|
76
|
-
return await this._start();
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 停止(兼容 mm_expand 行为,生命周期由 do 流水线驱动)
|
|
81
|
-
* @returns {Promise<Mod>} 当前实例
|
|
82
|
-
*/
|
|
83
|
-
Mod.prototype._stop = async function () {
|
|
84
|
-
return this;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* 停止
|
|
89
|
-
* @returns {Promise<Mod>} 当前实例
|
|
90
|
-
*/
|
|
91
|
-
Mod.prototype.stop = async function () {
|
|
92
|
-
return await this._stop();
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* 销毁(兼容 mm_expand 行为,生命周期由 do 流水线驱动)
|
|
97
|
-
* @returns {Promise<Mod>} 当前实例
|
|
98
|
-
*/
|
|
99
|
-
Mod.prototype._destroy = async function () {
|
|
100
|
-
return this;
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* 销毁
|
|
105
|
-
* @returns {Promise<Mod>} 当前实例
|
|
106
|
-
*/
|
|
107
|
-
Mod.prototype.destroy = async function () {
|
|
108
|
-
return await this._destroy();
|
|
109
|
-
};
|
|
47
|
+
// 生命周期入口保留为既有调用方的公开契约;实际编排由 do 流水线的 _xxxCore 钩子承担
|
|
48
|
+
for (const name of ['init', 'start', 'stop', 'destroy']) {
|
|
49
|
+
Mod.prototype[name] = async function () {
|
|
50
|
+
return this;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
110
53
|
|
|
111
54
|
/**
|
|
112
55
|
* 包装错误,附加模块文件路径上下文,用于溯源
|
package/lib/script_protocol.js
CHANGED
|
@@ -7,59 +7,62 @@
|
|
|
7
7
|
* - Drive 系(Drive / App / Plugin / Cmd):script_file、reloadScript()
|
|
8
8
|
* - Mod 系脚本驱动(Api / Event / Service / Pendant):_getScriptFile()、
|
|
9
9
|
* load() 把脚本方法挂到实例 this.main 上
|
|
10
|
-
* 管理器判断「某个脚本属于哪个驱动」以及「如何让它生效」时走本模块,
|
|
11
10
|
* 新增一种驱动类型只需注册适配器,不必修改管理器或宿主。
|
|
12
11
|
*/
|
|
13
12
|
|
|
14
13
|
const path = require('path');
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
|
-
*
|
|
18
|
-
* @
|
|
16
|
+
* 默认入口解析器
|
|
17
|
+
* @returns {Array<function(object): string>} 解析器列表
|
|
19
18
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return drive._getScriptFile() || '';
|
|
27
|
-
} catch {
|
|
28
|
-
return '';
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
// Pendant 等驱动 getDir() 返回空串,目录只保存在 config.dir;
|
|
32
|
-
// 直接用 getDir() 会把入口误解析到进程 cwd 下的同名文件
|
|
33
|
-
(drive) => {
|
|
34
|
-
const main = drive.config && drive.config.main;
|
|
35
|
-
if (!main) return '';
|
|
36
|
-
let dir = (drive.config && drive.config.dir) || '';
|
|
37
|
-
if (!dir && typeof drive.getDir === 'function') {
|
|
19
|
+
function defaultEntryResolvers() {
|
|
20
|
+
return [
|
|
21
|
+
(drive) => drive.file || '',
|
|
22
|
+
(drive) => drive.script_file || '',
|
|
23
|
+
(drive) => {
|
|
24
|
+
if (typeof drive._getScriptFile !== 'function') return '';
|
|
38
25
|
try {
|
|
39
|
-
|
|
26
|
+
return drive._getScriptFile() || '';
|
|
40
27
|
} catch {
|
|
41
28
|
return '';
|
|
42
29
|
}
|
|
30
|
+
},
|
|
31
|
+
// Pendant 等驱动 getDir() 返回空串,目录只保存在 config.dir;
|
|
32
|
+
// 直接用 getDir() 会把入口误解析到进程 cwd 下的同名文件
|
|
33
|
+
(drive) => {
|
|
34
|
+
const main = drive.config && drive.config.main;
|
|
35
|
+
if (!main) return '';
|
|
36
|
+
let dir = (drive.config && drive.config.dir) || '';
|
|
37
|
+
if (!dir && typeof drive.getDir === 'function') {
|
|
38
|
+
try {
|
|
39
|
+
dir = drive.getDir() || '';
|
|
40
|
+
} catch {
|
|
41
|
+
return '';
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return dir ? path.resolve(dir, main) : '';
|
|
43
45
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
];
|
|
46
|
+
];
|
|
47
|
+
}
|
|
47
48
|
|
|
48
49
|
/**
|
|
49
|
-
*
|
|
50
|
-
* @
|
|
50
|
+
* 默认重载器
|
|
51
|
+
* @returns {Array<{test: function(object): boolean, run: function(object): *}>} 重载器列表
|
|
51
52
|
*/
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
53
|
+
function defaultReloaders() {
|
|
54
|
+
return [
|
|
55
|
+
{ test: (d) => typeof d.reloadScript === 'function', run: (d) => d.reloadScript() },
|
|
56
|
+
// 普通 Mod 的 load 是生命周期方法,额外要求 _setMainMethod 避免误触
|
|
57
|
+
{
|
|
58
|
+
test: (d) => typeof d._setMainMethod === 'function' && typeof d.load === 'function',
|
|
59
|
+
run: (d) => d.load()
|
|
60
|
+
}
|
|
61
|
+
];
|
|
62
|
+
}
|
|
60
63
|
|
|
61
|
-
const
|
|
62
|
-
const
|
|
64
|
+
const ENTRY_RESOLVERS = defaultEntryResolvers();
|
|
65
|
+
const RELOADERS = defaultReloaders();
|
|
63
66
|
|
|
64
67
|
/**
|
|
65
68
|
* 注册入口解析器(扩展点)
|
|
@@ -90,12 +93,12 @@ function registerReloader(test, run, first = false) {
|
|
|
90
93
|
}
|
|
91
94
|
|
|
92
95
|
/**
|
|
93
|
-
*
|
|
96
|
+
* 恢复默认适配器,原地替换以保持注册表引用稳定
|
|
94
97
|
* @returns {void}
|
|
95
98
|
*/
|
|
96
99
|
function resetAdapters() {
|
|
97
|
-
ENTRY_RESOLVERS.splice(0, ENTRY_RESOLVERS.length, ...
|
|
98
|
-
RELOADERS.splice(0, RELOADERS.length, ...
|
|
100
|
+
ENTRY_RESOLVERS.splice(0, ENTRY_RESOLVERS.length, ...defaultEntryResolvers());
|
|
101
|
+
RELOADERS.splice(0, RELOADERS.length, ...defaultReloaders());
|
|
99
102
|
}
|
|
100
103
|
|
|
101
104
|
/**
|
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
"description": "
|
|
2
|
+
"name": "mm_machine",
|
|
3
|
+
"version": "6.0.1",
|
|
4
|
+
"description": "Node.js 插件机制:目录扫描、生命周期编排、方法调用与热重载",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "jest --coverage",
|
|
8
|
-
"demo": "node example/index.js"
|
|
9
|
-
"dev": "nodemon ./test.js"
|
|
8
|
+
"demo": "node example/index.js"
|
|
10
9
|
},
|
|
11
10
|
"repository": {
|
|
12
11
|
"type": "git",
|
|
@@ -15,7 +14,6 @@
|
|
|
15
14
|
"keywords": [
|
|
16
15
|
"mm_mod",
|
|
17
16
|
"mm_infra",
|
|
18
|
-
"mm_runtime",
|
|
19
17
|
"plugin-system",
|
|
20
18
|
"module-management",
|
|
21
19
|
"hot-reload",
|
|
@@ -25,25 +23,27 @@
|
|
|
25
23
|
"framework",
|
|
26
24
|
"mechanism"
|
|
27
25
|
],
|
|
28
|
-
"layer": "
|
|
26
|
+
"layer": "shared",
|
|
29
27
|
"group": "runtime",
|
|
30
28
|
"author": "邱文武",
|
|
31
29
|
"license": "ISC",
|
|
32
30
|
"engines": {
|
|
33
|
-
"node": ">=
|
|
31
|
+
"node": ">=18"
|
|
34
32
|
},
|
|
35
33
|
"files": [
|
|
36
34
|
"index.js",
|
|
37
|
-
"lib
|
|
35
|
+
"lib",
|
|
38
36
|
"example",
|
|
39
|
-
"README.md"
|
|
37
|
+
"README.md",
|
|
38
|
+
"tool"
|
|
40
39
|
],
|
|
41
40
|
"dependencies": {
|
|
42
41
|
"@prettier/sync": "^0.6.1",
|
|
43
|
-
"
|
|
42
|
+
"mm_files": "^1.4.0",
|
|
44
43
|
"mm_helper": "^1.0.3",
|
|
45
|
-
"mm_loader": "^
|
|
44
|
+
"mm_loader": "^7.0.0",
|
|
46
45
|
"mm_mod": "^1.0.6",
|
|
46
|
+
"mm_scanner": "^5.0.0",
|
|
47
47
|
"mm_tpl": "^2.7.3"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* machine —— 插件机制契约自省工具
|
|
4
|
+
* 方法清单直接从模块导出的类原型采集,避免文档与实现漂移。
|
|
5
|
+
*/
|
|
6
|
+
const machine = require('../../index.js');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 采集类原型上的公开方法
|
|
10
|
+
* @param {Function} Cls 类
|
|
11
|
+
* @returns {Array<string>} 方法名列表
|
|
12
|
+
*/
|
|
13
|
+
function _methods(Cls) {
|
|
14
|
+
if (typeof Cls !== 'function' || !Cls.prototype) return [];
|
|
15
|
+
return Object.getOwnPropertyNames(Cls.prototype).filter((k) => k !== 'constructor' && k[0] !== '_').sort();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function main(params, ctx, options) {
|
|
19
|
+
void options;
|
|
20
|
+
const p = params || {};
|
|
21
|
+
const action = p.action || 'modes';
|
|
22
|
+
const done = (data) => { ctx.output = data; return data; };
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
if (action === 'modes') {
|
|
26
|
+
return done({
|
|
27
|
+
code: 0,
|
|
28
|
+
msg: '成功',
|
|
29
|
+
modes: machine.MODE || {},
|
|
30
|
+
lifecycle: ['init', 'start', 'stop', 'destroy']
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (action === 'classes') {
|
|
35
|
+
const names = p.class ? [p.class] : ['Mod', 'Drive', 'Manager'];
|
|
36
|
+
const classes = {};
|
|
37
|
+
for (const name of names) {
|
|
38
|
+
const Cls = machine[name];
|
|
39
|
+
if (!Cls) throw new Error('未知类: ' + name);
|
|
40
|
+
classes[name] = _methods(Cls);
|
|
41
|
+
}
|
|
42
|
+
return done({ code: 0, msg: '成功', classes });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return done({ code: 10001, msg: '不支持的操作: ' + action });
|
|
46
|
+
} catch (error) {
|
|
47
|
+
return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = { main };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "machine",
|
|
3
|
+
"title": "插件机制契约自省",
|
|
4
|
+
"description": "基于 mm_machine 的契约自省能力:列出模块状态枚举值、查看 Mod/Drive/Manager 三个核心类的方法清单与生命周期顺序。适用场景:写自定义受管模块前确认要实现哪些钩子、排查管理器实例该调哪个方法。",
|
|
5
|
+
"category": "内核运行时",
|
|
6
|
+
"group": "kernel",
|
|
7
|
+
"type": "function",
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"params": {
|
|
10
|
+
"type": "object",
|
|
11
|
+
"properties": {
|
|
12
|
+
"action": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "操作类型: modes(状态枚举)/classes(类方法清单)",
|
|
15
|
+
"enum": [
|
|
16
|
+
"modes",
|
|
17
|
+
"classes"
|
|
18
|
+
],
|
|
19
|
+
"default": "modes"
|
|
20
|
+
},
|
|
21
|
+
"class": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
"description": "类名(classes 可选): Mod/Drive/Manager,缺省全部"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"required": [
|
|
27
|
+
"action"
|
|
28
|
+
]
|
|
29
|
+
},
|
|
30
|
+
"returns": {
|
|
31
|
+
"type": "object",
|
|
32
|
+
"properties": {
|
|
33
|
+
"code": {
|
|
34
|
+
"type": "number",
|
|
35
|
+
"description": "0 表示成功,1xxxx 表示业务/执行异常"
|
|
36
|
+
},
|
|
37
|
+
"msg": {
|
|
38
|
+
"type": "string",
|
|
39
|
+
"description": "结果描述"
|
|
40
|
+
},
|
|
41
|
+
"modes": {
|
|
42
|
+
"type": "object",
|
|
43
|
+
"description": "状态枚举(MODE)"
|
|
44
|
+
},
|
|
45
|
+
"classes": {
|
|
46
|
+
"type": "object",
|
|
47
|
+
"description": "类名 → 方法清单"
|
|
48
|
+
},
|
|
49
|
+
"lifecycle": {
|
|
50
|
+
"type": "array",
|
|
51
|
+
"description": "生命周期方法顺序"
|
|
52
|
+
},
|
|
53
|
+
"error": {
|
|
54
|
+
"type": "object",
|
|
55
|
+
"description": "异常对象 { name, message }"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"handler": "manager"
|
|
60
|
+
}
|