mm_machine 3.5.6 → 3.6.0

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 CHANGED
@@ -494,6 +494,24 @@ mm_machine 模块系统基于以下核心设计理念:
494
494
  6. **执行方法**:根据应用需求,执行模块的特定方法(如 main)
495
495
  7. **监听文件变化**:在热更新模式下,监听文件变化并自动重新加载模块
496
496
 
497
+ #### 目录监听与热重载
498
+
499
+ `update(dir)` 完成后会对检索目录开启一次递归监听(底层为 mm_scanner,同一实例同时扫描配置文件与 `.js` 脚本,变更按扩展名分流):
500
+
501
+ | 变更 | 模式要求 | 行为 |
502
+ | --- | --- | --- |
503
+ | 配置文件新增 | 任意 | 按配置文件注册模块,非懒加载时立即加载 |
504
+ | 配置文件修改 | `mode >= 2`(热更新配置) | 同步配置快照并应用到模块(触发 `_onConfigChange` 钩子与 `config:change` 事件),`main` 指向变化时重载脚本 |
505
+ | 配置文件删除 | 任意 | 注销模块(停止 → 卸载 → 移除记录) |
506
+ | 配置文件修改 | `mode < 2` | 注销后重新注册模块 |
507
+ | 脚本文件删除 | `mode = 3 / 4`(热重载脚本) | 注销该脚本所属模块,脚本已无法运行 |
508
+ | 脚本新增/重建 | `mode = 3 / 4` | 同目录存在配置文件时补注册,非懒加载时立即加载 |
509
+ | 脚本内容修改 | `mode = 3 / 4` | 管理器重载脚本(清 require 缓存后重新加载并重挂方法) |
510
+
511
+ - 脚本事件只在热重载模式(`MODE.HOT_RELOAD` / `MODE.HOT_BOTH`)下纳入监听,生产与仅热更新配置模式不扫描脚本,避免无谓开销
512
+ - 热更新不依赖 `mm_hot_reload`:配置与脚本的变化均由管理器的目录监听(mm_loader)统一接管;`mm_hot_reload` 仅供函数自行应用其他模块时选用
513
+ - 调用 `unwatch()` 可关闭全部目录监听
514
+
497
515
  ### 数据流向
498
516
 
499
517
  模块间的数据流转遵循以下规则:
@@ -553,6 +571,14 @@ mm_machine 模块系统基于以下核心设计理念:
553
571
 
554
572
  ## 更新日志
555
573
 
574
+ ### v3.6.0
575
+
576
+ - 移除对 `mm_hot_reload` 的依赖:配置与脚本热更新全部由管理器目录监听(mm_loader)接管,`mm_hot_reload` 回归为函数自行应用其他模块时的可选工具
577
+ - 修正 `Drive.setConfig`:`config` 为共享配置记录的 getter,改为原地深合并(原先 `conf()` 重新赋值为静默无效操作,热更新配置从未真正写入模块)
578
+ - 热更新配置模式下,配置变化同步应用到模块(触发 `_onConfigChange` 钩子与 `config:change` 事件),`main` 指向变化时重载脚本
579
+ - 热重载模式下,脚本内容变化由管理器重载(清缓存后重新 require 并重挂方法)
580
+ - 移除 `Drive.watchConfig`、`_loadHotReload`,`_handleHotReload` 更名为 `_applyConfigUpdate`
581
+
556
582
  ### v1.0.0 (初始版本)
557
583
 
558
584
  - 基础模块管理功能
@@ -570,9 +596,11 @@ mm_machine 模块系统基于以下核心设计理念:
570
596
 
571
597
  ## 依赖
572
598
 
573
- - mm_config: ^1.1.4
574
- - mm_loader: 脚本加载与热更新
575
- - mm_expand: 用于路径处理和文件操作
599
+ - mm_config: ^2.5.3
600
+ - mm_helper: ^1.0.3
601
+ - mm_loader: ^1.4.0(目录监听与配置/脚本热更新)
602
+ - mm_mod: ^1.0.6
603
+ - mm_tpl: ^2.7.3
576
604
 
577
605
  ## 许可证
578
606
 
package/lib/drive.js CHANGED
@@ -1,7 +1,5 @@
1
1
  const path = require('path');
2
- const { conf } = require('mm_config');
3
2
  const { getTpl } = require('mm_tpl');
