mm_machine 1.8.7 → 1.8.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.
@@ -10,7 +10,7 @@
10
10
  /**
11
11
  * 描述, 用于描述该有什么用的
12
12
  */
13
- "description": "用于测试动态加载、更新、卸载、删除脚本",
13
+ "description": "用于测试动态加载、更新、卸载、删除脚本123",
14
14
  /**
15
15
  * 文件路径, 当调用函数不存在时,会先从文件中加载
16
16
  */
@@ -0,0 +1,13 @@
1
+ class Demo {
2
+ constructor() {}
3
+ }
4
+ var i = 0;
5
+ Demo.prototype.main = function() {
6
+ console.log("我很好123", i++);
7
+ return i;
8
+ }
9
+ Demo.prototype.init = function(){
10
+ console.log("初始化test2");
11
+ }
12
+
13
+ module.exports = new Demo();
@@ -1,4 +1,4 @@
1
- {
1
+ [{
2
2
  /**
3
3
  * 名称, 由中英文和下“_”组成, 用于卸载接口 例如: demo
4
4
  */
@@ -14,7 +14,7 @@
14
14
  /**
15
15
  * 文件路径, 当调用函数不存在时,会先从文件中加载
16
16
  */
17
- "func_file": "./index.js",
17
+ "func_file": "./main.js",
18
18
  /**
19
19
  * 回调函数名 用于决定调用脚本的哪个函数
20
20
  */
@@ -31,4 +31,38 @@
31
31
  * 显示, 0表示不显示, 1表示显示
32
32
  */
33
33
  "show": 0
34
- }
34
+ },
35
+ {
36
+ /**
37
+ * 名称, 由中英文和下“_”组成, 用于卸载接口 例如: demo
38
+ */
39
+ "name": "demo3",
40
+ /**
41
+ * 标题, 介绍作用
42
+ */
43
+ "title": "示例脚本3",
44
+ /**
45
+ * 描述, 用于描述该有什么用的
46
+ */
47
+ "description": "用于测试动态加载、更新、卸载、删除脚本321",
48
+ /**
49
+ * 文件路径, 当调用函数不存在时,会先从文件中加载
50
+ */
51
+ "func_file": "./after.js",
52
+ /**
53
+ * 回调函数名 用于决定调用脚本的哪个函数
54
+ */
55
+ "func_name": "main",
56
+ /**
57
+ * 排序
58
+ */
59
+ "sort": 10,
60
+ /**
61
+ * 状态, 0表示未启用, 1表示启用
62
+ */
63
+ "state": 1,
64
+ /**
65
+ * 显示, 0表示不显示, 1表示显示
66
+ */
67
+ "show": 0
68
+ }]
@@ -4,6 +4,7 @@ class Demo {
4
4
  var i = 0;
5
5
  Demo.prototype.main = function() {
6
6
  console.log("我很好123", i++);
7
+ return i;
7
8
  }
8
9
  Demo.prototype.init = function(){
9
10
  console.log("初始化test2");
@@ -5,6 +5,7 @@ var i = 0;
5
5
  Demo.prototype.main = function() {
6
6
  console.log("我很好123", i++);
7
7
  }
8
+
8
9
  Demo.prototype.init = function(){
9
10
  console.log("初始化test2");
10
11
  }
@@ -0,0 +1,13 @@
1
+ class Demo {
2
+ constructor() {}
3
+ }
4
+ var i = 0;
5
+ Demo.prototype.main = function() {
6
+ console.log("我很好123", i++);
7
+ return i;
8
+ }
9
+ Demo.prototype.init = function(){
10
+ console.log("初始化test2");
11
+ }
12
+
13
+ module.exports = new Demo();
@@ -1,4 +1,4 @@
1
- {
1
+ [{
2
2
  /**
3
3
  * 名称, 由中英文和下“_”组成, 用于卸载接口 例如: demo
4
4
  */
@@ -14,7 +14,7 @@
14
14
  /**
15
15
  * 文件路径, 当调用函数不存在时,会先从文件中加载
16
16
  */
17
- "func_file": "./index.js",
17
+ "func_file": "./main.js",
18
18
  /**
19
19
  * 回调函数名 用于决定调用脚本的哪个函数
20
20
  */
@@ -31,4 +31,38 @@
31
31
  * 显示, 0表示不显示, 1表示显示
32
32
  */
33
33
  "show": 0
34
- }
34
+ },
35
+ {
36
+ /**
37
+ * 名称, 由中英文和下“_”组成, 用于卸载接口 例如: demo
38
+ */
39
+ "name": "demo3",
40
+ /**
41
+ * 标题, 介绍作用
42
+ */
43
+ "title": "示例脚本3",
44
+ /**
45
+ * 描述, 用于描述该有什么用的
46
+ */
47
+ "description": "用于测试动态加载、更新、卸载、删除脚本",
48
+ /**
49
+ * 文件路径, 当调用函数不存在时,会先从文件中加载
50
+ */
51
+ "func_file": "./after.js",
52
+ /**
53
+ * 回调函数名 用于决定调用脚本的哪个函数
54
+ */
55
+ "func_name": "main",
56
+ /**
57
+ * 排序
58
+ */
59
+ "sort": 10,
60
+ /**
61
+ * 状态, 0表示未启用, 1表示启用
62
+ */
63
+ "state": 1,
64
+ /**
65
+ * 显示, 0表示不显示, 1表示显示
66
+ */
67
+ "show": 0
68
+ }]
@@ -0,0 +1,13 @@
1
+ class Demo {
2
+ constructor() {}
3
+ }
4
+ var i = 0;
5
+ Demo.prototype.main = function() {
6
+ console.log("我很好123", i++);
7
+ }
8
+
9
+ Demo.prototype.init = function(){
10
+ console.log("初始化test2");
11
+ }
12
+
13
+ module.exports = new Demo();
package/index.js CHANGED
@@ -83,6 +83,15 @@ class Item {
83
83
  * 模块目录
84
84
  */
85
85
  this.dir_base = dir_base;
86
+
87
+ /**
88
+ * 模式
89
+ * 1.生产模式,改变文件不会重新加载
90
+ * 2.热更新模式,改变文件重新加载
91
+ * 3.重载模式,执行完后重新加载,避免变量污染
92
+ * 4.热更新+重载模式,改变文件重新加载,执行完后重新加载
93
+ */
94
+ this.mode = 1;
86
95
  }
87
96
  }
88
97
 
@@ -99,7 +108,7 @@ Item.prototype.set_config = function(config) {
99
108
  * 设置配置后
100
109
  */
101
110
  Item.prototype.set_config_after = function() {
102
-
111
+
103
112
  };
104
113
 
105
114
  /**
@@ -140,7 +149,6 @@ Item.prototype.unloadObj = function(file) {
140
149
  this.remove_module(file || this.config.func_file.fullname(this.dir));
141
150
  }
142
151
 
143
-
144
152
  /**
145
153
  * 卸载
146
154
  * @param {Boolean} remove 是否删除文件
@@ -166,9 +174,14 @@ Item.prototype.loadObj = function(config) {
166
174
  var f = config.func_file;
167
175
  if (f) {
168
176
  var file = f.fullname(this.dir);
169
- if (file.hasFile()) {
170
- var name = config.func_name;
171
- var cs = $.require(file, (cs, way) => {
177
+ if (!file.hasFile()) {
178
+ this.new_script(file);
179
+ }
180
+
181
+ var name = config.func_name;
182
+ var cs;
183
+ if (this.mode === 2 || this.mode === 4) {
184
+ cs = $.require(file, (cs, way) => {
172
185
  if (way == "change" && cs) {
173
186
  if (name) {
174
187
  this.main = cs[name];
@@ -177,15 +190,15 @@ Item.prototype.loadObj = function(config) {
177
190
  }
178
191
  }
179
192
  });
180
- if (cs) {
181
- if (name) {
182
- this.main = cs[name];
183
- } else {
184
- $.push(this, cs, true);
185
- }
186
- }
187
193
  } else {
188
- this.new_script(file);
194
+ cs = require(file);
195
+ }
196
+ if (cs) {
197
+ if (name) {
198
+ this.main = cs[name];
199
+ } else {
200
+ $.push(this, cs, true);
201
+ }
189
202
  }
190
203
  }
191
204
  this.load_after();
@@ -203,25 +216,54 @@ Item.prototype.new_config = function(file) {
203
216
  /**
204
217
  * 加载配置文件
205
218
  * @param {String} file 文件路径
219
+ * @param {String} name 配置项名称
206
220
  * @return {Object} 配置对象
207
221
  */
208
- Item.prototype.loadFile = function(file) {
209
- var obj;
222
+ Item.prototype.loadFile = function(file, name) {
223
+ var config;
210
224
  var f = file.fullname(this.dir);
211
225
  var text = f.loadText();
212
226
  if (!text) {
213
227
  this.new_config(f);
214
- text = f.loadText();
215
228
  }
216
229
  if (text) {
217
- obj = $.loadJson(f, (mod) => {
218
- if (mod) {
219
- this.set_config(mod);
230
+ if (this.mode === 2 || this.mode === 4) {
231
+ config = $.loadJson(f, (conf, way) => {
232
+ if (way == "change" && conf) {
233
+ if (Array.isArray(conf)) {
234
+ var list = conf;
235
+ for (var i = 0; i < list.length; i++) {
236
+ var o = list[i];
237
+ if (this.config.name === o.name) {
238
+ this.set_config(o);
239
+ break;
240
+ }
241
+ }
242
+ } else {
243
+ this.set_config(conf);
244
+ }
245
+ }
246
+ });
247
+ } else {
248
+ config = f.loadJson();
249
+ }
250
+
251
+ if (name) {
252
+ if (Array.isArray(config)) {
253
+ var list = config;
254
+ for (var i = 0; i < list.length; i++) {
255
+ var o = list[i];
256
+ if (name === o.name) {
257
+ config = o;
258
+ break;
259
+ }
260
+ }
220
261
  }
221
- });
262
+ }
222
263
  }
264
+ this.dir = f.dirname();
223
265
  this.filename = f;
224
- return obj;
266
+ return config;
225
267
  };
226
268
 
227
269
  /**
@@ -229,7 +271,7 @@ Item.prototype.loadFile = function(file) {
229
271
  */
230
272
  Item.prototype.reload = function() {
231
273
  this.unloadObj();
232
- this.load(this.filename);
274
+ this.load(this.filename, this.config.name);
233
275
  }
234
276
 
235
277
  /**
@@ -284,11 +326,12 @@ Item.prototype.remove_file = function() {
284
326
  /**
285
327
  * 载入配置
286
328
  * @param {Object|String} cg 配置对象或配置路径
329
+ * @param {String} name 配置名称
287
330
  */
288
- Item.prototype.load = function(cg) {
331
+ Item.prototype.load = function(cg, name) {
289
332
  var config;
290
333
  if (typeof(cg) === "string") {
291
- config = this.loadFile(cg);
334
+ config = this.loadFile(cg, name);
292
335
  } else {
293
336
  config = cg;
294
337
  }
@@ -426,9 +469,13 @@ class Index {
426
469
  this.dir_base = dir_base;
427
470
 
428
471
  /**
429
- * 模式 0生产模式,1开发模式,2热重载模式
472
+ * 模式
473
+ * 1.生产模式,改变文件不会重新加载
474
+ * 2.热更新模式,改变文件重新加载
475
+ * 3.重载模式,执行完后重新加载,避免变量污染
476
+ * 4.热更新+重载模式,改变文件重新加载,执行完后重新加载
430
477
  */
431
- this.mode = 2;
478
+ this.mode = 1;
432
479
  }
433
480
  }
434
481
 
@@ -451,10 +498,35 @@ Index.prototype.Drive = Item;
451
498
  * @param {String} file 配置文件
452
499
  */
453
500
  Index.prototype.load_item = function(dir, cg, file) {
454
- var drive = new this.Drive(dir);
455
- drive.filename = file;
456
- drive.loadObj(cg);
457
- this.list.push(drive);
501
+ if (this.Drive) {
502
+ var drive = new this.Drive(dir);
503
+ if (cg) {
504
+ if (this.mode < 3) {
505
+ drive.mode = this.mode;
506
+ } else {
507
+ drive.mode = 2;
508
+ }
509
+ drive.load(file, cg.name);
510
+ } else {
511
+ drive.mode = this.mode;
512
+ drive.load(file);
513
+ }
514
+ this.list.push(drive);
515
+ return drive;
516
+ } else {
517
+ var json = file.loadJson();
518
+ if (!json) {
519
+ var fl = this.dir_base + "/config.tpl.json";
520
+ if (fl.hasFile()) {
521
+ fl.copyFile(file);
522
+ json = file.loadJson();
523
+ }
524
+ }
525
+ if (json) {
526
+ this.list.push(json);
527
+ }
528
+ return json;
529
+ }
458
530
  };
459
531
 
460
532
  /**
@@ -462,44 +534,9 @@ Index.prototype.load_item = function(dir, cg, file) {
462
534
  * @param {Array} list 文件列表
463
535
  */
464
536
  Index.prototype.load_list = function(list) {
465
- var _this = this;
466
537
  // 遍历文件路径
467
- list.map(function(file) {
468
- var dir = file.dirname();
469
- // 载入文件
470
- var obj;
471
- if (_this.mode) {
472
- obj = $.loadJson(file, function(config) {
473
- if (config.name) {
474
- var plug = _this.get(config.name);
475
- if (plug) {
476
- plug.set_config(config);
477
- }
478
- }
479
- });
480
- } else {
481
- obj = file.loadJson(dir)
482
- }
483
- if (obj) {
484
- if (obj.constructor == Array) {
485
- obj.map(function(o) {
486
- // 实例化一个驱动
487
- _this.load_item(dir, o, file);
488
- });
489
- } else {
490
- _this.load_item(dir, obj, file);
491
- }
492
- } else {
493
- if (_this.Drive) {
494
- var drive = new _this.Drive();
495
- drive.new_config(file);
496
- } else {
497
- var fl = _this.dir_base + "/config.tpl.json";
498
- if (fl.hasFile()) {
499
- fl.copyFile(file);
500
- }
501
- }
502
- }
538
+ list.map((file) => {
539
+ this.load_file(file, true);
503
540
  });
504
541
  };
505
542
 
@@ -526,8 +563,7 @@ Index.prototype.load = function(path, accurate) {
526
563
  search_dir = this.type;
527
564
  }
528
565
  list_scope = $.dir.getAll(path, search_dir);
529
- }
530
- else {
566
+ } else {
531
567
  list_scope = $.dir.getAll(path);
532
568
  }
533
569
 
@@ -566,22 +602,60 @@ Index.prototype.update_after = async function(dir) {
566
602
  // console.log("更新后")
567
603
  }
568
604
 
605
+ /**
606
+ * 更新配置
607
+ * @param {Object} dir
608
+ */
609
+ Index.prototype.update_config = async function(dir) {
610
+ if (!dir) {
611
+ var list = this.list;
612
+ for (var i = 0; i < list.length; i++) {
613
+ var o = list[i];
614
+ var file = o.filename;
615
+ if (file) {
616
+ var config = file.loadJson();
617
+ if (config) {
618
+ if (Array.isArray(config)) {
619
+ var lt = config;
620
+ for (var n = 0; n < lt.length; n++) {
621
+ var cg = lt[n];
622
+ if (cg.name == o.config.name) {
623
+ o.set_config(cg);
624
+ continue;
625
+ }
626
+ }
627
+ } else {
628
+ o.set_config(config);
629
+ }
630
+ }
631
+ }
632
+ }
633
+ }
634
+ }
635
+
569
636
  /**
570
637
  * 更新
638
+ * @param {String} dir 检索的路径
639
+ * @param {Boolean} loadJS 是否加载JS
571
640
  */
572
- Index.prototype.update_main = async function(dir) {
573
- this.clear();
574
- this.load(dir);
641
+ Index.prototype.update_main = async function(dir, loadJS = true) {
642
+ if (loadJS) {
643
+ this.clear();
644
+ this.load(dir);
645
+ } else {
646
+ this.update_config(dir);
647
+ }
575
648
  this.sort();
576
649
  }
577
650
 
578
651
  /**
579
652
  * 更新配置
580
653
  * @param {String} dir 检索的路径
654
+ * @param {Boolean} loadJS 是否加载JS
581
655
  */
582
- Index.prototype.update = async function(dir) {
656
+ Index.prototype.update = async function(dir, loadJS = true) {
583
657
  await this.update_before(dir);
584
- await this.update_main(dir);
658
+ await this.update_main(dir, loadJS);
585
659
  await this.update_after(dir);
586
660
  };
587
661
 
@@ -742,28 +816,25 @@ Index.prototype.unload = function(name, remove) {
742
816
  /**
743
817
  * 通过文件加载配置
744
818
  * @param {String} file 文件名
819
+ * @param {Boolean} create 不存在则进行创建
745
820
  * @return {String} 加载失败返回错误提示,加载成功返回null
746
821
  */
747
- Index.prototype.load_file = function(file) {
748
- if (file.hasFile()) {
749
- var obj = file.loadJson();
750
- if (obj) {
751
- var dir = file.dirname();
752
- var _this = this;
753
- if (obj.constructor == Array) {
754
- obj.map(function(o) {
755
- if (!_this.get(o.name)) {
756
- _this.load_item(dir, o, file);
757
- }
758
- });
759
- } else {
760
- if (!_this.get(obj.name)) {
761
- _this.load_item(dir, obj, file);
762
- }
763
- }
822
+ Index.prototype.load_file = function(file, create = false) {
823
+ var _this = this;
824
+ var dir = file.dirname();
825
+ // 载入文件
826
+ var obj = file.loadJson();
827
+ if (obj) {
828
+ if (obj.constructor == Array) {
829
+ obj.map(function(o) {
830
+ // 实例化一个驱动
831
+ _this.load_item(dir, o, file);
832
+ });
764
833
  } else {
765
- return "配置文件格式错误";
834
+ return _this.load_item(dir, null, file);
766
835
  }
836
+ } else if (create) {
837
+ return _this.load_item(dir, null, file);
767
838
  } else {
768
839
  return file + "文件不存在";
769
840
  }
@@ -802,7 +873,7 @@ Index.prototype.run = async function(name, method, ...params) {
802
873
  } else {
803
874
  result = ret;
804
875
  }
805
- if (this.mode) {
876
+ if (this.mode > 2) {
806
877
  o.reload();
807
878
  }
808
879
  }
@@ -841,7 +912,7 @@ Index.prototype.exec = async function(name, method, ...params) {
841
912
  } else {
842
913
  result = ret;
843
914
  }
844
- if (this.mode) {
915
+ if (this.mode > 2) {
845
916
  o.reload();
846
917
  }
847
918
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "1.8.7",
3
+ "version": "1.8.9",
4
4
  "description": "这是超级美眉框架机制构建辅助模块,用于快速构建一个机制",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test.js CHANGED
@@ -13,8 +13,14 @@ class Drive extends Item {
13
13
  class Engine extends Index {
14
14
  constructor(scope, dir_base) {
15
15
  super(scope, dir_base);
16
- // 模式 0生产模式;1开发模式,会重新加载脚本
17
- this.mode = 2;
16
+ /**
17
+ * 模式
18
+ * 1.不更新重载模式,改变文件不会重新加载,执行完后也不会重新加载
19
+ * 2.热更新模式,改变文件重新加载
20
+ * 3.重载模式,执行完后重新加载,避免变量污染
21
+ * 4.热更新+重载模式,改变文件重新加载,执行完后重新加载
22
+ */
23
+ this.mode = 0;
18
24
  // 机制类型,必须填写,用于检索文件
19
25
  this.type = "demo";
20
26
  }
@@ -35,6 +41,10 @@ async function demo() {
35
41
  if (plug) {
36
42
  plug.loadFile(plug.filename);
37
43
  }
44
+
45
+ var i = await engine.run('demo3', 'main');
46
+ console.log("指明回调demo3", i);
47
+
38
48
  // console.log("模块数", engine.list.length);
39
49
 
40
50
  // var i = 0;
@@ -82,10 +92,19 @@ async function demo() {
82
92
  // file.saveText(text);
83
93
  // }, 26000)
84
94
 
85
- setInterval(() => {
95
+ // setInterval(() => {
96
+ // var plug = engine.get("demo1");
97
+ // console.log("查看变化1", plug.config);
98
+ // var plug = engine.get("demo3");
99
+ // console.log("查看变化3", plug.config);
100
+ // }, 3000);
101
+ setTimeout(async() => {
102
+ await engine.update(null, false);
86
103
  var plug = engine.get("demo1");
87
- console.log("查看变化", plug);
88
- }, 3000);
104
+ console.log("查看变化1", plug.config);
105
+ var plug = engine.get("demo3");
106
+ console.log("查看变化3", plug.config);
107
+ }, 10000);
89
108
  }
90
109
 
91
110
  /* 调用示例 */