mm_machine 1.6.1 → 1.6.3

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,34 @@
1
+ {
2
+ /**
3
+ * 名称, 由中英文和下“_”组成, 用于卸载接口 例如: demo
4
+ */
5
+ "name": "demo2",
6
+ /**
7
+ * 标题, 介绍作用
8
+ */
9
+ "title": "示例脚本2",
10
+ /**
11
+ * 描述, 用于描述该有什么用的
12
+ */
13
+ "description": "用于测试动态加载、更新、卸载、删除脚本",
14
+ /**
15
+ * 文件路径, 当调用函数不存在时,会先从文件中加载
16
+ */
17
+ "func_file": "./index.js",
18
+ /**
19
+ * 回调函数名 用于决定调用脚本的哪个函数
20
+ */
21
+ "func_name": "",
22
+ /**
23
+ * 排序
24
+ */
25
+ "sort": 10,
26
+ /**
27
+ * 状态, 0表示未启用, 1表示启用
28
+ */
29
+ "state": 1,
30
+ /**
31
+ * 显示, 0表示不显示, 1表示显示
32
+ */
33
+ "show": 0
34
+ }
@@ -0,0 +1,9 @@
1
+ class Demo {
2
+ constructor() {}
3
+ }
4
+ var i = 0;
5
+ Demo.prototype.main = function() {
6
+ console.log("我很好7654321", i++);
7
+ }
8
+
9
+ module.exports = new Demo();
@@ -0,0 +1,34 @@
1
+ {
2
+ /**
3
+ * 名称, 由中英文和下“_”组成, 用于卸载接口 例如: demo
4
+ */
5
+ "name": "demo1",
6
+ /**
7
+ * 标题, 介绍作用
8
+ */
9
+ "title": "示例脚本1",
10
+ /**
11
+ * 描述, 用于描述该有什么用的
12
+ */
13
+ "description": "用于测试动态加载、更新、卸载、删除脚本",
14
+ /**
15
+ * 文件路径, 当调用函数不存在时,会先从文件中加载
16
+ */
17
+ "func_file": "./index.js",
18
+ /**
19
+ * 回调函数名 用于决定调用脚本的哪个函数
20
+ */
21
+ "func_name": "",
22
+ /**
23
+ * 排序
24
+ */
25
+ "sort": 10,
26
+ /**
27
+ * 状态, 0表示未启用, 1表示启用
28
+ */
29
+ "state": 1,
30
+ /**
31
+ * 显示, 0表示不显示, 1表示显示
32
+ */
33
+ "show": 0
34
+ }
@@ -0,0 +1,11 @@
1
+
2
+ var i = 0;
3
+ function test() {
4
+ console.log("你好123", i++)
5
+ }
6
+
7
+ function main() {
8
+ test();
9
+ }
10
+
11
+ exports.main = main;
@@ -0,0 +1,34 @@
1
+ {
2
+ /**
3
+ * 名称, 由中英文和下“_”组成, 用于卸载接口 例如: demo
4
+ */
5
+ "name": "demo2",
6
+ /**
7
+ * 标题, 介绍作用
8
+ */
9
+ "title": "示例脚本2",
10
+ /**
11
+ * 描述, 用于描述该有什么用的
12
+ */
13
+ "description": "用于测试动态加载、更新、卸载、删除脚本",
14
+ /**
15
+ * 文件路径, 当调用函数不存在时,会先从文件中加载
16
+ */
17
+ "func_file": "./index.js",
18
+ /**
19
+ * 回调函数名 用于决定调用脚本的哪个函数
20
+ */
21
+ "func_name": "",
22
+ /**
23
+ * 排序
24
+ */
25
+ "sort": 10,
26
+ /**
27
+ * 状态, 0表示未启用, 1表示启用
28
+ */
29
+ "state": 1,
30
+ /**
31
+ * 显示, 0表示不显示, 1表示显示
32
+ */
33
+ "show": 0
34
+ }
@@ -0,0 +1,9 @@
1
+ class Demo {
2
+ constructor() {}
3
+ }
4
+ var i = 0;
5
+ Demo.prototype.main = function() {
6
+ console.log("我很好123", i++);
7
+ }
8
+
9
+ module.exports = new Demo();
package/index.js CHANGED
@@ -4,28 +4,34 @@
4
4
  * @version 1.5
5
5
  */
6
6
  var conf = require('mm_config');
7
+ const ph = require('path');
7
8
  const util = require('util');