4
- const { hotReloadAdmin } = require('mm_hot_reload');
5
3
  const {
6
4
  string,
7
5
  fullname,
@@ -19,20 +17,6 @@ const {
19
17
  } = require('mm_helper');
20
18
  const { Mod } = require('./mod');
21
19
 
22
- // 热重载管理器,按作用域惰性创建,避免模块加载时立即启动文件监听
23
- let hot_reload_admin = null;
24
-
25
- /**
26
- * 获取热重载管理器
27
- * @returns {object} 热重载管理器实例
28
- */
29
- function getHotReload() {
30
- if (!hot_reload_admin) {
31
- hot_reload_admin = hotReloadAdmin('server');
32
- }
33
- return hot_reload_admin;
34
- }
35
-
36
20
  const MODE = {
37
21
  // 生产模式
38
22
  PRODUCTION: 1,
@@ -134,39 +118,15 @@ Drive.prototype.applyConfig = function (file, config) {
134
118
  };
135
119
 
136
120
  /**
137
- * 监听配置文件变化(热更新模式下生效)
138
- * @param {string} file 配置文件路径
139
- * @returns {object|null} 热更新管理器加载的配置,未开启热更新时返回 null
140
- */
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;
149
- };
150
-
151
- /**
152
- * 设置配置
121
+ * 设置配置(配置由管理器读取后传入)
122
+ * @description config 是共享配置记录的 getter,只能原地合并,不可整体替换
153
123
  * @param {object} config 配置对象
154
124
  * @returns {object} 配置项
155
125
  */
156
126
  Drive.prototype.setConfig = function (config) {
157
127
  if (config) {
158
- if (this.config_file) {
159
- // 合并配置并应用配置处理
160
- this.config = conf(
161
- {
162
- ...this.config,
163
- ...config
164
- },
165
- this.config_file
166
- );
167
- } else {
168
- Object.assign(this.config, config);
169
- }
128
+ // 深合并进共享配置记录
129
+ this._setConfig(config);
170
130
  }
171
131
  var file = this.config.main;
172
132
  if (file) {
@@ -399,15 +359,11 @@ Drive.prototype._remove = function () {
399
359
  if (!file) return;
400
360
 
401
361
  try {
402
- if (this.mode === MODE.HOT_RELOAD || this.mode === MODE.HOT_BOTH) {
403
- getHotReload().unload(file);
404
- } else {
405
- // 移除模块缓存
406
- let file_name = require.resolve(file);
407
- if (require.cache[file_name]) {
408
- require.cache[file_name] = null;
409
- delete require.cache[file_name];
410
- }
362
+ // 移除模块缓存,重新加载时会读取最新脚本
363
+ let file_name = require.resolve(file);
364
+ if (require.cache[file_name]) {
365
+ require.cache[file_name] = null;
366
+ delete require.cache[file_name];
411
367
  }
412
368
  } catch (err) {
413
369
  this.log('error', `移除加载的脚本模块失败`, err);
@@ -518,26 +474,18 @@ Drive.prototype._getScriptTpl = function () {
518
474
  };
519
475
 
520
476
  /**
521
- * 加载脚本文件,支持热重载模式
522
- * 在开发模式(mode=3或4)下使用热重载管理器进行热重载
523
- * 在生产模式下使用标准的 require 加载
477
+ * 加载脚本文件
478
+ * @description 统一通过 require 加载;脚本变化的重载由上层目录监听触发,
479
+ * 本方法只负责读取最新脚本并挂载方法
524
480
  * @returns {object | null} 加载的模块对象
525
481
  */
526
482
  Drive.prototype.loadScript = function () {
527
483
  let file = this._getScriptFile();
528
484
  if (!file) return null;
529
485
  try {
530
- if (this.mode === MODE.HOT_RELOAD || this.mode === MODE.HOT_BOTH) {
531
- let cs = getHotReload().load(file, (mod) => {
532
- this._setMainMethod(mod);
533
- });
534
- this._setMainMethod(cs);
535
- return cs;
536
- } else {
537
- let cs = require(file);
538
- this._setMainMethod(cs);
539
- return cs;
540
- }
486
+ let cs = require(file);
487
+ this._setMainMethod(cs);
488
+ return cs;
541
489
  } catch (err) {
542
490
  this.log('error', '加载脚本文件失败', this._wrapError(err));
543
491
  return null;
@@ -582,6 +530,13 @@ Drive.prototype.newConfig = function (file) {
582
530
  this.config.name = this._getName(file);
583
531
  this.config.scope = this._getScope(file);
584
532
  }
533
+ // 归属信息:app 与注册域一致(scope 即应用名),plugin 取路径中的插件段,应用级资源为空
534
+ if (!this.config.app) {
535
+ this.config.app = this._getScope(file) || 'os';
536
+ }
537
+ if (this.config.plugin === undefined) {
538
+ this.config.plugin = string.between(file.replace(/\\/g, '/'), 'plugin/', '/') || '';
539
+ }
585
540
  this._createConfigFile(file);
586
541
  };
587
542
 
@@ -697,7 +652,7 @@ Drive.prototype.loadFile = function (file, name) {
697
652
 
698
653
  if (!text) return null;
699
654
 
700
- let config = this._loadHotReload(filename);
655
+ let config = loadJson(filename);
701
656
  let final_config = this._findConfigByName(config, name);
702
657
  return final_config;
703
658
  } catch (err) {
@@ -706,21 +661,6 @@ Drive.prototype.loadFile = function (file, name) {
706
661
  }
707
662
  };
708
663
 
709
- /**
710
- * 加载配置文件(JSON5 语法),热更新模式下同时注册文件监听
711
- * @param {string} file 完整文件路径
712
- * @returns {object | Array | null | undefined} 配置对象
713
- */
714
- Drive.prototype._loadHotReload = function (file) {
715
- if (!file) return null;
716
- if (this.mode >= MODE.HOT_CONFIG) {
717
- return getHotReload().load(file, (config) => {
718
- this._handleHotReload(config);
719
- });
720
- }
721
- return loadJson(file);
722
- };
723
-
724
664
  /**
725
665
  * 根据名称在配置数组中查找配置项
726
666
  * @param {object | Array} config 配置对象或数组
@@ -733,12 +673,14 @@ Drive.prototype._findConfigByName = function (config, name) {
733
673
  };
734
674
 
735
675
  /**
736
- * 热更新回调处理
676
+ * 应用更新后的配置
677
+ * @description 由上层目录监听在配置文件变化时调用:
678
+ * 合并配置、调用 _onConfigChange 钩子、触发事件并按需重载脚本
737
679
  * @param {object} config 配置对象
738
680
  * @returns {object} 配置对象
739
681
  * @private
740
682
  */
741
- Drive.prototype._handleHotReload = function (config) {
683
+ Drive.prototype._applyConfigUpdate = function (config) {
742
684
  var old_script = this.script_file;
743
685
  var old_config = { ...this.config };
744
686
  this.setConfig(config);
package/lib/manager.js CHANGED
@@ -1,5 +1,6 @@
1
+ const path = require('path');
1
2
  const { Manager: Core } = require('mm_mod');
2
- const { hasDir, hasFile, fullname, basename, dirname } = require('mm_helper');
3
+ const { hasDir, hasFile, fullname, basename, dirname, string } = require('mm_helper');
3
4
  const { Loader } = require('mm_loader');
4
5
  const { Mod, addModMethods } = require('./mod.js');
5
6
  const { Drive, MODE } = require('./drive.js');
@@ -191,10 +192,11 @@ Manager.prototype.getTplDir = function () {
191
192
 
192
193
  /**
193
194
  * 获取配置加载器
195
+ * @param {boolean} [watch_scripts] 是否把脚本文件纳入目录监听
194
196
  * @returns {Loader} 配置加载器
195
197
  * @private
196
198
  */
197
- Manager.prototype._getLoader = function () {
199
+ Manager.prototype._getLoader = function (watch_scripts = false) {
198
200
  return new Loader({
199
201
  resources: {
200
202
  mod: {
@@ -202,10 +204,23 @@ Manager.prototype._getLoader = function () {
202
204
  hot: false
203
205
  }
204
206
  },
205
- ignores: this.config.ignore
207
+ ignores: this.config.ignore,
208
+ // 只有目录监听且处于热重载模式时才扫描脚本,其余场景保持只扫配置文件
209
+ scripts: { hot: !!watch_scripts }
206
210
  });
207
211
  };
208
212
 
213
+ /**
214
+ * 是否需要响应脚本文件变化
215
+ * @description 仅热重载(3)与热更新+重载(4)模式由驱动重载脚本,其余模式不响应
216
+ * @returns {boolean} 需要响应返回true
217
+ * @private
218
+ */
219
+ Manager.prototype._isScriptHot = function () {
220
+ let mode = this.config.mode;
221
+ return mode === MODE.HOT_RELOAD || mode === MODE.HOT_BOTH;
222
+ };
223
+
209
224
  // ==== 模块管理接口 ====
210
225
  /**
211
226
  * 清除所有模块
@@ -271,13 +286,32 @@ Manager.prototype.getMods = function () {
271
286
  */
272
287
  Manager.prototype._readModConfig = function (config_file, config = {}) {
273
288
  let mod_config = { ...config };
274
- if (mod_config.name || !config_file) {
275
- return mod_config;
289
+ if (!mod_config.name && config_file) {
290
+ let file_config = hasFile(config_file) ? this._getLoader().load(config_file) : null;
291
+ mod_config = { ...file_config, ...mod_config };
292
+ if (!mod_config.name) {
293
+ mod_config.name = basename(dirname(config_file));
294
+ }
276
295
  }
277
- let file_config = hasFile(config_file) ? this._getLoader().load(config_file) : null;
278
- mod_config = { ...file_config, ...mod_config };
279
- if (!mod_config.name) {
280
- mod_config.name = basename(dirname(config_file));
296
+ // 驱动目录默认为配置文件所在目录,供子类解析主脚本与参数、SQL配置的相对路径
297
+ if (!mod_config.dir && config_file) {
298
+ mod_config.dir = dirname(config_file);
299
+ }
300
+ // 归属信息:app 为所属应用(声明值优先,兼作注册域),plugin 为所属插件(应用级资源为空),
301
+ // scope 为注册域(与管理器域一致的推导口径,即该资源配置到 require(module).admin 的哪个域)。
302
+ // 旧版 update(dir) 全量扫描注册(如 xxx_web 网关镜像)不经过 load_resources/_addResource,
303
+ // 归属信息统一在这里兜底注入
304
+ if (config_file) {
305
+ const norm_path = String(config_file).replace(/\\/g, '/');
306
+ if (mod_config.app === undefined || mod_config.app === '') {
307
+ mod_config.app = string.between(norm_path, 'app/', '/') || 'os';
308
+ }
309
+ if (mod_config.plugin === undefined) {
310
+ mod_config.plugin = string.between(norm_path, 'plugin/', '/') || '';
311
+ }
312
+ if (mod_config.scope === undefined || typeof mod_config.scope === 'boolean') {
313
+ mod_config.scope = mod_config.app;
314
+ }
281
315
  }
282
316
  return mod_config;
283
317
  };
@@ -305,9 +339,12 @@ Manager.prototype.newMod = function (config_file, config, script) {
305
339
  }
306
340
  mod.mode = this.config.mode;
307
341
 
342
+ // 仅 Drive 子类提供配置文件应用能力,Mod 子类(如 api、event、plugin)构造时已持有完整配置
343
+ // 配置与脚本的热更新由目录监听统一负责(见 _applyChangesAsync / _applyScriptChangesAsync)
308
344
  if (mod_config && config_file) {
309
- mod.applyConfig(config_file, mod_config);
310
- mod.watchConfig(config_file);
345
+ if (typeof mod.applyConfig === 'function') {
346
+ mod.applyConfig(config_file, mod_config);
347
+ }
311
348
  }
312
349
 
313
350
  if (script) {
@@ -512,18 +549,27 @@ Manager.prototype._findFiles = async function (dir) {
512
549
 
513
550
  // ==== 目录监听 ====
514
551
  /**
515
- * 监听目录中配置文件的变化
516
- * @description 只扫描一次,之后由文件事件增量维护模块
552
+ * 监听目录中配置文件与脚本文件的变化
553
+ * @description 只扫描一次,之后由文件事件增量维护模块;脚本事件仅在热重载模式下响应
517
554
  * @param {string} dir 目录路径
518
555
  * @private
519
556
  */
520
557
  Manager.prototype._watchDir = function (dir) {
521
558
  if (!dir || this._dir_watchers.has(dir)) return;
522
559
  if (!hasDir(dir)) return;
523
- let loader = this._getLoader();
524
- let success = loader.watch(dir, (changes) => {
525
- this._applyChanges(changes);
526
- });
560
+ let script_hot = this._isScriptHot();
561
+ let loader = this._getLoader(script_hot);
562
+ let success = loader.watch(
563
+ dir,
564
+ (changes) => {
565
+ this._applyChanges(changes);
566
+ },
567
+ script_hot
568
+ ? (changes) => {
569
+ this._applyScriptChanges(changes);
570
+ }
571
+ : undefined
572
+ );
527
573
  if (success) {
528
574
  this._dir_watchers.set(dir, loader);
529
575
  }
@@ -563,6 +609,12 @@ Manager.prototype._applyChangesAsync = async function (changes) {
563
609
 
564
610
  if (change.type === 'change' && info && hot) {
565
611
  this._syncInfo(info, change.file);
612
+ // 同步应用新配置到模块:合并配置、触发 _onConfigChange 钩子与 config:change 事件,
613
+ // main 指向的脚本路径变化时由模块自行重载脚本
614
+ let mod = this.getMod(info.name);
615
+ if (mod && typeof mod._applyConfigUpdate === 'function') {
616
+ mod._applyConfigUpdate(this._readModConfig(change.file));
617
+ }
566
618
  continue;
567
619
  }
568
620
 
@@ -598,6 +650,97 @@ Manager.prototype._syncInfo = function (info, file) {
598
650
  Object.assign(info, mod_config, { config_file: file });
599
651
  };
600
652
 
653
+ /**
654
+ * 应用脚本文件变更
655
+ * @param {object[]} changes 变更列表,每项含 type 与 file
656
+ * @private
657
+ */
658
+ Manager.prototype._applyScriptChanges = function (changes) {
659
+ this._applyScriptChangesAsync(changes).catch((err) => {
660
+ this.log('error', '应用脚本文件变更失败', err);
661
+ });
662
+ };
663
+
664
+ /**
665
+ * 按变更列表增量维护脚本所属模块
666
+ * @description 脚本删除时注销模块(脚本已无法运行),脚本重建或新增时按同目录配置文件补注册;
667
+ * 脚本内容变化时重载脚本(清缓存后重新 require 并重挂方法)
668
+ * @param {object[]} changes 变更列表,每项含 type 与 file
669
+ * @returns {Promise<void>} 无返回值
670
+ * @private
671
+ */
672
+ Manager.prototype._applyScriptChangesAsync = async function (changes) {
673
+ if (!this._isScriptHot()) return;
674
+ let lazy = this.config.lazy_load;
675
+
676
+ for (let change of changes) {
677
+ try {
678
+ let info = this._getInfoByScript(change.file);
679
+
680
+ if (change.type === 'unlink') {
681
+ if (info) {
682
+ this.log('warn', `脚本已删除,注销模块 ${info.name}: ${change.file}`);
683
+ await this.unregMod(info.name);
684
+ }
685
+ continue;
686
+ }
687
+
688
+ // 已注册模块的脚本内容变化:重载脚本并重挂方法
689
+ if (info) {
690
+ let mod = this.getMod(info.name);
691
+ if (mod && typeof mod.reloadScript === 'function') {
692
+ mod.reloadScript();
693
+ }
694
+ continue;
695
+ }
696
+
697
+ // 脚本重建或新增:同目录存在配置文件时补注册
698
+ let config_file = this._getConfigByScript(change.file);
699
+ if (!config_file) continue;
700
+ let mod = this.regMod(config_file);
701
+ if (mod && !lazy) {
702
+ await this.loadMod(mod.config.name);
703
+ }
704
+ } catch (err) {
705
+ this.log('error', '处理脚本变更失败: ' + change.file, err);
706
+ }
707
+ }
708
+
709
+ this._sort();
710
+ };
711
+
712
+ /**
713
+ * 按脚本文件路径获取模块信息
714
+ * @param {string} file 脚本文件路径
715
+ * @returns {object|null} 模块信息,未注册时返回null
716
+ * @private
717
+ */
718
+ Manager.prototype._getInfoByScript = function (file) {
719
+ let target = path.resolve(file);
720
+ for (let info of this.configs) {
721
+ let mod = this.getMod(info.name);
722
+ if (mod && mod.script_file && path.resolve(mod.script_file) === target) {
723
+ return info;
724
+ }
725
+ }
726
+ return null;
727
+ };
728
+
729
+ /**
730
+ * 按脚本文件路径查找同目录的配置文件
731
+ * @description 脚本重建后据此补注册;已注册或配置文件不存在时返回null
732
+ * @param {string} file 脚本文件路径
733
+ * @returns {string|null} 配置文件路径
734
+ * @private
735
+ */
736
+ Manager.prototype._getConfigByScript = function (file) {
737
+ let filename = this.config.filename;
738
+ if (!filename) return null;
739
+ let config_file = path.join(path.dirname(file), filename);
740
+ if (this._getInfoByFile(config_file)) return null;
741
+ return hasFile(config_file) ? config_file : null;
742
+ };
743
+
601
744
  /**
602
745
  * 关闭全部目录监听
603
746
  * @returns {void}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.5.6",
3
+ "version": "3.6.0",
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": {
@@ -41,9 +41,8 @@
41
41
  "dependencies": {
42
42
  "@prettier/sync": "^0.6.1",
43
43
  "mm_config": "^2.5.3",
44
- "mm_hot_reload": "^1.5.3",
45
44
  "mm_helper": "^1.0.3",
46
- "mm_loader": "^1.1.1",
45
+ "mm_loader": "^1.4.0",
47
46
  "mm_mod": "^1.0.6",
48
47
  "mm_tpl": "^2.7.3"
49
48
  },