mm_machine 1.5.8 → 1.6.1

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 (3) hide show
  1. package/index.js +75 -37
  2. package/nodemon.json +26 -0
  3. package/package.json +4 -3
package/index.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * @version 1.5
5
5
  */
6
6
  var conf = require('mm_config');
7
-
7
+ const util = require('util');
8
8
  const fs = require('fs');
9
9
 
10
10
  /**
@@ -63,7 +63,11 @@ class Item {
63
63
  /**
64
64
  * 状态, 0表示未启用, 1表示启用
65
65
  */
66
- "state": 1
66
+ "state": 1,
67
+ /**
68
+ * 显示, 0表示不显示, 1表示显示
69
+ */
70
+ "show": 0
67
71
  };
68
72
 
69
73
  /**
@@ -77,8 +81,8 @@ class Item {
77
81
  * 设置配置
78
82
  * @param {Object} config 配置
79
83
  */
80
- Item.prototype.set_config = function(config){
81
- this.config = conf(Object.assign({}, this.config, config || {}));
84
+ Item.prototype.set_config = function(config) {
85
+ this.config = conf(Object.assign({}, this.config, config || {}), this.filename);
82
86
  }
83
87
 
84
88
  /**
@@ -101,10 +105,10 @@ Item.prototype.new_script = function(file) {
101
105
 
102
106
  /**
103
107
  * @description 移除模块
104
- * @param {Object} module
108
+ * @param {Object} m
105
109
  */
106
- Item.prototype.remove_module = function(module) {
107
- var path = require.resolve(module);
110
+ Item.prototype.remove_module = function(m) {
111
+ var path = require.resolve(m);
108
112
  delete require.cache[path];
109
113
  // require.cache[path] = null;
110
114
  };
@@ -113,7 +117,7 @@ Item.prototype.remove_module = function(module) {
113
117
  * @description 卸载对象
114
118
  * @param {String} file 文件
115
119
  */
116
- Item.prototype.unloadObj = function(file){
120
+ Item.prototype.unloadObj = function(file) {
117
121
  this.remove_module(file || this.filename);
118
122
  }
119
123
 
@@ -122,10 +126,9 @@ Item.prototype.unloadObj = function(file){
122
126
  * @param {Object} obj 配置对象
123
127
  */
124
128
  Item.prototype.loadObj = function(config) {
125
- if(!config){
129
+ if (!config) {
126
130
  config = this.config;
127
- }
128
- else {
131
+ } else {
129
132
  this.set_config(config);
130
133
  config = this.config;
131
134
  }
@@ -133,7 +136,7 @@ Item.prototype.loadObj = function(config) {
133
136
  if (f) {
134
137
  var file = f.fullname(this.dir);
135
138
  if (file.hasFile()) {
136
- if(!config.state){
139
+ if (!config.state) {
137
140
  return;
138
141
  }
139
142
  this.remove_module(file);
@@ -184,32 +187,13 @@ Item.prototype.loadFile = function(file) {
184
187
  return obj;
185
188
  };
186
189
 
187
- function rmdir(dir) {
188
- var list = fs.readdirSync(dir);
189
- for (var i = 0; i < list.length; i++) {
190
- var filename = path.join(dir, list[i]);
191
- var stat = fs.statSync(filename);
192
-
193
- if (filename == "." || filename == "..") {
194
- // pass these files
195
- } else if (stat.isDirectory()) {
196
- // rmdir recursively
197
- rmdir(filename);
198
- } else {
199
- // rm fiilename
200
- fs.unlinkSync(filename);
201
- }
202
- }
203
- fs.rmdirSync(dir);
204
- };
205
-
206
190
  /**
207
191
  * @description 删除脚本
208
192
  */
209
193
  Item.prototype.del_script = function() {
210
194
  var f = this.config.func_file;
211
195
  if (f) {
212
- rmdir(this.dir);
196
+ $.dir.del(this.dir);
213
197
  }
214
198
  };
215
199
 
@@ -301,12 +285,25 @@ Item.prototype.main = function(param1, param2) {
301
285
  return null;
302
286
  };
303
287
 
288
+
289
+ /**
290
+ * 调用函数
291
+ * @param {String} method 函数名
292
+ * @param {Object} params 参数集合
293
+ * @return {Object} 执行结果
294
+ */
295
+ Item.prototype.run = function(method, ...params) {
296
+ if (this.config.state && this[method]) {
297
+ return this[method](params);
298
+ }
299
+ };
300
+
304
301
  /**
305
302
  * 运行指令
306
303
  * @param {Number} state 状态 0为不可以,1为可用
307
304
  * @param {Array} ...params 参数集合
308
305
  */
309
- Item.prototype.run_cmd = function(state, ...params){
306
+ Item.prototype.run_cmd = function(state, ...params) {
310
307
  return null;
311
308
  };
312
309
 
@@ -315,7 +312,7 @@ Item.prototype.run_cmd = function(state, ...params){
315
312
  * @param {Number} state 状态 0为不可以,1为可用
316
313
  * @param {Array} ...params 参数集合
317
314
  */
318
- Item.prototype.cmd = function(state, ...params){
315
+ Item.prototype.cmd = function(state, ...params) {
319
316
  this.config.state = state;
320
317
  return this.run_cmd(state, ...param);
321
318
  }
@@ -360,6 +357,11 @@ class Index {
360
357
  * 模块目录
361
358
  */
362
359
  this.dir_base = dir_base;
360
+
361
+ /**
362
+ * 模式 0生产模式,1开发模式,
363
+ */
364
+ this.mode = 0;
363
365
  }
364
366
  }
365
367
 
@@ -496,9 +498,9 @@ Index.prototype.get = function(name) {
496
498
  * @param {Number} state 状态
497
499
  * @param {Array} ...params 参数集合
498
500
  */
499
- Index.prototype.cmd = async function(name, state, ...params){
501
+ Index.prototype.cmd = async function(name, state, ...params) {
500
502
  var obj = this.get(name);
501
- if(!obj){
503
+ if (!obj) {
502
504
  return "error: program does not exist";
503
505
  }
504
506
  return await obj.cmd(name, ...params);
@@ -643,7 +645,43 @@ Index.prototype.load_file = function(file) {
643
645
  return null;
644
646
  };
645
647
 
648
+ /**
649
+ * @description 重载脚本和配置
650
+ * @param {String} file 文件名
651
+ * @return {String} 重载失败返回错误提示,重载成功返回null
652
+ */
653
+ Index.prototype.reload = function(name) {
654
+ var o = this.get(name);
655
+ if (o) {
656
+ o.load(o.filename);
657
+ return null;
658
+ }
659
+ return '没有找到模块';
660
+ }
661
+
662
+ /**
663
+ * 调用函数
664
+ * @param {String} 模块名
665
+ * @param {String} method 函数名
666
+ * @param {Object} params 参数集合
667
+ * @return {Object} 执行结果
668
+ */
669
+ Index.prototype.run = async function(name, method, ...params) {
670
+ var o = await this.get(name);
671
+ var ret;
672
+ if (o) {
673
+ ret = o.run(method, params);
674
+ if (util.types.isPromise(ret)) {
675
+ ret = await ret;
676
+ }
677
+ if (this.mode) {
678
+ o.load(o.filename);
679
+ }
680
+ }
681
+ return ret;
682
+ };
683
+
646
684
  /**
647
685
  * @module 导出Index类
648
686
  */
649
- exports.Index = Index;
687
+ exports.Index = Index;
package/nodemon.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "restartable": "rs",
3
+ "ignore": [
4
+ ".git",
5
+ ".svn",
6
+ "node_modules/**/node_modules",
7
+ "static/src/*",
8
+ "static/js/*",
9
+ "static/**/js",
10
+ "static/*.js",
11
+ "*data.json",
12
+ "*cache.json"
13
+ ],
14
+ "verbose": true,
15
+ "execMap": {
16
+ "": "node",
17
+ "js": "node --harmony"
18
+ },
19
+ "watch": [
20
+ ],
21
+ "env": {
22
+ "NODE_ENV": "development"
23
+ },
24
+ "ext": "js json",
25
+ "legacy-watch": false
26
+ }
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "1.5.8",
3
+ "version": "1.6.1",
4
4
  "description": "这是超级美眉框架机制构建辅助模块,用于快速构建一个机制",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "node test.js"
7
+ "test": "node ./demo/demo.js",
8
+ "dev": "nodemon ./demo/demo.js"
8
9
  },
9
10
  "repository": {
10
11
  "type": "git",
@@ -29,6 +30,6 @@
29
30
  },
30
31
  "homepage": "https://github.com/qiuwenwu/mm_machine#readme",
31
32
  "dependencies": {
32
- "mm_config": "^1.0.9"
33
+ "mm_config": "^1.1.1"
33
34
  }
34
35
  }