8
- const fs = require('fs');
9
+ const Mod = require('mm_hot_reload');
10
+
11
+ $.mod = new Mod();
12
+ $.require = function(file, func) {
13
+ return $.mod.load(file, func);
14
+ }
9
15
 
10
16
  /**
11
- * @description 驱动基础类
17
+ * 驱动基础类
12
18
  * @class
13
19
  */
14
20
  class Item {
15
21
  /**
16
- * @description 构造函数
22
+ * 构造函数
17
23
  * @param {String} dir 当前目录
18
24
  * @param {String} dir_base 模块目录
19
25
  * @constructor
20
26
  */
21
27
  constructor(dir, dir_base) {
22
28
  /**
23
- * @description 当前路径
29
+ * 当前路径
24
30
  */
25
31
  this.dir = dir;
26
32
 
27
33
  /**
28
- * @description 默认文件
34
+ * 默认文件
29
35
  */
30
36
  this.default_file = "./sys.json";
31
37
 
@@ -86,14 +92,14 @@ Item.prototype.set_config = function(config) {
86
92
  }
87
93
 
88
94
  /**
89
- * @description 加载完成时
95
+ * 加载完成时
90
96
  */
91
97
  Item.prototype.load_after = function() {
92
98
 
93
99
  };
94
100
 
95
101
  /**
96
- * @description 新建脚本
102
+ * 新建脚本
97
103
  * @param {String} file
98
104
  */
99
105
  Item.prototype.new_script = function(file) {
@@ -104,25 +110,39 @@ Item.prototype.new_script = function(file) {
104
110
  };
105
111
 
106
112
  /**
107
- * @description 移除模块
113
+ * 移除模块
108
114
  * @param {Object} m
109
115
  */
110
116
  Item.prototype.remove_module = function(m) {
111
- var path = require.resolve(m);
112
- delete require.cache[path];
117
+ // 移除模块和监听
118
+ $.mod.unload(m);
119
+ // var path = require.resolve(m);
120
+ // delete require.cache[path];
113
121
  // require.cache[path] = null;
114
122
  };
115
123
 
116
124
  /**
117
- * @description 卸载对象
125
+ * 卸载对象
118
126
  * @param {String} file 文件
119
127
  */
120
128
  Item.prototype.unloadObj = function(file) {
121
- this.remove_module(file || this.filename);
129
+ this.remove_module(file || this.config.func_file.fullname(this.dir));
122
130
  }
123
131
 
132
+
124
133
  /**
125
- * @description 加载配置对象
134
+ * 卸载
135
+ * @param {Boolean} remove 是否删除文件
136
+ */
137
+ Item.prototype.unload = function(remove) {
138
+ this.unloadObj();
139
+ if (remove) {
140
+ this.remove_file();
141
+ }
142
+ }
143
+
144
+ /**
145
+ * 加载配置对象
126
146
  * @param {Object} obj 配置对象
127
147
  */
128
148
  Item.prototype.loadObj = function(config) {
@@ -139,10 +159,17 @@ Item.prototype.loadObj = function(config) {
139
159
  if (!config.state) {
140
160
  return;
141
161
  }
142
- this.remove_module(file);
143
- var cs = require(file);
162
+ var name = config.func_name;
163
+ var cs = $.require(file, (cs, way) => {
164
+ if (way == "change" && cs) {
165
+ if (name) {
166
+ this.main = cs[name];
167
+ } else {
168
+ $.push(this, cs, true);
169
+ }
170
+ }
171
+ });
144
172
  if (cs) {
145
- var name = config.func_name;
146
173
  if (name) {
147
174
  this.main = cs[name];
148
175
  } else {
@@ -157,7 +184,7 @@ Item.prototype.loadObj = function(config) {
157
184
  };
158
185
 
159
186
  /**
160
- * @description 新建配置
187
+ * 新建配置
161
188
  * @param {String} file
162
189
  */
163
190
  Item.prototype.new_config = function(file) {
@@ -166,7 +193,7 @@ Item.prototype.new_config = function(file) {
166
193
  };
167
194
 
168
195
  /**
169
- * @description 加载配置文件
196
+ * 加载配置文件
170
197
  * @param {String} file 文件路径
171
198
  * @return {Object} 配置对象
172
199
  */
@@ -188,7 +215,15 @@ Item.prototype.loadFile = function(file) {
188
215
  };
189
216
 
190
217
  /**
191
- * @description 删除脚本
218
+ * 重载配置和脚本
219
+ */
220
+ Item.prototype.reload = function() {
221
+ this.unloadObj();
222
+ this.load(this.filename);
223
+ }
224
+
225
+ /**
226
+ * 删除脚本
192
227
  */
193
228
  Item.prototype.del_script = function() {
194
229
  var f = this.config.func_file;
@@ -198,10 +233,10 @@ Item.prototype.del_script = function() {
198
233
  };
199
234
 
200
235
  /**
201
- * @description 删除配置和脚本文件
236
+ * 删除配置和脚本文件
202
237
  * @param {Object} item 项目
203
238
  */
204
- Item.prototype.removeFile = function() {
239
+ Item.prototype.remove_file = function() {
205
240
  var name = this.config.name;
206
241
  var file = this.filename;
207
242
 
@@ -226,11 +261,9 @@ Item.prototype.removeFile = function() {
226
261
  }
227
262
  } else {
228
263
  this.del_script();
229
- file.delFile();
230
264
  }
231
265
  } else {
232
266
  this.del_script();
233
- file.delFile();
234
267
  }
235
268
  } else {
236
269
  msg = "配置文件不存在";
@@ -239,7 +272,7 @@ Item.prototype.removeFile = function() {
239
272
  };
240
273
 
241
274
  /**
242
- * @description 载入配置
275
+ * 载入配置
243
276
  * @param {Object|String} cg 配置对象或配置路径
244
277
  */
245
278
  Item.prototype.load = function(cg) {
@@ -253,7 +286,7 @@ Item.prototype.load = function(cg) {
253
286
  };
254
287
 
255
288
  /**
256
- * @description 保存配置
289
+ * 保存配置
257
290
  */
258
291
  Item.prototype.save = function() {
259
292
  var f = this.filename.fullname(this.dir);
@@ -277,7 +310,7 @@ Item.prototype.save = function() {
277
310
  };
278
311
 
279
312
  /**
280
- * @description 主要执行函数
313
+ * 主要执行函数
281
314
  * @param {Object} param1 参数一
282
315
  * @param {Object} param2 参数二
283
316
  */
@@ -285,7 +318,6 @@ Item.prototype.main = function(param1, param2) {
285
318
  return null;
286
319
  };
287
320
 
288
-
289
321
  /**
290
322
  * 调用函数
291
323
  * @param {String} method 函数名
@@ -327,7 +359,7 @@ exports.Item = Item;
327
359
  */
328
360
  class Index {
329
361
  /**
330
- * @description 构造函数
362
+ * 构造函数
331
363
  * @param {Object} scope 作用域
332
364
  * @param {String} dir_base 模块目录
333
365
  * @constructor
@@ -359,25 +391,26 @@ class Index {
359
391
  this.dir_base = dir_base;
360
392
 
361
393
  /**
362
- * 模式 0生产模式,1开发模式,
394
+ * 模式 0生产模式,1开发模式,2热重载模式
363
395
  */
364
396
  this.mode = 0;
365
397
  }
366
398
  }
367
399
 
368
-
369
- /// 清除接口缓存
400
+ /**
401
+ * 清除接口缓存
402
+ */
370
403
  Index.prototype.clear = function() {
371
404
  this.list = [];
372
405
  };
373
406
 
374
407
  /**
375
- * @description 默认驱动
408
+ * 默认驱动
376
409
  */
377
410
  Index.prototype.Drive = Item;
378
411
 
379
412
  /**
380
- * @description 加载项
413
+ * 加载项
381
414
  * @param {String} dir 文件路径
382
415
  * @param {Object} cg 配置参数
383
416
  * @param {String} file 配置文件
@@ -390,7 +423,7 @@ Index.prototype.load_item = function(dir, cg, file) {
390
423
  };
391
424
 
392
425
  /**
393
- * @description 加载列表
426
+ * 加载列表
394
427
  * @param {Array} list 文件列表
395
428
  */
396
429
  Index.prototype.load_list = function(list) {
@@ -424,15 +457,18 @@ Index.prototype.load_list = function(list) {
424
457
  };
425
458
 
426
459
  /**
427
- * @description 加载配置
460
+ * 加载配置
428
461
  * @param {String} path 检索路径
429
462
  */
430
463
  Index.prototype.load = function(path) {
431
464
  if (path) {
432
- path = ('/app/' + path).fullname();
465
+ if (!ph.isAbsolute(path)) {
466
+ path = ph.join('app', path);
467
+ }
433
468
  } else {
434
469
  path = '/app/';
435
470
  }
471
+
436
472
  // 获取所有应用路径
437
473
  var search_dir;
438
474
  if (this.scope && this.scope !== $.val.scope) {
@@ -443,16 +479,15 @@ Index.prototype.load = function(path) {
443
479
  var list_scope = $.dir.getAll(path, search_dir);
444
480
 
445
481
  // 遍历目录路径
446
- var _this = this;
447
- list_scope.map(function(f) {
482
+ list_scope.map((f) => {
448
483
  // 获取所有配置文件
449
- var list_file = $.file.getAll(f, "*" + _this.type + ".json");
450
- _this.load_list(list_file);
484
+ var list_file = $.file.getAll(f, "*" + this.type + ".json");
485
+ this.load_list(list_file);
451
486
  });
452
487
  };
453
488
 
454
489
  /**
455
- * @description 排序
490
+ * 排序
456
491
  */
457
492
  Index.prototype.sort = function() {
458
493
  var _this = this;
@@ -464,7 +499,7 @@ Index.prototype.sort = function() {
464
499
  };
465
500
 
466
501
  /**
467
- * @description 更新配置
502
+ * 更新配置
468
503
  * @param {String} dir 检索的路径
469
504
  */
470
505
  Index.prototype.update = async function(dir) {
@@ -474,7 +509,7 @@ Index.prototype.update = async function(dir) {
474
509
  };
475
510
 
476
511
  /**
477
- * @description 查询配置项
512
+ * 查询配置项
478
513
  * @param {String} name 名称
479
514
  * @return {Object} 返回单项配置
480
515
  */
@@ -507,7 +542,7 @@ Index.prototype.cmd = async function(name, state, ...params) {
507
542
  }
508
543
 
509
544
  /**
510
- * @description 查询配置项
545
+ * 查询配置项
511
546
  * @param {String} name 名称
512
547
  * @return {Object} 返回单项配置
513
548
  */
@@ -527,7 +562,7 @@ Index.prototype.set = function(cg) {
527
562
  };
528
563
 
529
564
  /**
530
- * @description 保存配置
565
+ * 保存配置
531
566
  * @param {String} name 保存的配置
532
567
  */
533
568
  Index.prototype.save = function(name) {
@@ -541,7 +576,7 @@ Index.prototype.save = function(name) {
541
576
  };
542
577
 
543
578
  /**
544
- * @description 添加
579
+ * 添加
545
580
  * @param {Object} obj 配置参数
546
581
  * @return {String} 失败返回错误提示,成功返回null
547
582
  */
@@ -590,32 +625,45 @@ Index.prototype.add = function(obj) {
590
625
  };
591
626
 
592
627
  /**
593
- * @description 删除
628
+ * 删除
594
629
  * @param {String} name 保存的配置名
595
630
  * @param {Boolean} remove 是否删除配置文件
596
631
  * @return {String} 失败返回null,成功返回文件路径
597
632
  */
598
633
  Index.prototype.del = function(name, remove) {
599
634
  var lt = this.list;
600
- var file = null;
635
+ var obj = null;
601
636
  var len = lt.length;
602
637
  for (var i = 0; i < len; i++) {
603
638
  var o = lt[i];
604
639
  if (name === o.config.name) {
605
- file = o.filename;
606
- if (remove) {
607
- o.removeFile();
608
- }
640
+ obj = o;
609
641
  // 删除成员
610
642
  lt.splice(i, 1);
611
643
  break;
612
644
  }
613
645
  }
614
- return file;
646
+ return obj;
615
647
  };
616
648
 
617
649
  /**
618
- * @description 通过文件加载配置
650
+ * 卸载模块
651
+ * @param {String} name 模块名称
652
+ * @param {Boolean} remove 是否删除配置文件
653
+ * @return {String} 失败返回null,成功返回文件路径
654
+ */
655
+ Index.prototype.unload = function(name, remove) {
656
+ var o = this.del(name, remove);
657
+ var file = null;
658
+ if (o) {
659
+ o.unload(remove);
660
+ file = o.filename;
661
+ }
662
+ return file;
663
+ }
664
+
665
+ /**
666
+ * 通过文件加载配置
619
667
  * @param {String} file 文件名
620
668
  * @return {String} 加载失败返回错误提示,加载成功返回null
621
669
  */
@@ -646,14 +694,14 @@ Index.prototype.load_file = function(file) {
646
694
  };
647
695
 
648
696
  /**
649
- * @description 重载脚本和配置
697
+ * 重载脚本和配置
650
698
  * @param {String} file 文件名
651
699
  * @return {String} 重载失败返回错误提示,重载成功返回null
652
700
  */
653
701
  Index.prototype.reload = function(name) {
654
702
  var o = this.get(name);
655
703
  if (o) {
656
- o.load(o.filename);
704
+ o.reload();
657
705
  return null;
658
706
  }
659
707
  return '没有找到模块';
@@ -675,7 +723,7 @@ Index.prototype.run = async function(name, method, ...params) {
675
723
  ret = await ret;
676
724
  }
677
725
  if (this.mode) {
678
- o.load(o.filename);
726
+ o.reload();
679
727
  }
680
728
  }
681
729
  return ret;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "1.6.1",
3
+ "version": "1.6.3",
4
4
  "description": "这是超级美眉框架机制构建辅助模块,用于快速构建一个机制",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "node ./demo/demo.js",
8
- "dev": "nodemon ./demo/demo.js"
7
+ "test": "node ./test.js",
8
+ "dev": "nodemon ./test.js"
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
@@ -30,6 +30,7 @@
30
30
  },
31
31
  "homepage": "https://github.com/qiuwenwu/mm_machine#readme",
32
32
  "dependencies": {
33
- "mm_config": "^1.1.1"
33
+ "mm_config": "^1.1.1",
34
+ "mm_hot_reload": "^1.0.0"
34
35
  }
35
36
  }
package/test.js ADDED
@@ -0,0 +1,86 @@
1
+ var {
2
+ Item,
3
+ Index
4
+ } = require('./index.js');
5
+
6
+ class Drive extends Item {
7
+ constructor(dir, dir_base) {
8
+ super(dir, dir_base);
9
+ this.default_file = "./demo.json";
10
+ }
11
+ }
12
+
13
+ class Engine extends Index {
14
+ constructor(scope, dir_base) {
15
+ super(scope, dir_base);
16
+ // 模式 0生产模式;1开发模式,会重新加载脚本
17
+ this.mode = 0;
18
+ // 机制类型,必须填写,用于检索文件
19
+ this.type = "demo";
20
+ }
21
+ }
22
+
23
+ Engine.prototype.Drive = Drive;
24
+
25
+ async function demo() {
26
+ var engine = new Engine();
27
+ console.info("→ 加载mod");
28
+ await engine.update("./".fullname(__dirname));
29
+ console.log("模块数", engine.list.length);
30
+
31
+ var i = 0;
32
+ var timer = setInterval(async () => {
33
+ await engine.run('demo1', 'main');
34
+ await engine.run('demo2', 'main');
35
+ i++;
36
+ if (i == 5) {
37
+ console.info("→ 重载mod");
38
+ engine.reload("demo1");
39
+ } else if (i == 8) {
40
+ console.info("→ 卸载mod");
41
+ engine.unload("demo2");
42
+ } else if (i == 13) {
43
+ console.info("→ 卸载并删除mod");
44
+ engine.unload("demo1", true);
45
+ } else if (i == 15) {
46
+ clearTimeout(timer);
47
+ process.exit(0);
48
+ }
49
+ }, 3000);
50
+
51
+ setTimeout(() => {
52
+ var file = "./demo/test1/index.js";
53
+ var text = file.loadText();
54
+ text = text.replace("123", "1234567");
55
+ file.saveText(text);
56
+ }, 7000)
57
+
58
+ setTimeout(() => {
59
+ var file = "./demo/test2/index.js";
60
+ var text = file.loadText();
61
+ text = text.replace("123", "7654321");
62
+ file.saveText(text);
63
+ }, 20000)
64
+
65
+ setTimeout(() => {
66
+ // 让热重载失效
67
+ console.info("→ 热重载失效");
68
+ $.mod.config.watch = false;
69
+ // 失效后再加载的模块修改文件没有变化
70
+ var file = "./demo/test1/index.js";
71
+ var text = file.loadText();
72
+ text = text.replace("1234567", "7654321");
73
+ file.saveText(text);
74
+ }, 26000)
75
+ }
76
+
77
+ /* 调用示例 */
78
+ async function test() {
79
+ './demo2'.fullname(__dirname).copyDir('./demo'.fullname(__dirname), () => {
80
+ setTimeout(() => {
81
+ demo();
82
+ }, 1000)
83
+ });
84
+ }
85
+
86
+ test();