mm_machine 1.5.7 → 1.6.0

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 +68 -34
  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
  /**
@@ -77,8 +77,8 @@ class Item {
77
77
  * 设置配置
78
78
  * @param {Object} config 配置
79
79
  */
80
- Item.prototype.set_config = function(config){
81
- this.config = conf(Object.assign({}, this.config, config || {}));
80
+ Item.prototype.set_config = function(config) {
81
+ this.config = conf(Object.assign({}, this.config, config || {}), this.filename);
82
82
  }
83
83
 
84
84
  /**
@@ -113,7 +113,7 @@ Item.prototype.remove_module = function(module) {
113
113
  * @description 卸载对象
114
114
  * @param {String} file 文件
115
115
  */
116
- Item.prototype.unloadObj = function(file){
116
+ Item.prototype.unloadObj = function(file) {
117
117
  this.remove_module(file || this.filename);
118
118
  }
119
119
 
@@ -122,10 +122,9 @@ Item.prototype.unloadObj = function(file){
122
122
  * @param {Object} obj 配置对象
123
123
  */
124
124
  Item.prototype.loadObj = function(config) {
125
- if(!config){
125
+ if (!config) {
126
126
  config = this.config;
127
- }
128
- else {
127
+ } else {
129
128
  this.set_config(config);
130
129
  config = this.config;
131
130
  }
@@ -133,7 +132,7 @@ Item.prototype.loadObj = function(config) {
133
132
  if (f) {
134
133
  var file = f.fullname(this.dir);
135
134
  if (file.hasFile()) {
136
- if(!config.state){
135
+ if (!config.state) {
137
136
  return;
138
137
  }
139
138
  this.remove_module(file);
@@ -143,7 +142,7 @@ Item.prototype.loadObj = function(config) {
143
142
  if (name) {
144
143
  this.main = cs[name];
145
144
  } else {
146
- $.push(this, cs);
145
+ $.push(this, cs, true);
147
146
  }
148
147
  }
149
148
  } else {
@@ -184,32 +183,13 @@ Item.prototype.loadFile = function(file) {
184
183
  return obj;
185
184
  };
186
185
 
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
186
  /**
207
187
  * @description 删除脚本
208
188
  */
209
189
  Item.prototype.del_script = function() {
210
190
  var f = this.config.func_file;
211
191
  if (f) {
212
- rmdir(this.dir);
192
+ $.dir.del(this.dir);
213
193
  }
214
194
  };
215
195
 
@@ -301,12 +281,25 @@ Item.prototype.main = function(param1, param2) {
301
281
  return null;
302
282
  };
303
283
 
284
+
285
+ /**
286
+ * 调用函数
287
+ * @param {String} method 函数名
288
+ * @param {Object} params 参数集合
289
+ * @return {Object} 执行结果
290
+ */
291
+ Item.prototype.run = function(method, ...params) {
292
+ if (this.config.state && this[method]) {
293
+ return this[method](params);
294
+ }
295
+ };
296
+
304
297
  /**
305
298
  * 运行指令
306
299
  * @param {Number} state 状态 0为不可以,1为可用
307
300
  * @param {Array} ...params 参数集合
308
301
  */
309
- Item.prototype.run_cmd = function(state, ...params){
302
+ Item.prototype.run_cmd = function(state, ...params) {
310
303
  return null;
311
304
  };
312
305
 
@@ -315,7 +308,7 @@ Item.prototype.run_cmd = function(state, ...params){
315
308
  * @param {Number} state 状态 0为不可以,1为可用
316
309
  * @param {Array} ...params 参数集合
317
310
  */
318
- Item.prototype.cmd = function(state, ...params){
311
+ Item.prototype.cmd = function(state, ...params) {
319
312
  this.config.state = state;
320
313
  return this.run_cmd(state, ...param);
321
314
  }
@@ -360,6 +353,11 @@ class Index {
360
353
  * 模块目录
361
354
  */
362
355
  this.dir_base = dir_base;
356
+
357
+ /**
358
+ * 模式 0生产模式,1开发模式,
359
+ */
360
+ this.mode = 0;
363
361
  }
364
362
  }
365
363
 
@@ -496,9 +494,9 @@ Index.prototype.get = function(name) {
496
494
  * @param {Number} state 状态
497
495
  * @param {Array} ...params 参数集合
498
496
  */
499
- Index.prototype.cmd = async function(name, state, ...params){
497
+ Index.prototype.cmd = async function(name, state, ...params) {
500
498
  var obj = this.get(name);
501
- if(!obj){
499
+ if (!obj) {
502
500
  return "error: program does not exist";
503
501
  }
504
502
  return await obj.cmd(name, ...params);
@@ -643,7 +641,43 @@ Index.prototype.load_file = function(file) {
643
641
  return null;
644
642
  };
645
643
 
644
+ /**
645
+ * @description 重载脚本和配置
646
+ * @param {String} file 文件名
647
+ * @return {String} 重载失败返回错误提示,重载成功返回null
648
+ */
649
+ Index.prototype.reload = function(name) {
650
+ var o = this.get(name);
651
+ if (o) {
652
+ o.load(o.filename);
653
+ return null;
654
+ }
655
+ return '没有找到模块';
656
+ }
657
+
658
+ /**
659
+ * 调用函数
660
+ * @param {String} 模块名
661
+ * @param {String} method 函数名
662
+ * @param {Object} params 参数集合
663
+ * @return {Object} 执行结果
664
+ */
665
+ Index.prototype.run = async function(name, method, ...params) {
666
+ var o = await this.get(name);
667
+ var ret;
668
+ if (o) {
669
+ ret = o.run(method, params);
670
+ if (util.types.isPromise(ret)) {
671
+ ret = await ret;
672
+ }
673
+ if (this.mode) {
674
+ o.load(o.filename);
675
+ }
676
+ }
677
+ return ret;
678
+ };
679
+
646
680
  /**
647
681
  * @module 导出Index类
648
682
  */
649
- exports.Index = Index;
683
+ 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.7",
3
+ "version": "1.6.0",
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.0"
33
34
  }
34
35
  }