mm_machine 3.3.6 → 3.3.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.
@@ -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
+ var 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
+ var 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
+ var main_file = require.resolve(name);
46
+ delete require.cache[main_file];
47
+ } catch (e) {
48
+ void e;
49
+ }
50
+ var sep = require('path').sep;
51
+ var marker = sep + 'node_modules' + sep + name + sep;
52
+ for (var 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
+ var install_dir = cwd || process.cwd();
66
+ var cmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
67
+ var 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
+ var raw_name = _extractErrName(e);
86
+ if (!raw_name || _isLocal(raw_name)) {
87
+ return false;
88
+ }
89
+ var 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
+ var 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
@@ -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,119 @@ 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
+ var options = this.config.options;
168
+ if (!options) return;
169
+ var cache_file = this._getOptionsFile();
170
+
171
+ var cached = {};
172
+ // 尝试从缓存文件加载已有键值对
173
+ if (cache_file && cache_file.hasFile()) {
174
+ cached = cache_file.loadJson() || {};
175
+ }
176
+
177
+ this._options = {};
178
+ // 根据config.options生成键值对,缓存值优先,否则取默认值
179
+ for (var 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_name = '';
203
+ let parent = this.getParent();
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
+ let current_name = this.constructor.name + '_' + (this.config.name || 'default');
213
+ return `./${parent_name}/${current_name}.json`.toLowerCase();
214
+ };
215
+
216
+ /**
217
+ * 获取选项缓存文件路径
218
+ * @returns {string} 选项缓存文件路径 为空字符串表示未配置缓存目录
219
+ */
220
+ Drive.prototype._getOptionsFile = function () {
221
+ var dir = this._getCacheDir();
222
+ if (!dir) return '';
223
+ return this._getOptionsFileName().fullname(dir);
224
+ };
225
+
226
+ /**
227
+ * 获取所有选项
228
+ * @returns {object} 所有选项
229
+ */
230
+ Drive.prototype.getOptions = function () {
231
+ return this._options || {};
232
+ };
233
+
234
+ /**
235
+ * 获取选项
236
+ * @param {string} key 选项键
237
+ * @returns {*} 选项值
238
+ */
239
+ Drive.prototype.getOption = function (key) {
240
+ let options = this.getOptions();
241
+ return options[key];
242
+ };
243
+
244
+ /**
245
+ * 设置所有选项
246
+ * @param {object} options 所有选项
247
+ */
248
+ Drive.prototype.setOptions = function (options) {
249
+ this._options = options;
250
+ };
251
+
252
+ /**
253
+ * 设置选项并保存到缓存文件
254
+ * @param {string} key 选项键
255
+ * @param {*} value 选项值
256
+ */
257
+ Drive.prototype.setOption = function (key, value) {
258
+ if (typeof key !== 'string') {
259
+ throw new TypeError('key必须是字符串');
260
+ }
261
+ if (!this._options) {
262
+ this._options = {};
263
+ }
264
+ this._options[key] = value;
265
+
266
+ var cache_file = this._getOptionsFile();
267
+ if (cache_file) {
268
+ // 确保目录存在
269
+ cache_file.addDir();
270
+ cache_file.saveJson(this._options);
271
+ }
272
+ };
273
+
166
274
  /**
167
275
  * 获取当前文件
168
276
  * @returns {object} 当前文件对象
@@ -176,10 +284,13 @@ Drive.prototype.getDir = function () {
176
284
  } else if (this.config_file) {
177
285
  dir = this.config_file.dirname();
178
286
  this._dir = dir;
179
- } else if (this.getParent) {
180
- let parent_dir = this.getParent()?.getDir() || '';
181
- if (parent_dir) {
182
- dir = this.config.name.fullname(parent_dir);
287
+ } else {
288
+ let parent = this.getParent();
289
+ if (parent) {
290
+ let parent_dir = parent.getDir() || '';
291
+ if (parent_dir) {
292
+ dir = ('./' + this.config.name).fullname(parent_dir);
293
+ }
183
294
  }
184
295
  }
185
296
  }
@@ -269,9 +380,9 @@ Drive.prototype.newScript = function (file) {
269
380
  this._createScriptFile(file);
270
381
  };
271
382
 
272
- Drive.prototype.renderTpl = function(tpl, model){
383
+ Drive.prototype.renderTpl = function (tpl, model) {
273
384
  return $.tpl.render(tpl, model);
274
- }
385
+ };
275
386
 
276
387
  /**
277
388
  * 创建脚本
@@ -694,8 +805,7 @@ Drive.prototype.loadBefore = function () {
694
805
  /**
695
806
  * 加载处理
696
807
  */
697
- Drive.prototype._loadCore = function () {
698
- };
808
+ Drive.prototype._loadCore = function () { };
699
809
 
700
810
  /**
701
811
  * 保存配置
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)热重载
@@ -785,6 +758,14 @@ 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} 模块目录
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.3.6",
3
+ "version": "3.3.7",
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": {