mm_machine 3.5.5 → 3.5.7
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/example/index.js +1 -4
- package/lib/drive.js +19 -81
- package/lib/manager.js +253 -170
- package/lib/mod.js +3 -241
- package/package.json +8 -7
- package/lib/scanner.js +0 -225
package/example/index.js
CHANGED
|
@@ -29,7 +29,7 @@ async function runDemo() {
|
|
|
29
29
|
await manager.runAll('start');
|
|
30
30
|
|
|
31
31
|
let mod = manager.getMod('hello');
|
|
32
|
-
console.log('
|
|
32
|
+
console.log('是否运行中:', await manager.isRunning('hello'));
|
|
33
33
|
|
|
34
34
|
// 3. 调用模块方法
|
|
35
35
|
console.log('\n=== 3. 调用模块方法 ===');
|
|
@@ -54,11 +54,8 @@ async function runDemo() {
|
|
|
54
54
|
// 6. 单模块生命周期:停止 -> 卸载 -> 销毁
|
|
55
55
|
console.log('\n=== 6. 停止 / 卸载 / 销毁 ===');
|
|
56
56
|
await mod.do('stop');
|
|
57
|
-
console.log('停止后状态:', mod.getState());
|
|
58
57
|
await mod.do('unload');
|
|
59
|
-
console.log('卸载后状态:', mod.getState(), '是否已加载:', mod.isLoaded());
|
|
60
58
|
await mod.do('destroy');
|
|
61
|
-
console.log('销毁后状态:', mod.getState());
|
|
62
59
|
|
|
63
60
|
console.log('\n示例执行完成。');
|
|
64
61
|
}
|
package/lib/drive.js
CHANGED
|
@@ -53,8 +53,6 @@ const MODE = {
|
|
|
53
53
|
class Drive extends Mod {
|
|
54
54
|
// 配置项
|
|
55
55
|
static config = {
|
|
56
|
-
// 名称, 由中英文和下"_"组成, 用于卸载接口 例如: demo
|
|
57
|
-
name: '',
|
|
58
56
|
// 标题, 介绍作用
|
|
59
57
|
title: '',
|
|
60
58
|
// 描述, 用于描述该有什么用的
|
|
@@ -101,16 +99,9 @@ class Drive extends Mod {
|
|
|
101
99
|
Drive.prototype._onInit = function () {
|
|
102
100
|
// 初始化
|
|
103
101
|
this.on('init:before', async (ctx) => {
|
|
104
|
-
await this.setState('initing');
|
|
105
102
|
this._initOptions();
|
|
106
103
|
await this._initCore(...ctx.params);
|
|
107
104
|
});
|
|
108
|
-
this.on('init:success', async (ctx) => {
|
|
109
|
-
await this.isInited(true);
|
|
110
|
-
});
|
|
111
|
-
this.on('init:after', async (ctx) => {
|
|
112
|
-
await this.setState('inited');
|
|
113
|
-
});
|
|
114
105
|
};
|
|
115
106
|
|
|
116
107
|
/**
|
|
@@ -120,49 +111,41 @@ Drive.prototype._onInit = function () {
|
|
|
120
111
|
Drive.prototype._onLoad = async function (ctx) {
|
|
121
112
|
// 1. 加载
|
|
122
113
|
this.on('load:before', async (ctx) => {
|
|
123
|
-
await this.setState('loading');
|
|
124
114
|
this.loadScript();
|
|
125
115
|
await this._loadCore(...ctx.params);
|
|
126
116
|
});
|
|
127
|
-
this.on('load:after', async (ctx) => {
|
|
128
|
-
await this.setState('loaded');
|
|
129
|
-
await this.isLoaded(true);
|
|
130
|
-
});
|
|
131
117
|
};
|
|
132
118
|
|
|
133
119
|
/**
|
|
134
|
-
*
|
|
120
|
+
* 应用配置(配置由管理器读取后传入)
|
|
135
121
|
* @param {string} file 配置文件路径
|
|
122
|
+
* @param {object} config 配置对象
|
|
136
123
|
* @returns {object} 配置项
|
|
137
124
|
*/
|
|
138
|
-
Drive.prototype.
|
|
125
|
+
Drive.prototype.applyConfig = function (file, config) {
|
|
139
126
|
if (file) {
|
|
140
127
|
this.config_file = file;
|
|
141
|
-
//
|
|
128
|
+
// 清除构造阶段缓存的目录与脚本路径,按配置文件重新推导
|
|
142
129
|
this._dir = '';
|
|
143
130
|
this.script_file = '';
|
|
144
131
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
let config = this._loadHotReload(this.config_file);
|
|
148
|
-
if (!config) {
|
|
149
|
-
this.newConfig(this.config_file);
|
|
150
|
-
config = this._loadHotReload(this.config_file);
|
|
151
|
-
}
|
|
152
|
-
this.setConfig(config);
|
|
153
|
-
return config;
|
|
154
|
-
} catch (err) {
|
|
155
|
-
this.log('error', '加载配置失败', this._wrapError(err));
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
132
|
+
this.setConfig(config);
|
|
133
|
+
return this.config;
|
|
158
134
|
};
|
|
159
135
|
|
|
160
136
|
/**
|
|
161
|
-
*
|
|
137
|
+
* 监听配置文件变化(热更新模式下生效)
|
|
162
138
|
* @param {string} file 配置文件路径
|
|
139
|
+
* @returns {object|null} 热更新管理器加载的配置,未开启热更新时返回 null
|
|
163
140
|
*/
|
|
164
|
-
Drive.prototype.
|
|
165
|
-
|
|
141
|
+
Drive.prototype.watchConfig = function (file) {
|
|
142
|
+
if (!file) return null;
|
|
143
|
+
if (this.mode >= MODE.HOT_CONFIG) {
|
|
144
|
+
return getHotReload().load(file, (config) => {
|
|
145
|
+
this._handleHotReload(config);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
166
149
|
};
|
|
167
150
|
|
|
168
151
|
/**
|
|
@@ -171,7 +154,6 @@ Drive.prototype.loadConfig = function (file) {
|
|
|
171
154
|
* @returns {object} 配置项
|
|
172
155
|
*/
|
|
173
156
|
Drive.prototype.setConfig = function (config) {
|
|
174
|
-
var old_script = this.script_file;
|
|
175
157
|
if (config) {
|
|
176
158
|
if (this.config_file) {
|
|
177
159
|
// 合并配置并应用配置处理
|
|
@@ -190,11 +172,6 @@ Drive.prototype.setConfig = function (config) {
|
|
|
190
172
|
if (file) {
|
|
191
173
|
this.script_file = fullname(file, this.getDir());
|
|
192
174
|
}
|
|
193
|
-
// 用 script_file 字符串比较,避免对象引用比较导致误判
|
|
194
|
-
// 构造阶段 script_file 由空变为配置值时无需重置(默认即为未加载)
|
|
195
|
-
if (old_script && old_script !== this.script_file) {
|
|
196
|
-
this.isLoaded(false);
|
|
197
|
-
}
|
|
198
175
|
this._preset();
|
|
199
176
|
return this.config;
|
|
200
177
|
};
|
|
@@ -346,7 +323,7 @@ Drive.prototype.getOption = function (key) {
|
|
|
346
323
|
*/
|
|
347
324
|
Drive.prototype.setOptions = function (options) {
|
|
348
325
|
if (!this._hasOptions() || !options) return;
|
|
349
|
-
push(this._options, options
|
|
326
|
+
push(this._options, options);
|
|
350
327
|
this.setOptionsToFile();
|
|
351
328
|
};
|
|
352
329
|
|
|
@@ -442,7 +419,6 @@ Drive.prototype._remove = function () {
|
|
|
442
419
|
*/
|
|
443
420
|
Drive.prototype.unloadScript = function () {
|
|
444
421
|
this._remove();
|
|
445
|
-
this.isLoaded(false);
|
|
446
422
|
};
|
|
447
423
|
|
|
448
424
|
/**
|
|
@@ -454,18 +430,6 @@ Drive.prototype.reloadScript = function () {
|
|
|
454
430
|
return this.loadScript();
|
|
455
431
|
};
|
|
456
432
|
|
|
457
|
-
/**
|
|
458
|
-
* 卸载之后处理
|
|
459
|
-
* @param {boolean} remove 是否删除文件
|
|
460
|
-
*/
|
|
461
|
-
Drive.prototype.unloadCore = async function (remove) {
|
|
462
|
-
// 删除脚本
|
|
463
|
-
this.unloadScript();
|
|
464
|
-
if (remove) {
|
|
465
|
-
this.delFile();
|
|
466
|
-
}
|
|
467
|
-
};
|
|
468
|
-
|
|
469
433
|
/**
|
|
470
434
|
* 获取脚本文件路径
|
|
471
435
|
* @returns {string | null} 完整文件路径
|
|
@@ -587,7 +551,7 @@ Drive.prototype.loadScript = function () {
|
|
|
587
551
|
Drive.prototype._setMainMethod = function (mod) {
|
|
588
552
|
if (!mod) return;
|
|
589
553
|
let name = this.config.func_name || 'main';
|
|
590
|
-
if (
|
|
554
|
+
if (typeof mod[name] === 'function') {
|
|
591
555
|
this.main = mod[name];
|
|
592
556
|
}
|
|
593
557
|
for (let key in mod) {
|
|
@@ -598,7 +562,6 @@ Drive.prototype._setMainMethod = function (mod) {
|
|
|
598
562
|
this.setData(key, obj);
|
|
599
563
|
}
|
|
600
564
|
}
|
|
601
|
-
this.isLoaded(true);
|
|
602
565
|
};
|
|
603
566
|
|
|
604
567
|
/**
|
|
@@ -896,34 +859,9 @@ Drive.prototype._delArray = function (file, config, name) {
|
|
|
896
859
|
* @returns {string} 配置文件名称
|
|
897
860
|
*/
|
|
898
861
|
Drive.prototype._getName = function (file) {
|
|
899
|
-
|
|
900
|
-
return name;
|
|
901
|
-
// // 提取文件名作为模块名
|
|
902
|
-
// let word = new Word();
|
|
903
|
-
// // 将文件名改为驼峰命名法
|
|
904
|
-
// return word.toCamelCase(name);
|
|
862
|
+
return basename(dirname(file));
|
|
905
863
|
};
|
|
906
864
|
|
|
907
|
-
/**
|
|
908
|
-
* 加载前处理
|
|
909
|
-
*/
|
|
910
|
-
Drive.prototype.loadBefore = function () {
|
|
911
|
-
try {
|
|
912
|
-
let mod = this.loadScript();
|
|
913
|
-
if (mod) {
|
|
914
|
-
this.isLoaded(true);
|
|
915
|
-
}
|
|
916
|
-
} catch (err) {
|
|
917
|
-
this.log('error', `加载前处理失败`, this._wrapError(err));
|
|
918
|
-
this.isLoaded(false);
|
|
919
|
-
}
|
|
920
|
-
};
|
|
921
|
-
|
|
922
|
-
/**
|
|
923
|
-
* 加载处理
|
|
924
|
-
*/
|
|
925
|
-
Drive.prototype._loadCore = function () { };
|
|
926
|
-
|
|
927
865
|
/**
|
|
928
866
|
* 保存配置
|
|
929
867
|
*/
|