mm_machine 1.6.0 → 1.6.2

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
 
@@ -63,7 +69,11 @@ class Item {
63
69
  /**
64
70
  * 状态, 0表示未启用, 1表示启用
65
71
  */
66
- "state": 1
72
+ "state": 1,
73
+ /**
74
+ * 显示, 0表示不显示, 1表示显示
75
+ */
76
+ "show": 0
67
77
  };
68
78
 
69
79
  /**
@@ -82,14 +92,14 @@ Item.prototype.set_config = function(config) {
82
92
  }
83
93
 
84
94
  /**
85
- * @description 加载完成时
95
+ * 加载完成时
86
96
  */
87
97
  Item.prototype.load_after = function() {
88
98
 
89
99
  };
90
100
 
91
101
  /**
92
- * @description 新建脚本
102
+ * 新建脚本
93
103
  * @param {String} file
94
104
  */
95
105
  Item.prototype.new_script = function(file) {
@@ -100,25 +110,39 @@ Item.prototype.new_script = function(file) {
100
110
  };
101
111
 
102
112
  /**
103
- * @description 移除模块
104
- * @param {Object} module
113
+ * 移除模块
114
+ * @param {Object} m
105
115
  */
106
- Item.prototype.remove_module = function(module) {
107
- var path = require.resolve(module);
108
- delete require.cache[path];
116
+ Item.prototype.remove_module = function(m) {
117
+ // 移除模块和监听
118
+ $.mod.unload(m);
119
+ // var path = require.resolve(m);
120
+ // delete require.cache[path];
109
121
  // require.cache[path] = null;
110
122
  };
111
123
 
112
124
  /**
113
- * @description 卸载对象
125
+ * 卸载对象
114
126
  * @param {String} file 文件
115
127
  */
116
128
  Item.prototype.unloadObj = function(file) {
117
- this.remove_module(file || this.filename);
129
+ this.remove_module(file || this.config.func_file.fullname(this.dir));
130
+ }
131
+
132
+
133
+ /**
134
+ * 卸载
135
+ * @param {Boolean} remove 是否删除文件
136
+ */
137
+ Item.prototype.unload = function(remove) {
138
+ this.unloadObj();
139
+ if (remove) {
140
+ this.remove_file();
141
+ }
118
142
  }
119
143
 
120
144
  /**
121
- * @description 加载配置对象
145
+ * 加载配置对象
122
146
  * @param {Object} obj 配置对象
123
147
  */
124
148
  Item.prototype.loadObj = function(config) {
@@ -135,10 +159,17 @@ Item.prototype.loadObj = function(config) {
135
159
  if (!config.state) {
136
160
  return;
137
161
  }
138
- this.remove_module(file);
139
- 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
+ });
140
172
  if (cs) {
141
- var name = config.func_name;
142
173
  if (name) {
143
174
  this.main = cs[name];
144
175
  } else {
@@ -153,7 +184,7 @@ Item.prototype.loadObj = function(config) {
153
184
  };
154
185
 
155
186
  /**
156
- * @description 新建配置
187
+ * 新建配置
157
188
  * @param {String} file
158
189
  */
159
190
  Item.prototype.new_config = function(file) {
@@ -162,7 +193,7 @@ Item.prototype.new_config = function(file) {
162
193
  };
163
194
 
164
195
  /**
165
- * @description 加载配置文件
196
+ * 加载配置文件
166
197
  * @param {String} file 文件路径
167
198
  * @return {Object} 配置对象
168
199
  */
@@ -184,7 +215,15 @@ Item.prototype.loadFile = function(file) {
184
215
  };
185
216
 
186
217
  /**
187
- * @description 删除脚本
218
+ * 重载配置和脚本
219
+ */
220
+ Item.prototype.reload = function() {
221
+ this.unloadObj();
222
+ this.load(this.filename);
223
+ }
224
+
225
+ /**
226
+ * 删除脚本
188
227
  */
189
228
  Item.prototype.del_script = function() {
190
229
  var f = this.config.func_file;
@@ -194,10 +233,10 @@ Item.prototype.del_script = function() {
194
233
  };
195
234
 
196
235
  /**
197
- * @description 删除配置和脚本文件
236
+ * 删除配置和脚本文件
198
237
  * @param {Object} item 项目
199
238
  */
200
- Item.prototype.removeFile = function() {
239
+ Item.prototype.remove_file = function() {
201
240
  var name = this.config.name;
202
241
  var file = this.filename;
203
242
 
@@ -222,11 +261,9 @@ Item.prototype.removeFile = function() {
222
261
  }
223
262
  } else {
224
263
  this.del_script();
225
- file.delFile();
226
264
  }
227
265
  } else {
228
266
  this.del_script();
229
- file.delFile();
230
267
  }
231
268
  } else {
232
269
  msg = "配置文件不存在";
@@ -235,7 +272,7 @@ Item.prototype.removeFile = function() {
235
272
  };
236
273
 
237
274
  /**
238
- * @description 载入配置
275
+ * 载入配置
239
276
  * @param {Object|String} cg 配置对象或配置路径
240
277
  */
241
278
  Item.prototype.load = function(cg) {
@@ -249,7 +286,7 @@ Item.prototype.load = function(cg) {
249
286
  };
250
287
 
251
288
  /**
252
- * @description 保存配置
289
+ * 保存配置
253
290
  */
254
291
  Item.prototype.save = function() {
255
292
  var f = this.filename.fullname(this.dir);
@@ -273,7 +310,7 @@ Item.prototype.save = function() {
273
310
  };
274
311
 
275
312
  /**
276
- * @description 主要执行函数
313
+ * 主要执行函数
277
314
  * @param {Object} param1 参数一
278
315
  * @param {Object} param2 参数二
279
316
  */
@@ -281,7 +318,6 @@ Item.prototype.main = function(param1, param2) {
281
318
  return null;
282
319
  };
283
320
 
284
-
285
321
  /**
286
322
  * 调用函数
287
323
  * @param {String} method 函数名
@@ -323,7 +359,7 @@ exports.Item = Item;
323
359
  */
324
360
  class Index {
325
361
  /**
326
- * @description 构造函数
362
+ * 构造函数
327
363
  * @param {Object} scope 作用域
328
364
  * @param {String} dir_base 模块目录
329
365
  * @constructor
@@ -355,25 +391,26 @@ class Index {
355
391
  this.dir_base = dir_base;
356
392
 
357
393
  /**
358
- * 模式 0生产模式,1开发模式,
394
+ * 模式 0生产模式,1开发模式,2热重载模式
359
395
  */
360
396
  this.mode = 0;
361
397
  }
362
398
  }
363
399
 
364
-
365
- /// 清除接口缓存
400
+ /**
401
+ * 清除接口缓存
402
+ */
366
403
  Index.prototype.clear = function() {
367
404
  this.list = [];
368
405
  };
369
406
 
370
407
  /**
371
- * @description 默认驱动
408
+ * 默认驱动
372
409
  */
373
410
  Index.prototype.Drive = Item;
374
411
 
375
412
  /**
376
- * @description 加载项
413
+ * 加载项
377
414
  * @param {String} dir 文件路径
378
415
  * @param {Object} cg 配置参数
379
416
  * @param {String} file 配置文件
@@ -386,7 +423,7 @@ Index.prototype.load_item = function(dir, cg, file) {
386
423
  };
387
424
 
388
425
  /**
389
- * @description 加载列表
426
+ * 加载列表
390
427
  * @param {Array} list 文件列表
391
428
  */
392
429
  Index.prototype.load_list = function(list) {
@@ -420,15 +457,18 @@ Index.prototype.load_list = function(list) {
420
457
  };
421
458
 
422
459
  /**
423
- * @description 加载配置
460
+ * 加载配置
424
461
  * @param {String} path 检索路径
425
462
  */
426
463
  Index.prototype.load = function(path) {
427
464
  if (path) {
428
- path = ('/app/' + path).fullname();
465
+ if (!ph.isAbsolute(path)) {
466
+ path = ph.join('app', path);
467
+ }
429
468
  } else {
430
469
  path = '/app/';
431
470
  }
471
+
432
472
  // 获取所有应用路径
433
473
  var search_dir;
434
474
  if (this.scope && this.scope !== $.val.scope) {
@@ -439,16 +479,15 @@ Index.prototype.load = function(path) {
439
479
  var list_scope = $.dir.getAll(path, search_dir);
440
480
 
441
481
  // 遍历目录路径
442
- var _this = this;
443
- list_scope.map(function(f) {
482
+ list_scope.map((f) => {
444
483
  // 获取所有配置文件
445
- var list_file = $.file.getAll(f, "*" + _this.type + ".json");
446
- _this.load_list(list_file);
484
+ var list_file = $.file.getAll(f, "*" + this.type + ".json");
485
+ this.load_list(list_file);
447
486
  });
448
487
  };
449
488
 
450
489
  /**
451
- * @description 排序
490
+ * 排序
452
491
  */
453
492
  Index.prototype.sort = function() {
454
493
  var _this = this;
@@ -460,7 +499,7 @@ Index.prototype.sort = function() {
460
499
  };
461
500
 
462
501
  /**
463
- * @description 更新配置
502
+ * 更新配置
464
503
  * @param {String} dir 检索的路径
465
504
  */
466
505
  Index.prototype.update = async function(dir) {
@@ -470,7 +509,7 @@ Index.prototype.update = async function(dir) {
470
509
  };
471
510
 
472
511
  /**
473
- * @description 查询配置项
512
+ * 查询配置项
474
513
  * @param {String} name 名称
475
514
  * @return {Object} 返回单项配置
476
515
  */
@@ -503,7 +542,7 @@ Index.prototype.cmd = async function(name, state, ...params) {
503
542
  }
504
543
 
505
544
  /**
506
- * @description 查询配置项
545
+ * 查询配置项
507
546
  * @param {String} name 名称
508
547
  * @return {Object} 返回单项配置
509
548
  */
@@ -523,7 +562,7 @@ Index.prototype.set = function(cg) {
523
562
  };
524
563
 
525
564
  /**
526
- * @description 保存配置
565
+ * 保存配置
527
566
  * @param {String} name 保存的配置
528
567
  */
529
568
  Index.prototype.save = function(name) {
@@ -537,7 +576,7 @@ Index.prototype.save = function(name) {
537
576
  };
538
577
 
539
578
  /**
540
- * @description 添加
579
+ * 添加
541
580
  * @param {Object} obj 配置参数
542
581
  * @return {String} 失败返回错误提示,成功返回null
543
582
  */
@@ -586,32 +625,45 @@ Index.prototype.add = function(obj) {
586
625
  };
587
626
 
588
627
  /**
589
- * @description 删除
628
+ * 删除
590
629
  * @param {String} name 保存的配置名
591
630
  * @param {Boolean} remove 是否删除配置文件
592
631
  * @return {String} 失败返回null,成功返回文件路径
593
632
  */
594
633
  Index.prototype.del = function(name, remove) {
595
634
  var lt = this.list;
596
- var file = null;
635
+ var obj = null;
597
636
  var len = lt.length;
598
637
  for (var i = 0; i < len; i++) {
599
638
  var o = lt[i];
600
639
  if (name === o.config.name) {
601
- file = o.filename;
602
- if (remove) {
603
- o.removeFile();
604
- }
640
+ obj = o;
605
641
  // 删除成员
606
642
  lt.splice(i, 1);
607
643
  break;
608
644
  }
609
645
  }
610
- return file;
646
+ return obj;
611
647
  };
612
648
 
613
649
  /**
614
- * @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
+ * 通过文件加载配置
615
667
  * @param {String} file 文件名
616
668
  * @return {String} 加载失败返回错误提示,加载成功返回null
617
669
  */
@@ -642,14 +694,14 @@ Index.prototype.load_file = function(file) {
642
694
  };
643
695
 
644
696
  /**
645
- * @description 重载脚本和配置
697
+ * 重载脚本和配置
646
698
  * @param {String} file 文件名
647
699
  * @return {String} 重载失败返回错误提示,重载成功返回null
648
700
  */
649
701
  Index.prototype.reload = function(name) {
650
702
  var o = this.get(name);
651
703
  if (o) {
652
- o.load(o.filename);
704
+ o.reload();
653
705
  return null;
654
706
  }
655
707
  return '没有找到模块';
@@ -671,7 +723,7 @@ Index.prototype.run = async function(name, method, ...params) {
671
723
  ret = await ret;
672
724
  }
673
725
  if (this.mode) {
674
- o.load(o.filename);
726
+ o.reload();
675
727
  }
676
728
  }
677
729
  return ret;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
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.0"
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();