mm_machine 3.3.6 → 3.3.8
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/lib/auto_require.js +128 -0
- package/lib/drive.js +143 -29
- package/lib/manager.js +9 -3
- package/lib/mod.js +18 -36
- package/lib/scanner.js +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
require('mm_hot_reload');
|
|
2
|
+
|
|
3
|
+
const _AUTO_INST = {};
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 从模块标识符中提取npm包名
|
|
7
|
+
* @param {string} id 模块标识符
|
|
8
|
+
* @returns {string} npm包名
|
|
9
|
+
*/
|
|
10
|
+
function _extractPkgName(id) {
|
|
11
|
+
if (id.startsWith('@')) {
|
|
12
|
+
let parts = id.split('/');
|
|
13
|
+
if (parts.length >= 2) {
|
|
14
|
+
return parts[0] + '/' + parts[1];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return id.split('/')[0];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 从MODULE_NOT_FOUND错误消息中提取模块名
|
|
22
|
+
* @param {Error} e 错误对象
|
|
23
|
+
* @returns {string} 模块名
|
|
24
|
+
*/
|
|
25
|
+
function _extractErrName(e) {
|
|
26
|
+
let match = e.message.match(/'([^']+)'/);
|
|
27
|
+
return match ? match[1] : '';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 是否为本地路径引用(非npm包)
|
|
32
|
+
* @param {string} name 模块名
|
|
33
|
+
* @returns {boolean} 是否为本地路径
|
|
34
|
+
*/
|
|
35
|
+
function _isLocal(name) {
|
|
36
|
+
return name.startsWith('.') || name.startsWith('/') || /^[a-zA-Z]:/.test(name);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 清除指定包的require缓存
|
|
41
|
+
* @param {string} name 包名
|
|
42
|
+
*/
|
|
43
|
+
function _clearPkgCache(name) {
|
|
44
|
+
try {
|
|
45
|
+
let main_file = require.resolve(name);
|
|
46
|
+
delete require.cache[main_file];
|
|
47
|
+
} catch (e) {
|
|
48
|
+
void e;
|
|
49
|
+
}
|
|
50
|
+
let sep = require('path').sep;
|
|
51
|
+
let marker = sep + 'node_modules' + sep + name + sep;
|
|
52
|
+
for (let key in require.cache) {
|
|
53
|
+
if (key.indexOf(marker) !== -1) {
|
|
54
|
+
delete require.cache[key];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 同步安装npm包
|
|
61
|
+
* @param {string} name 包名
|
|
62
|
+
* @param {string} cwd 安装目录
|
|
63
|
+
*/
|
|
64
|
+
function _npmInstall(name, cwd) {
|
|
65
|
+
let install_dir = cwd || process.cwd();
|
|
66
|
+
let cmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
67
|
+
let exec_sync = require('child_process').execSync;
|
|
68
|
+
exec_sync(cmd + ' install ' + name + ' --save-prod',
|
|
69
|
+
{ cwd: install_dir, stdio: 'pipe' });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 分析 $.mod.load 失败原因,若是缺少npm依赖则安装
|
|
74
|
+
* @param {string} file 文件路径
|
|
75
|
+
* @returns {boolean} 是否已安装依赖
|
|
76
|
+
*/
|
|
77
|
+
function _tryInstall(file) {
|
|
78
|
+
try {
|
|
79
|
+
require(require('path').resolve(file));
|
|
80
|
+
return false;
|
|
81
|
+
} catch (e) {
|
|
82
|
+
if (e.code !== 'MODULE_NOT_FOUND') {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
let raw_name = _extractErrName(e);
|
|
86
|
+
if (!raw_name || _isLocal(raw_name)) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
let pkg_name = _extractPkgName(raw_name);
|
|
90
|
+
if (!pkg_name || _AUTO_INST[pkg_name]) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
_AUTO_INST[pkg_name] = true;
|
|
94
|
+
_npmInstall(pkg_name);
|
|
95
|
+
_clearPkgCache(pkg_name);
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 增强require函数,支持脚本文件(.js)热重载和依赖自动安装
|
|
102
|
+
* 当引用的目标模块缺少npm依赖时,自动npm install并重试加载
|
|
103
|
+
* @param {string} file 脚本文件路径(.js)
|
|
104
|
+
* @param {Function} func 热更新回调函数,文件变化时触发
|
|
105
|
+
* @returns {object} 加载的模块对象
|
|
106
|
+
* @throws {TypeError} 参数类型错误时抛出
|
|
107
|
+
*/
|
|
108
|
+
$.require = function (file, func) {
|
|
109
|
+
if (typeof file !== 'string') {
|
|
110
|
+
throw new TypeError('第一个参数(file)必须是字符串');
|
|
111
|
+
}
|
|
112
|
+
if (func && typeof func !== 'function') {
|
|
113
|
+
throw new TypeError('第二个参数(func)必须是函数');
|
|
114
|
+
}
|
|
115
|
+
if (!file.endsWith('.js')) {
|
|
116
|
+
throw new TypeError('$.require 只能用于.js脚本文件');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let mod = $.mod.load(file, func);
|
|
120
|
+
if (mod !== null) {
|
|
121
|
+
return mod;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (_tryInstall(file)) {
|
|
125
|
+
return $.mod.load(file, func);
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
};
|
package/lib/drive.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
require('mm_tpl');
|
|
2
|
-
|
|
2
|
+
const { conf } = require('mm_config');
|
|
3
3
|
const { Mod } = require('./mod');
|
|
4
4
|
|
|
5
5
|
const MODE = {
|
|
@@ -81,12 +81,6 @@ Drive.prototype.getEventer = function () {
|
|
|
81
81
|
return $.eventer;
|
|
82
82
|
};
|
|
83
83
|
|
|
84
|
-
/**
|
|
85
|
-
* 配置驱动
|
|
86
|
-
*/
|
|
87
|
-
Drive.prototype._preset = function () {
|
|
88
|
-
};
|
|
89
|
-
|
|
90
84
|
/**
|
|
91
85
|
* 获取配置
|
|
92
86
|
* @param {string} file 配置文件路径
|
|
@@ -152,6 +146,7 @@ Drive.prototype.setConfig = function (config) {
|
|
|
152
146
|
this.script_file = file.fullname(this.getDir());
|
|
153
147
|
}
|
|
154
148
|
this._preset();
|
|
149
|
+
this._initOptions();
|
|
155
150
|
return this.config;
|
|
156
151
|
};
|
|
157
152
|
|
|
@@ -163,6 +158,123 @@ Drive.prototype.getConfig = function () {
|
|
|
163
158
|
return this.config;
|
|
164
159
|
};
|
|
165
160
|
|
|
161
|
+
/**
|
|
162
|
+
* 初始化选项
|
|
163
|
+
* 从缓存文件加载键值对,如果没有缓存则根据config.options生成默认键值对
|
|
164
|
+
* 缓存存在时会补全config.options中新增键的默认值
|
|
165
|
+
*/
|
|
166
|
+
Drive.prototype._initOptions = function () {
|
|
167
|
+
let options = this.config.options;
|
|
168
|
+
if (!options) return;
|
|
169
|
+
let cache_file = this._getOptionsFile();
|
|
170
|
+
|
|
171
|
+
let cached = {};
|
|
172
|
+
// 尝试从缓存文件加载已有键值对
|
|
173
|
+
if (cache_file && cache_file.hasFile()) {
|
|
174
|
+
cached = cache_file.loadJson() || {};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
this._options = {};
|
|
178
|
+
// 根据config.options生成键值对,缓存值优先,否则取默认值
|
|
179
|
+
for (let key in options) {
|
|
180
|
+
if (cached.hasOwnProperty(key) && cached[key] !== undefined) {
|
|
181
|
+
this._options[key] = cached[key];
|
|
182
|
+
} else {
|
|
183
|
+
this._options[key] = options[key].default;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* 获取缓存目录
|
|
190
|
+
* @returns {string} 缓存目录
|
|
191
|
+
*/
|
|
192
|
+
Drive.prototype._getCacheDir = function () {
|
|
193
|
+
return './cache/options'.fullname();
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 获取选项缓存文件名
|
|
198
|
+
* @returns {string} 选项缓存文件名
|
|
199
|
+
*/
|
|
200
|
+
Drive.prototype._getOptionsFileName = function () {
|
|
201
|
+
// 先获取上级名
|
|
202
|
+
let parent = this.getParent();
|
|
203
|
+
let parent_name = '';
|
|
204
|
+
if (parent) {
|
|
205
|
+
// 获取上级构造名
|
|
206
|
+
let constructor_name = parent.constructor.name || '';
|
|
207
|
+
// 再获取上级名称
|
|
208
|
+
let mod_name = parent.config?.name || '';
|
|
209
|
+
parent_name = constructor_name + '_' + mod_name;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// 再获取当前模块名
|
|
213
|
+
let current_name = this.constructor.name + '_' + (this.config.name || 'default');
|
|
214
|
+
if (parent_name) {
|
|
215
|
+
current_name = parent_name + '/' + current_name;
|
|
216
|
+
}
|
|
217
|
+
return `./${current_name}.json`.toLowerCase();
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 获取选项缓存文件路径
|
|
222
|
+
* @returns {string} 选项缓存文件路径 为空字符串表示未配置缓存目录
|
|
223
|
+
*/
|
|
224
|
+
Drive.prototype._getOptionsFile = function () {
|
|
225
|
+
let dir = this._getCacheDir();
|
|
226
|
+
if (!dir) return '';
|
|
227
|
+
return this._getOptionsFileName().fullname(dir);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* 获取所有选项
|
|
232
|
+
* @returns {object} 所有选项
|
|
233
|
+
*/
|
|
234
|
+
Drive.prototype.getOptions = function () {
|
|
235
|
+
return this._options || {};
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 获取选项
|
|
240
|
+
* @param {string} key 选项键
|
|
241
|
+
* @returns {*} 选项值
|
|
242
|
+
*/
|
|
243
|
+
Drive.prototype.getOption = function (key) {
|
|
244
|
+
let options = this.getOptions();
|
|
245
|
+
return options[key];
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* 设置所有选项
|
|
250
|
+
* @param {object} options 所有选项
|
|
251
|
+
*/
|
|
252
|
+
Drive.prototype.setOptions = function (options) {
|
|
253
|
+
this._options = options;
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* 设置选项并保存到缓存文件
|
|
258
|
+
* @param {string} key 选项键
|
|
259
|
+
* @param {*} value 选项值
|
|
260
|
+
*/
|
|
261
|
+
Drive.prototype.setOption = function (key, value) {
|
|
262
|
+
if (typeof key !== 'string') {
|
|
263
|
+
throw new TypeError('key必须是字符串');
|
|
264
|
+
}
|
|
265
|
+
if (!this._options) {
|
|
266
|
+
this._options = {};
|
|
267
|
+
}
|
|
268
|
+
this._options[key] = value;
|
|
269
|
+
|
|
270
|
+
let cache_file = this._getOptionsFile();
|
|
271
|
+
if (cache_file) {
|
|
272
|
+
// 确保目录存在
|
|
273
|
+
cache_file.addDir();
|
|
274
|
+
cache_file.saveJson(this._options);
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
|
|
166
278
|
/**
|
|
167
279
|
* 获取当前文件
|
|
168
280
|
* @returns {object} 当前文件对象
|
|
@@ -176,10 +288,13 @@ Drive.prototype.getDir = function () {
|
|
|
176
288
|
} else if (this.config_file) {
|
|
177
289
|
dir = this.config_file.dirname();
|
|
178
290
|
this._dir = dir;
|
|
179
|
-
} else
|
|
180
|
-
let
|
|
181
|
-
if (
|
|
182
|
-
|
|
291
|
+
} else {
|
|
292
|
+
let parent = this.getParent();
|
|
293
|
+
if (parent) {
|
|
294
|
+
let parent_dir = parent.getDir() || '';
|
|
295
|
+
if (parent_dir) {
|
|
296
|
+
dir = ('./' + this.config.name).fullname(parent_dir);
|
|
297
|
+
}
|
|
183
298
|
}
|
|
184
299
|
}
|
|
185
300
|
}
|
|
@@ -269,9 +384,9 @@ Drive.prototype.newScript = function (file) {
|
|
|
269
384
|
this._createScriptFile(file);
|
|
270
385
|
};
|
|
271
386
|
|
|
272
|
-
Drive.prototype.renderTpl = function(tpl, model){
|
|
387
|
+
Drive.prototype.renderTpl = function (tpl, model) {
|
|
273
388
|
return $.tpl.render(tpl, model);
|
|
274
|
-
}
|
|
389
|
+
};
|
|
275
390
|
|
|
276
391
|
/**
|
|
277
392
|
* 创建脚本
|
|
@@ -279,8 +394,8 @@ Drive.prototype.renderTpl = function(tpl, model){
|
|
|
279
394
|
* @returns {boolean} 创建是否成功
|
|
280
395
|
*/
|
|
281
396
|
Drive.prototype._createScriptFile = function (file) {
|
|
282
|
-
|
|
283
|
-
|
|
397
|
+
let tpl = this._getScriptTpl();
|
|
398
|
+
let content = this.renderTpl(tpl, this.getModel('script'));
|
|
284
399
|
if (!content) {
|
|
285
400
|
return '';
|
|
286
401
|
}
|
|
@@ -294,7 +409,7 @@ Drive.prototype._createScriptFile = function (file) {
|
|
|
294
409
|
* @returns {string} 脚本模板内容
|
|
295
410
|
*/
|
|
296
411
|
Drive.prototype._getScriptTpl = function () {
|
|
297
|
-
|
|
412
|
+
let f = './script.tpl.js'.fullname(this._getTplDir());
|
|
298
413
|
if (f.hasFile()) {
|
|
299
414
|
return f.loadText();
|
|
300
415
|
} else {
|
|
@@ -344,13 +459,13 @@ Drive.prototype.loadScript = function () {
|
|
|
344
459
|
if (!file) return null;
|
|
345
460
|
try {
|
|
346
461
|
if (this.mode === MODE.HOT_RELOAD || this.mode === MODE.HOT_BOTH) {
|
|
347
|
-
|
|
462
|
+
let cs = $.require(file, (mod) => {
|
|
348
463
|
this._setMainMethod(mod);
|
|
349
464
|
});
|
|
350
465
|
this._setMainMethod(cs);
|
|
351
466
|
return cs;
|
|
352
467
|
} else {
|
|
353
|
-
|
|
468
|
+
let cs = require(file);
|
|
354
469
|
this._setMainMethod(cs);
|
|
355
470
|
return cs;
|
|
356
471
|
}
|
|
@@ -366,12 +481,12 @@ Drive.prototype.loadScript = function () {
|
|
|
366
481
|
*/
|
|
367
482
|
Drive.prototype._setMainMethod = function (mod) {
|
|
368
483
|
if (!mod) return;
|
|
369
|
-
|
|
484
|
+
let name = this.config.func_name || 'main';
|
|
370
485
|
if (name && typeof mod[name] === 'function') {
|
|
371
486
|
this.main = mod[name];
|
|
372
487
|
}
|
|
373
|
-
for (
|
|
374
|
-
|
|
488
|
+
for (let key in mod) {
|
|
489
|
+
let obj = mod[key];
|
|
375
490
|
if (typeof obj === 'function') {
|
|
376
491
|
this.setMethod(key, obj);
|
|
377
492
|
} else {
|
|
@@ -447,8 +562,8 @@ Drive.prototype.getModel = function (type) {
|
|
|
447
562
|
* @returns {string} 配置文件内容
|
|
448
563
|
*/
|
|
449
564
|
Drive.prototype._createConfigFile = function (file, model) {
|
|
450
|
-
|
|
451
|
-
|
|
565
|
+
let tpl = this._getConfigTpl();
|
|
566
|
+
let content = this.renderTpl(tpl, model || this.getModel('config'));
|
|
452
567
|
let code = this.prettyCode(content, 'json');
|
|
453
568
|
file.saveText(code);
|
|
454
569
|
return file;
|
|
@@ -470,7 +585,7 @@ Drive.prototype.createConfigFile = function (file, model) {
|
|
|
470
585
|
* @returns {string} 配置模板内容
|
|
471
586
|
*/
|
|
472
587
|
Drive.prototype._getConfigTpl = function () {
|
|
473
|
-
|
|
588
|
+
let f = './config.tpl.json'.fullname(this._getTplDir());
|
|
474
589
|
if (f.hasFile()) {
|
|
475
590
|
return f.loadText();
|
|
476
591
|
} else {
|
|
@@ -553,11 +668,11 @@ Drive.prototype._findConfigByName = function (config, name) {
|
|
|
553
668
|
* @private
|
|
554
669
|
*/
|
|
555
670
|
Drive.prototype._handleHotReload = function (config) {
|
|
556
|
-
|
|
671
|
+
let old_config = { ...this.config };
|
|
557
672
|
this.setConfig(config);
|
|
558
673
|
|
|
559
674
|
// 调用 _onConfigChange 钩子(如果脚本中定义了)
|
|
560
|
-
|
|
675
|
+
let on_config_change = this.getMethod('_onConfigChange');
|
|
561
676
|
if (typeof on_config_change === 'function') {
|
|
562
677
|
on_config_change.call(this, old_config, this.config);
|
|
563
678
|
}
|
|
@@ -671,7 +786,7 @@ Drive.prototype._getName = function (file) {
|
|
|
671
786
|
let name = file.dirname().basename();
|
|
672
787
|
return name;
|
|
673
788
|
// // 提取文件名作为模块名
|
|
674
|
-
//
|
|
789
|
+
// let word = new Word();
|
|
675
790
|
// // 将文件名改为驼峰命名法
|
|
676
791
|
// return word.toCamelCase(name);
|
|
677
792
|
};
|
|
@@ -694,8 +809,7 @@ Drive.prototype.loadBefore = function () {
|
|
|
694
809
|
/**
|
|
695
810
|
* 加载处理
|
|
696
811
|
*/
|
|
697
|
-
Drive.prototype._loadCore = function () {
|
|
698
|
-
};
|
|
812
|
+
Drive.prototype._loadCore = function () { };
|
|
699
813
|
|
|
700
814
|
/**
|
|
701
815
|
* 保存配置
|
package/lib/manager.js
CHANGED
|
@@ -4,6 +4,12 @@ const { Scanner } = require('./scanner.js');
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* 管理器类
|
|
7
|
+
* @class
|
|
8
|
+
* @extends Mod
|
|
9
|
+
* @param {object} config 配置项
|
|
10
|
+
* @param {object} parent 父项对象
|
|
11
|
+
* @param {object} mods 模块对象列表
|
|
12
|
+
* @param {object} Drive 驱动对象
|
|
7
13
|
*/
|
|
8
14
|
class Manager extends Mod {
|
|
9
15
|
/**
|
|
@@ -260,10 +266,10 @@ Manager.prototype.newMod = function (config_file, config, script) {
|
|
|
260
266
|
let mod;
|
|
261
267
|
switch (this.config.mod_type) {
|
|
262
268
|
case 'script':
|
|
263
|
-
mod = new Mod(config, this);
|
|
269
|
+
mod = new Mod(config, this.getParent() || this);
|
|
264
270
|
break;
|
|
265
271
|
default:
|
|
266
|
-
mod = new this.Drive(config, this);
|
|
272
|
+
mod = new this.Drive(config, this.getParent() || this);
|
|
267
273
|
break;
|
|
268
274
|
}
|
|
269
275
|
mod.mode = this.config.mode;
|
|
@@ -960,7 +966,7 @@ Manager.prototype.createFile = function (config, file) {
|
|
|
960
966
|
if (this.getInfo(config.name)) {
|
|
961
967
|
throw new Error(`创建失败,原因:模块${config.name}已存在`);
|
|
962
968
|
}
|
|
963
|
-
let mod = new this.Drive(config, this);
|
|
969
|
+
let mod = new this.Drive(config, this.getParent() || this);
|
|
964
970
|
let f = file;
|
|
965
971
|
if (!f) {
|
|
966
972
|
f = `${config.name}/${this.config.filename}`;
|
package/lib/mod.js
CHANGED
|
@@ -1,32 +1,5 @@
|
|
|
1
1
|
const { Base } = require('mm_expand');
|
|
2
|
-
|
|
3
|
-
require('mm_hot_reload');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 增强require函数,专门用于脚本文件(.js)热重载
|
|
7
|
-
* 当脚本文件内容发生变化时,自动重新加载并触发回调
|
|
8
|
-
* @param {string} file 脚本文件路径(.js)
|
|
9
|
-
* @param {Function} func 热更新回调函数,文件变化时触发
|
|
10
|
-
* @returns {object} 加载的模块对象
|
|
11
|
-
* @throws {TypeError} 文件路径不是字符串或回调不是函数时抛出
|
|
12
|
-
*/
|
|
13
|
-
$.require = function (file, func) {
|
|
14
|
-
// 参数校验
|
|
15
|
-
if (typeof file !== 'string') {
|
|
16
|
-
throw new TypeError('第一个参数(file)必须是字符串');
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (func && typeof func !== 'function') {
|
|
20
|
-
throw new TypeError('第二个参数(func)必须是函数');
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// 确保文件是.js文件
|
|
24
|
-
if (!file.endsWith('.js')) {
|
|
25
|
-
throw new TypeError('$.require 只能用于.js脚本文件');
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return $.mod.load(file, func);
|
|
29
|
-
};
|
|
2
|
+
require('./auto_require.js');
|
|
30
3
|
|
|
31
4
|
/**
|
|
32
5
|
* 增强JSON加载函数,专门用于配置文件(.json)热重载
|
|
@@ -147,7 +120,7 @@ Mod.prototype._wrapError = function (err, context = {}) {
|
|
|
147
120
|
// 已包装过的不再重复包装
|
|
148
121
|
if (err.__wrapped) return err;
|
|
149
122
|
|
|
150
|
-
|
|
123
|
+
let parts = [];
|
|
151
124
|
// 附加配置文件路径
|
|
152
125
|
if (this.config_file) {
|
|
153
126
|
parts.push('cfg:' + this.config_file);
|
|
@@ -160,10 +133,10 @@ Mod.prototype._wrapError = function (err, context = {}) {
|
|
|
160
133
|
if (context.method) {
|
|
161
134
|
parts.push(context.method);
|
|
162
135
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
136
|
+
let prefix = '[' + this.constructor.name + '.' + this.config.name + ']';
|
|
137
|
+
let suffix = parts.length > 0 ? ' (' + parts.join(', ') + ')' : '';
|
|
138
|
+
let msg = prefix + suffix + ' - ' + err.message;
|
|
139
|
+
let wrapped = new Error(msg);
|
|
167
140
|
wrapped.stack = wrapped.stack + '\n' + err.stack;
|
|
168
141
|
wrapped.__wrapped = true;
|
|
169
142
|
return wrapped;
|
|
@@ -717,7 +690,7 @@ Mod.prototype.call = async function (method, ...params) {
|
|
|
717
690
|
let result;
|
|
718
691
|
|
|
719
692
|
try {
|
|
720
|
-
|
|
693
|
+
let before_result = await this._callBefore(method, params);
|
|
721
694
|
if (before_result) return before_result;
|
|
722
695
|
result = await this._callMain(method_func, params, before_result);
|
|
723
696
|
result = await this._callAfter(method, params, result);
|
|
@@ -785,13 +758,22 @@ Mod.prototype._callAfter = async function (method, params, result) {
|
|
|
785
758
|
}
|
|
786
759
|
};
|
|
787
760
|
|
|
761
|
+
/**
|
|
762
|
+
* 获取父模块
|
|
763
|
+
* @returns {object} 父模块对象
|
|
764
|
+
*/
|
|
765
|
+
Mod.prototype.getParent = function () {
|
|
766
|
+
return null;
|
|
767
|
+
};
|
|
768
|
+
|
|
788
769
|
/**
|
|
789
770
|
* 获取模块目录
|
|
790
771
|
* @returns {string} 模块目录
|
|
791
772
|
*/
|
|
792
773
|
Mod.prototype.getDir = function () {
|
|
793
|
-
|
|
794
|
-
|
|
774
|
+
let parent = this.getParent();
|
|
775
|
+
if (!parent) return '';
|
|
776
|
+
return parent.getDir();
|
|
795
777
|
};
|
|
796
778
|
|
|
797
779
|
module.exports = {
|
package/lib/scanner.js
CHANGED
|
@@ -26,8 +26,8 @@ class Scanner {
|
|
|
26
26
|
* @returns {Array} 文件路径数组
|
|
27
27
|
*/
|
|
28
28
|
Scanner.prototype.scan = async function (dir, filename, dirname) {
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
let dirs = await this.scanDir(dir, dirname);
|
|
30
|
+
let results = await Promise.all(
|
|
31
31
|
dirs.map((d) => {
|
|
32
32
|
return $.file.getAllAsync(d, filename);
|
|
33
33
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_machine",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.8",
|
|
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": {
|