mm_machine 2.5.6 → 2.5.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.
Files changed (4) hide show
  1. package/drive.js +21 -3
  2. package/index.js +58 -1
  3. package/mod.js +9 -0
  4. package/package.json +4 -4
package/drive.js CHANGED
@@ -163,6 +163,12 @@ Drive.prototype._getDir = function () {
163
163
  dir = this.config_file.dirname();
164
164
  this._dir = dir;
165
165
  }
166
+ else if (this.getParent) {
167
+ let parent_dir = this.getParent()?.getDir() || '';
168
+ if (parent_dir) {
169
+ dir = this.config.name.fullname(parent_dir);
170
+ }
171
+ }
166
172
  }
167
173
  return dir;
168
174
  };
@@ -172,7 +178,7 @@ Drive.prototype._getDir = function () {
172
178
  * @returns {string} 模板目录
173
179
  */
174
180
  Drive.prototype._getTplDir = function () {
175
- return this.getParent().getTplDir();
181
+ return this.getParent()?.getTplDir() || '';
176
182
  };
177
183
 
178
184
  /**
@@ -418,16 +424,28 @@ Drive.prototype.getModel = function (type) {
418
424
  /**
419
425
  * 创建配置文件
420
426
  * @param {string} file 目标文件路径
427
+ * @param {object} model 模块模型
421
428
  * @returns {string} 配置文件内容
422
429
  */
423
- Drive.prototype._createConfigFile = function (file) {
430
+ Drive.prototype._createConfigFile = function (file, model) {
424
431
  var tpl = this._getConfigTpl();
425
- var content = $.tpl.render(tpl, this.getModel('config'));
432
+ var content = $.tpl.render(tpl, model || this.getModel('config'));
426
433
  let code = this.prettyCode(content, 'json');
427
434
  file.saveText(code);
428
435
  return file;
429
436
  };
430
437
 
438
+ /**
439
+ * 创建配置文件
440
+ * @param {string} file 目标文件路径
441
+ * @param {object} model 模块模型
442
+ * @returns {string} 配置文件内容
443
+ */
444
+ Drive.prototype.createConfigFile = function (file, model) {
445
+ file.addDir();
446
+ return this._createConfigFile(file, model);
447
+ };
448
+
431
449
  /**
432
450
  * 获取配置模板
433
451
  * @returns {string} 配置模板内容
package/index.js CHANGED
@@ -597,12 +597,29 @@ Manager.prototype._initManager = function () {
597
597
 
598
598
  };
599
599
 
600
+ /**
601
+ * 获取公共模块目录
602
+ * @returns {string} 公共模块目录
603
+ */
604
+ Manager.prototype.getBaseDir = function () {
605
+ return this.config.base_dir;
606
+ };
607
+
608
+ /**
609
+ * 获取二次开发模块目录
610
+ * @returns {string} 二次开发模块目录
611
+ */
612
+ Manager.prototype.getDir = function () {
613
+ return this.config.dir || this._getDir();
614
+ };
615
+
600
616
  /**
601
617
  * 加载资源
602
618
  */
603
619
  Manager.prototype._loadSources = async function () {
604
620
  // 通过更新函数加载资源信息
605
- let { base_dir, dir } = this.config;
621
+ let base_dir = this.getBaseDir();
622
+ let dir = this.getDir();
606
623
  if (base_dir) {
607
624
  await this.call('update', base_dir);
608
625
  await this.call('update', dir, false);
@@ -900,6 +917,46 @@ Manager.prototype.offEvent = function (event) {
900
917
  this.getEventer()?.off(event);
901
918
  };
902
919
 
920
+ /**
921
+ * 创建模块
922
+ * @param {object} config 模块配置
923
+ * @returns {string} 创建的模块文件路径
924
+ */
925
+ Manager.prototype.createFile = function (config) {
926
+ if (!config.name) {
927
+ throw new TypeError('模块名称不能为空');
928
+ }
929
+ if (!config.title) {
930
+ throw new TypeError('模块标题不能为空');
931
+ }
932
+ if (!config.description) {
933
+ throw new TypeError('模块描述不能为空');
934
+ }
935
+ if (this.getMod(config.name)) {
936
+ throw new Error(`创建失败,原因:模块${config.name}已存在`);
937
+ }
938
+ let mod = new this.Drive(config, this);
939
+ let file = `${config.name}/${this.config.filename}`;
940
+ if (this.config.search_way === 'dir') {
941
+ let dir_name = this._getDirName();
942
+ file = `${dir_name}/${file}`;
943
+ }
944
+ file = file.fullname(this.config.dir);
945
+ mod.createConfigFile(file, config);
946
+ return file;
947
+ };
948
+
949
+ /**
950
+ * 创建模块
951
+ * @param {object} config 模块配置
952
+ * @returns {object} 创建的模块实例
953
+ */
954
+ Manager.prototype.create = function (config) {
955
+ let file = this.createFile(config);
956
+ let mod = this.registerMod(file, config);
957
+ return mod;
958
+ };
959
+
903
960
  module.exports = {
904
961
  Manager,
905
962
  Drive,
package/mod.js CHANGED
@@ -660,6 +660,15 @@ Mod.prototype._destroyCore = async function () {
660
660
  this._methods = {};
661
661
  };
662
662
 
663
+ /**
664
+ * 获取模块目录
665
+ * @returns {string} 模块目录
666
+ */
667
+ Mod.prototype._getDir = function () {
668
+ if (!this.getParent) return '';
669
+ return this.getParent()?.getDir() || '';
670
+ };
671
+
663
672
  module.exports = {
664
673
  Mod
665
674
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "2.5.6",
3
+ "version": "2.5.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": {
@@ -34,12 +34,12 @@
34
34
  ],
35
35
  "dependencies": {
36
36
  "@prettier/sync": "^0.6.1",
37
- "mm_config": "^2.3.1",
38
- "mm_hot_reload": "^1.3.1",
37
+ "mm_config": "^2.3.2",
38
+ "mm_hot_reload": "^1.3.2",
39
39
  "mm_tpl": "^2.5.0"
40
40
  },
41
41
  "devDependencies": {
42
- "eslint": "^10.2.0",
42
+ "eslint": "^10.2.1",
43
43
  "eslint-plugin-jsdoc": "^62.9.0",
44
44
  "mm_eslint": "^1.7.1"
45
45
  }