mm_machine 3.3.7 → 3.3.9
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 +12 -12
- package/lib/drive.js +27 -23
- package/lib/mod.js +9 -8
- package/lib/scanner.js +2 -2
- package/package.json +6 -6
package/lib/auto_require.js
CHANGED
|
@@ -9,7 +9,7 @@ const _AUTO_INST = {};
|
|
|
9
9
|
*/
|
|
10
10
|
function _extractPkgName(id) {
|
|
11
11
|
if (id.startsWith('@')) {
|
|
12
|
-
|
|
12
|
+
let parts = id.split('/');
|
|
13
13
|
if (parts.length >= 2) {
|
|
14
14
|
return parts[0] + '/' + parts[1];
|
|
15
15
|
}
|
|
@@ -23,7 +23,7 @@ function _extractPkgName(id) {
|
|
|
23
23
|
* @returns {string} 模块名
|
|
24
24
|
*/
|
|
25
25
|
function _extractErrName(e) {
|
|
26
|
-
|
|
26
|
+
let match = e.message.match(/'([^']+)'/);
|
|
27
27
|
return match ? match[1] : '';
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -42,14 +42,14 @@ function _isLocal(name) {
|
|
|
42
42
|
*/
|
|
43
43
|
function _clearPkgCache(name) {
|
|
44
44
|
try {
|
|
45
|
-
|
|
45
|
+
let main_file = require.resolve(name);
|
|
46
46
|
delete require.cache[main_file];
|
|
47
47
|
} catch (e) {
|
|
48
48
|
void e;
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
for (
|
|
50
|
+
let sep = require('path').sep;
|
|
51
|
+
let marker = sep + 'node_modules' + sep + name + sep;
|
|
52
|
+
for (let key in require.cache) {
|
|
53
53
|
if (key.indexOf(marker) !== -1) {
|
|
54
54
|
delete require.cache[key];
|
|
55
55
|
}
|
|
@@ -62,9 +62,9 @@ function _clearPkgCache(name) {
|
|
|
62
62
|
* @param {string} cwd 安装目录
|
|
63
63
|
*/
|
|
64
64
|
function _npmInstall(name, cwd) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
68
|
exec_sync(cmd + ' install ' + name + ' --save-prod',
|
|
69
69
|
{ cwd: install_dir, stdio: 'pipe' });
|
|
70
70
|
}
|
|
@@ -82,11 +82,11 @@ function _tryInstall(file) {
|
|
|
82
82
|
if (e.code !== 'MODULE_NOT_FOUND') {
|
|
83
83
|
return false;
|
|
84
84
|
}
|
|
85
|
-
|
|
85
|
+
let raw_name = _extractErrName(e);
|
|
86
86
|
if (!raw_name || _isLocal(raw_name)) {
|
|
87
87
|
return false;
|
|
88
88
|
}
|
|
89
|
-
|
|
89
|
+
let pkg_name = _extractPkgName(raw_name);
|
|
90
90
|
if (!pkg_name || _AUTO_INST[pkg_name]) {
|
|
91
91
|
return false;
|
|
92
92
|
}
|
|
@@ -116,7 +116,7 @@ $.require = function (file, func) {
|
|
|
116
116
|
throw new TypeError('$.require 只能用于.js脚本文件');
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
|
|
119
|
+
let mod = $.mod.load(file, func);
|
|
120
120
|
if (mod !== null) {
|
|
121
121
|
return mod;
|
|
122
122
|
}
|
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 = {
|
|
@@ -164,11 +164,11 @@ Drive.prototype.getConfig = function () {
|
|
|
164
164
|
* 缓存存在时会补全config.options中新增键的默认值
|
|
165
165
|
*/
|
|
166
166
|
Drive.prototype._initOptions = function () {
|
|
167
|
-
|
|
167
|
+
let options = this.config.options;
|
|
168
168
|
if (!options) return;
|
|
169
|
-
|
|
169
|
+
let cache_file = this._getOptionsFile();
|
|
170
170
|
|
|
171
|
-
|
|
171
|
+
let cached = {};
|
|
172
172
|
// 尝试从缓存文件加载已有键值对
|
|
173
173
|
if (cache_file && cache_file.hasFile()) {
|
|
174
174
|
cached = cache_file.loadJson() || {};
|
|
@@ -176,7 +176,7 @@ Drive.prototype._initOptions = function () {
|
|
|
176
176
|
|
|
177
177
|
this._options = {};
|
|
178
178
|
// 根据config.options生成键值对,缓存值优先,否则取默认值
|
|
179
|
-
for (
|
|
179
|
+
for (let key in options) {
|
|
180
180
|
if (cached.hasOwnProperty(key) && cached[key] !== undefined) {
|
|
181
181
|
this._options[key] = cached[key];
|
|
182
182
|
} else {
|
|
@@ -199,8 +199,8 @@ Drive.prototype._getCacheDir = function () {
|
|
|
199
199
|
*/
|
|
200
200
|
Drive.prototype._getOptionsFileName = function () {
|
|
201
201
|
// 先获取上级名
|
|
202
|
-
let parent_name = '';
|
|
203
202
|
let parent = this.getParent();
|
|
203
|
+
let parent_name = '';
|
|
204
204
|
if (parent) {
|
|
205
205
|
// 获取上级构造名
|
|
206
206
|
let constructor_name = parent.constructor.name || '';
|
|
@@ -208,9 +208,13 @@ Drive.prototype._getOptionsFileName = function () {
|
|
|
208
208
|
let mod_name = parent.config?.name || '';
|
|
209
209
|
parent_name = constructor_name + '_' + mod_name;
|
|
210
210
|
}
|
|
211
|
+
|
|
211
212
|
// 再获取当前模块名
|
|
212
213
|
let current_name = this.constructor.name + '_' + (this.config.name || 'default');
|
|
213
|
-
|
|
214
|
+
if (parent_name) {
|
|
215
|
+
current_name = parent_name + '/' + current_name;
|
|
216
|
+
}
|
|
217
|
+
return `./${current_name}.json`.toLowerCase();
|
|
214
218
|
};
|
|
215
219
|
|
|
216
220
|
/**
|
|
@@ -218,7 +222,7 @@ Drive.prototype._getOptionsFileName = function () {
|
|
|
218
222
|
* @returns {string} 选项缓存文件路径 为空字符串表示未配置缓存目录
|
|
219
223
|
*/
|
|
220
224
|
Drive.prototype._getOptionsFile = function () {
|
|
221
|
-
|
|
225
|
+
let dir = this._getCacheDir();
|
|
222
226
|
if (!dir) return '';
|
|
223
227
|
return this._getOptionsFileName().fullname(dir);
|
|
224
228
|
};
|
|
@@ -263,7 +267,7 @@ Drive.prototype.setOption = function (key, value) {
|
|
|
263
267
|
}
|
|
264
268
|
this._options[key] = value;
|
|
265
269
|
|
|
266
|
-
|
|
270
|
+
let cache_file = this._getOptionsFile();
|
|
267
271
|
if (cache_file) {
|
|
268
272
|
// 确保目录存在
|
|
269
273
|
cache_file.addDir();
|
|
@@ -390,8 +394,8 @@ Drive.prototype.renderTpl = function (tpl, model) {
|
|
|
390
394
|
* @returns {boolean} 创建是否成功
|
|
391
395
|
*/
|
|
392
396
|
Drive.prototype._createScriptFile = function (file) {
|
|
393
|
-
|
|
394
|
-
|
|
397
|
+
let tpl = this._getScriptTpl();
|
|
398
|
+
let content = this.renderTpl(tpl, this.getModel('script'));
|
|
395
399
|
if (!content) {
|
|
396
400
|
return '';
|
|
397
401
|
}
|
|
@@ -405,7 +409,7 @@ Drive.prototype._createScriptFile = function (file) {
|
|
|
405
409
|
* @returns {string} 脚本模板内容
|
|
406
410
|
*/
|
|
407
411
|
Drive.prototype._getScriptTpl = function () {
|
|
408
|
-
|
|
412
|
+
let f = './script.tpl.js'.fullname(this._getTplDir());
|
|
409
413
|
if (f.hasFile()) {
|
|
410
414
|
return f.loadText();
|
|
411
415
|
} else {
|
|
@@ -455,13 +459,13 @@ Drive.prototype.loadScript = function () {
|
|
|
455
459
|
if (!file) return null;
|
|
456
460
|
try {
|
|
457
461
|
if (this.mode === MODE.HOT_RELOAD || this.mode === MODE.HOT_BOTH) {
|
|
458
|
-
|
|
462
|
+
let cs = $.require(file, (mod) => {
|
|
459
463
|
this._setMainMethod(mod);
|
|
460
464
|
});
|
|
461
465
|
this._setMainMethod(cs);
|
|
462
466
|
return cs;
|
|
463
467
|
} else {
|
|
464
|
-
|
|
468
|
+
let cs = require(file);
|
|
465
469
|
this._setMainMethod(cs);
|
|
466
470
|
return cs;
|
|
467
471
|
}
|
|
@@ -477,12 +481,12 @@ Drive.prototype.loadScript = function () {
|
|
|
477
481
|
*/
|
|
478
482
|
Drive.prototype._setMainMethod = function (mod) {
|
|
479
483
|
if (!mod) return;
|
|
480
|
-
|
|
484
|
+
let name = this.config.func_name || 'main';
|
|
481
485
|
if (name && typeof mod[name] === 'function') {
|
|
482
486
|
this.main = mod[name];
|
|
483
487
|
}
|
|
484
|
-
for (
|
|
485
|
-
|
|
488
|
+
for (let key in mod) {
|
|
489
|
+
let obj = mod[key];
|
|
486
490
|
if (typeof obj === 'function') {
|
|
487
491
|
this.setMethod(key, obj);
|
|
488
492
|
} else {
|
|
@@ -558,8 +562,8 @@ Drive.prototype.getModel = function (type) {
|
|
|
558
562
|
* @returns {string} 配置文件内容
|
|
559
563
|
*/
|
|
560
564
|
Drive.prototype._createConfigFile = function (file, model) {
|
|
561
|
-
|
|
562
|
-
|
|
565
|
+
let tpl = this._getConfigTpl();
|
|
566
|
+
let content = this.renderTpl(tpl, model || this.getModel('config'));
|
|
563
567
|
let code = this.prettyCode(content, 'json');
|
|
564
568
|
file.saveText(code);
|
|
565
569
|
return file;
|
|
@@ -581,7 +585,7 @@ Drive.prototype.createConfigFile = function (file, model) {
|
|
|
581
585
|
* @returns {string} 配置模板内容
|
|
582
586
|
*/
|
|
583
587
|
Drive.prototype._getConfigTpl = function () {
|
|
584
|
-
|
|
588
|
+
let f = './config.tpl.json'.fullname(this._getTplDir());
|
|
585
589
|
if (f.hasFile()) {
|
|
586
590
|
return f.loadText();
|
|
587
591
|
} else {
|
|
@@ -664,11 +668,11 @@ Drive.prototype._findConfigByName = function (config, name) {
|
|
|
664
668
|
* @private
|
|
665
669
|
*/
|
|
666
670
|
Drive.prototype._handleHotReload = function (config) {
|
|
667
|
-
|
|
671
|
+
let old_config = { ...this.config };
|
|
668
672
|
this.setConfig(config);
|
|
669
673
|
|
|
670
674
|
// 调用 _onConfigChange 钩子(如果脚本中定义了)
|
|
671
|
-
|
|
675
|
+
let on_config_change = this.getMethod('_onConfigChange');
|
|
672
676
|
if (typeof on_config_change === 'function') {
|
|
673
677
|
on_config_change.call(this, old_config, this.config);
|
|
674
678
|
}
|
|
@@ -782,7 +786,7 @@ Drive.prototype._getName = function (file) {
|
|
|
782
786
|
let name = file.dirname().basename();
|
|
783
787
|
return name;
|
|
784
788
|
// // 提取文件名作为模块名
|
|
785
|
-
//
|
|
789
|
+
// let word = new Word();
|
|
786
790
|
// // 将文件名改为驼峰命名法
|
|
787
791
|
// return word.toCamelCase(name);
|
|
788
792
|
};
|
package/lib/mod.js
CHANGED
|
@@ -120,7 +120,7 @@ Mod.prototype._wrapError = function (err, context = {}) {
|
|
|
120
120
|
// 已包装过的不再重复包装
|
|
121
121
|
if (err.__wrapped) return err;
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
let parts = [];
|
|
124
124
|
// 附加配置文件路径
|
|
125
125
|
if (this.config_file) {
|
|
126
126
|
parts.push('cfg:' + this.config_file);
|
|
@@ -133,10 +133,10 @@ Mod.prototype._wrapError = function (err, context = {}) {
|
|
|
133
133
|
if (context.method) {
|
|
134
134
|
parts.push(context.method);
|
|
135
135
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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);
|
|
140
140
|
wrapped.stack = wrapped.stack + '\n' + err.stack;
|
|
141
141
|
wrapped.__wrapped = true;
|
|
142
142
|
return wrapped;
|
|
@@ -690,7 +690,7 @@ Mod.prototype.call = async function (method, ...params) {
|
|
|
690
690
|
let result;
|
|
691
691
|
|
|
692
692
|
try {
|
|
693
|
-
|
|
693
|
+
let before_result = await this._callBefore(method, params);
|
|
694
694
|
if (before_result) return before_result;
|
|
695
695
|
result = await this._callMain(method_func, params, before_result);
|
|
696
696
|
result = await this._callAfter(method, params, result);
|
|
@@ -771,8 +771,9 @@ Mod.prototype.getParent = function () {
|
|
|
771
771
|
* @returns {string} 模块目录
|
|
772
772
|
*/
|
|
773
773
|
Mod.prototype.getDir = function () {
|
|
774
|
-
|
|
775
|
-
|
|
774
|
+
let parent = this.getParent();
|
|
775
|
+
if (!parent) return '';
|
|
776
|
+
return parent.getDir();
|
|
776
777
|
};
|
|
777
778
|
|
|
778
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.9",
|
|
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": {
|
|
@@ -33,14 +33,14 @@
|
|
|
33
33
|
],
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@prettier/sync": "^0.6.1",
|
|
36
|
-
"mm_config": "^2.4.
|
|
37
|
-
"mm_expand": "^2.4.
|
|
38
|
-
"mm_hot_reload": "^1.4.
|
|
36
|
+
"mm_config": "^2.4.6",
|
|
37
|
+
"mm_expand": "^2.4.6",
|
|
38
|
+
"mm_hot_reload": "^1.4.3",
|
|
39
39
|
"mm_tpl": "^2.6.5"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"eslint": "^10.
|
|
43
|
-
"eslint-plugin-jsdoc": "^63.2
|
|
42
|
+
"eslint": "^10.8.0",
|
|
43
|
+
"eslint-plugin-jsdoc": "^63.3.2",
|
|
44
44
|
"mm_eslint": "^1.7.5"
|
|
45
45
|
}
|
|
46
46
|
}
|