ee-core 2.8.0-beta.1 → 2.8.0-beta.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.
package/cross/index.js CHANGED
@@ -1,16 +1,12 @@
1
1
  const fs = require('fs');
2
2
  const EventEmitter = require('events');
3
- const path = require('path');
4
- const is = require('is-type-of');
5
3
  const Conf = require('../config/cache');
6
- const UtilsHelper = require('../utils/helper');
7
- const UtilsIs = require('../utils/is');
8
- const UtilsPargv = require('../utils/pargv');
4
+ const Helper = require('../utils/helper');
9
5
  const Ps = require('../ps');
10
- const Log = require('../log');
11
- const GetPort = require('../utils/get-port');
12
6
  const SpawnProcess = require('./spawnProcess');
13
7
  const Channel = require('../const/channel');
8
+ const extend = require('../utils/extend');
9
+ const GetPort = require('../utils/get-port');
14
10
 
15
11
  /**
16
12
  * Cross-language service
@@ -39,7 +35,7 @@ const CrossLanguageService = {
39
35
 
40
36
  // boot services
41
37
  const servicesCfg = Conf.getValue('cross');
42
- //await UtilsHelper.sleep(5 * 1000);
38
+ //await Helper.sleep(5 * 1000);
43
39
 
44
40
  for (let key of Object.keys(servicesCfg)) {
45
41
  let cfg = servicesCfg[key];
@@ -52,19 +48,17 @@ const CrossLanguageService = {
52
48
  /**
53
49
  * _initEventEmitter
54
50
  */
55
- async _initEventEmitter() {
51
+ _initEventEmitter() {
56
52
  if (this.emitter) {
57
53
  return
58
54
  }
59
55
  this.emitter = new EventEmitter();
60
56
  this.emitter.on(Channel.events.childProcessExit, (data) => {
61
- console.log("------childProcessExit:", data)
62
57
  const child = this.children[data.pid];
63
58
  delete this.childrenMap[child.name];
64
59
  delete this.children[data.pid];
65
60
  });
66
61
  this.emitter.on(Channel.events.childProcessError, (data) => {
67
- console.log("------childProcessError:", data)
68
62
  const child = this.children[data.pid];
69
63
  delete this.childrenMap[child.name];
70
64
  delete this.children[data.pid];
@@ -74,39 +68,37 @@ const CrossLanguageService = {
74
68
  /**
75
69
  * run
76
70
  */
77
- async run(service) {
71
+ async run(service, opt = {}) {
78
72
  // init dir
79
73
  this._initPath();
80
74
 
81
75
  const allConfig = Conf.all();
82
- if (!allConfig.cross.hasOwnProperty(service)) {
76
+ const defaultOpt = allConfig.cross[service] || {};
77
+ console.log("defaultOpt:", defaultOpt);
78
+ //const targetConf = Object.assign({}, defaultOpt, opt);
79
+ const targetConf = extend(true, {}, defaultOpt, opt);
80
+ if (Object.keys(targetConf).length == 0) {
83
81
  throw new Error(`[ee-core] [cross] The service [${service}] config does not exit`);
84
82
  }
85
-
86
- const targetConf = Object.assign({}, allConfig.cross[service]);
83
+ console.log("targetConf:", targetConf);
87
84
 
88
85
  // eventEmitter
89
86
  this._initEventEmitter();
90
87
 
91
- let cmdPath = this._getCmdPath(targetConf.name);
92
- let cmdArgs = is.string(targetConf.args) ? [targetConf.args] : targetConf.args;
93
- if (targetConf.hasOwnProperty('cmd')) {
94
- cmdPath = targetConf.cmd;
95
- }
96
-
97
- let confPort = this._getValueFromArgs(cmdArgs, 'port');
98
- if (UtilsHelper.validValue(confPort)) {
99
- // 动态生成port
88
+ // format params
89
+ let tmpArgs = targetConf.args;
90
+ let confPort = parseInt(Helper.getValueFromArgv(tmpArgs, 'port'));
91
+ console.log("confPort1:", confPort);
92
+ if (confPort > 0) {
93
+ // 动态生成port,传入的端口必须为int
100
94
  confPort = await GetPort({ port: confPort });
95
+ console.log("confPort2:", confPort);
101
96
  // 替换port
102
- cmdArgs = this._replaceValue(cmdArgs, "--port=", confPort)
103
- targetConf.args = cmdArgs;
97
+ targetConf.args = Helper.replaceArgsValue(tmpArgs, "port", String(confPort));
104
98
  }
105
99
 
106
- Log.coreLogger.info(`[ee-core] [cross/run] cmd: ${cmdPath}, args: ${cmdArgs}`);
107
-
108
100
  // 创建进程
109
- const subProcess = new SpawnProcess(this, { cmdPath, cmdArgs, targetConf });
101
+ const subProcess = new SpawnProcess(this, { targetConf, port: confPort });
110
102
  let uniqueName = targetConf.name;
111
103
  if (this.childrenMap.hasOwnProperty(uniqueName)) {
112
104
  uniqueName = uniqueName + "-" + String(subProcess.pid);
@@ -123,7 +115,6 @@ const CrossLanguageService = {
123
115
 
124
116
  killAll() {
125
117
  Object.keys(this.children).forEach(pid => {
126
- console.log("-----killAll: ", pid);
127
118
  this.kill(pid)
128
119
  });
129
120
  },
@@ -142,35 +133,9 @@ const CrossLanguageService = {
142
133
  }
143
134
  },
144
135
 
145
- _getValueFromArgs(argv, key) {
146
- // parse args
147
- const argsObj = UtilsPargv(argv);
148
- const value = argsObj[key];
149
-
150
- return value;
151
- },
152
-
153
- _replaceValue(arr, key, value) {
154
- arr = arr.map(item => {
155
- if (item.startsWith(key)) {
156
- let newItem = key + value;
157
- return newItem;
158
- } else {
159
- return item;
160
- }
161
- });
162
- return arr;
163
- },
164
-
165
136
  getUrl(name) {
166
137
  const entity = this.getProcByName(name);
167
- const args = entity.getArgsObj();
168
- let protocol = 'http://';
169
- if (args.hasOwnProperty('ssl') && (args.ssl == 'true' || args.ssl == '1')) {
170
- protocol = 'https://';
171
- }
172
- const hostname = args.hostname ? args.hostname : '127.0.0.1';
173
- const url = protocol + hostname + ":" + args.port;
138
+ const url = entity.getUrl();
174
139
 
175
140
  return url;
176
141
  },
@@ -204,21 +169,16 @@ const CrossLanguageService = {
204
169
  return pids;
205
170
  },
206
171
 
207
- _getCmdPath(name) {
208
- const coreName = UtilsIs.windows() ? name + ".exe" : name;
209
- const p = path.join(Ps.getExtraResourcesDir(), coreName);
210
- return p;
211
- },
212
-
213
172
  /**
214
173
  * init path
215
174
  */
216
175
  _initPath() {
217
176
  const pathname = Ps.getUserHomeConfigDir();
218
177
  if (!fs.existsSync(pathname)) {
219
- UtilsHelper.mkdir(pathname, {mode: 0o755});
178
+ Helper.mkdir(pathname, {mode: 0o755});
220
179
  }
221
- }
180
+ },
181
+
222
182
  }
223
183
 
224
184
  module.exports = CrossLanguageService;
@@ -1,12 +1,12 @@
1
1
  const EventEmitter = require('events');
2
2
  const path = require('path');
3
3
  const crossSpawn = require('cross-spawn');
4
- const serialize = require('serialize-javascript');
5
- const { app: electronApp } = require('electron');
6
4
  const Log = require('../log');
7
5
  const Ps = require('../ps');
8
6
  const Channel = require('../const/channel');
7
+ const EE = require('../ee');
9
8
  const Helper = require('../utils/helper');
9
+ const UtilsIs = require('../utils/is');
10
10
  const UtilsPargv = require('../utils/pargv');
11
11
 
12
12
  class SpawnProcess {
@@ -15,30 +15,73 @@ class SpawnProcess {
15
15
  this.host = host;
16
16
  this.child = undefined;
17
17
  this.pid = 0;
18
+ this.port = 0;
18
19
  this.name = "";
19
20
  this.config = {};
20
21
  this._init(opt);
21
22
  }
22
23
 
23
-
24
24
  /**
25
25
  * 初始化子进程
26
26
  */
27
27
  _init(options = {}) {
28
- const { cmdPath, cmdArgs, targetConf } = options;
28
+ const { targetConf, port } = options;
29
29
  this.config = targetConf;
30
- this.name = this.config.name;
30
+ this.port = port;
31
+ // 该名称如果在childrenMap重复,会被重写
32
+ this.name = targetConf.name;
31
33
 
32
34
  // Launch executable program
33
- let standardOutput = ['ignore', 'ignore', 'ignore', 'ipc'];
34
- if (!Ps.isPackaged()) {
35
- standardOutput = ['inherit', 'inherit', 'inherit', 'ipc'];
36
- }
35
+ let cmdPath = '';
36
+ let cmdArgs = targetConf.args;
37
+ console.log("targetConf.args:", targetConf.args);
37
38
  let execDir = Ps.getExtraResourcesDir();
38
- if (this.config.hasOwnProperty('directory')) {
39
- execDir = path.join(process.cwd(), this.config.directory);
39
+ let standardOutput = ['inherit', 'inherit', 'inherit', 'ipc'];
40
+ if (Ps.isPackaged()) {
41
+ standardOutput = ['ignore', 'ignore', 'ignore', 'ipc'];
42
+ }
43
+
44
+ const { cmd, directory } = targetConf;
45
+ // use cmd first
46
+ if (cmd) {
47
+ console.log("cmd:", cmd)
48
+ console.log("cmd isAbsolute :", path.isAbsolute(cmd))
49
+ if (!directory) {
50
+ throw new Error(`[ee-core] [cross] The config [directory] attribute does not exist`);
51
+ }
52
+ cmdPath = cmd;
53
+ if (!path.isAbsolute(cmd) && !Ps.isDev()) {
54
+ cmdPath = path.join(Ps.getExtraResourcesDir(), cmd);
55
+ }
56
+ } else {
57
+ cmdPath = path.join(Ps.getExtraResourcesDir(), targetConf.name);
58
+ }
59
+
60
+ // windonw
61
+ if (UtilsIs.windows() && path.extname(cmdPath) != '.exe') {
62
+ cmdPath += ".exe";
63
+ }
64
+
65
+ console.log("cmdPath:", cmdPath)
66
+
67
+ // executable program directory
68
+ console.log("directory:", directory)
69
+ if (directory && path.isAbsolute(directory)) {
70
+ console.log("directory isAbsolute :", path.isAbsolute(directory))
71
+ execDir = directory;
72
+ } else if (directory && !path.isAbsolute(directory)) {
73
+ console.log("directory isAbsolute :", path.isAbsolute(directory))
74
+ if (Ps.isDev()) {
75
+ execDir = path.join(Ps.getHomeDir(), directory);
76
+ } else {
77
+ execDir = path.join(Ps.getExtraResourcesDir(), directory);
78
+ }
79
+ } else {
80
+ execDir = Ps.getExtraResourcesDir();
40
81
  }
82
+ console.log("execDir:", execDir)
41
83
 
84
+ Log.coreLogger.info(`[ee-core] [cross/run] cmd: ${cmdPath}, args: ${cmdArgs}`);
42
85
  const coreProcess = crossSpawn(cmdPath, cmdArgs, {
43
86
  stdio: standardOutput,
44
87
  detached: false,
@@ -47,28 +90,6 @@ class SpawnProcess {
47
90
  });
48
91
  this.child = coreProcess;
49
92
  this.pid = coreProcess.pid;
50
- coreProcess.on('close', (code, signal) => {
51
- Log.coreLogger.info(`[ee-core] [cross/process] received a close from child-process, code:${code}, signal:${signal}, pid:${this.pid}`);
52
-
53
- // electron quit
54
- if (this.config.appExit) {
55
- setTimeout(() => {
56
- // 进程退出前的一些清理工作
57
- electronApp.quit();
58
- }, 1000)
59
- }
60
- });
61
-
62
- coreProcess.on('message', (m) => {
63
- Log.coreLogger.info(`[ee-core] [corss/process] received a message from child-process, message: ${serialize(m)}`);
64
-
65
- if (!m || !m.id) {
66
- return;
67
- }
68
-
69
- // 收到子进程消息,转发到 event
70
- //this.emitter.emit(m.event, m.data);
71
- });
72
93
 
73
94
  coreProcess.on('exit', (code, signal) => {
74
95
  let data = {
@@ -76,6 +97,7 @@ class SpawnProcess {
76
97
  }
77
98
  this.host.emitter.emit(Channel.events.childProcessExit, data);
78
99
  Log.coreLogger.info(`[ee-core] [corss/process] received a exit from child-process, code:${code}, signal:${signal}, pid:${this.pid}`);
100
+ this._exitElectron();
79
101
  });
80
102
 
81
103
  coreProcess.on('error', (err) => {
@@ -84,6 +106,7 @@ class SpawnProcess {
84
106
  }
85
107
  this.host.emitter.emit(Channel.events.childProcessError, data);
86
108
  Log.coreLogger.error(`[ee-core] [corss/process] received a error from child-process, error: ${err}, pid:${this.pid}`);
109
+ this._exitElectron();
87
110
  });
88
111
  }
89
112
 
@@ -91,7 +114,6 @@ class SpawnProcess {
91
114
  * kill
92
115
  */
93
116
  kill(timeout = 1000) {
94
- console.log("----- spawnProcess kill ---- ");
95
117
  this.child.kill('SIGINT');
96
118
  setTimeout(() => {
97
119
  if (this.child.killed) return;
@@ -107,11 +129,6 @@ class SpawnProcess {
107
129
  return this.sendByType('close', 'close');
108
130
  }
109
131
 
110
- _generateId() {
111
- const rid = Helper.getRandomString();
112
- return `node:${this.pid}:${rid}`;
113
- }
114
-
115
132
  async sendByType(message, type) {
116
133
  const msg = typeof message === 'string' ? message : JSON.stringify(message);
117
134
  const id = this._generateId();
@@ -124,10 +141,41 @@ class SpawnProcess {
124
141
  return;
125
142
  }
126
143
 
144
+ getUrl() {
145
+ const ssl = Helper.getValueFromArgv(this.config.args, 'ssl');
146
+ let hostname = Helper.getValueFromArgv(this.config.args, 'hostname')
147
+ let protocol = 'http://';
148
+ if (ssl && (ssl == 'true' || ssl == '1')) {
149
+ protocol = 'https://';
150
+ }
151
+ hostname = hostname ? hostname : '127.0.0.1';
152
+ const url = protocol + hostname + ":" + this.port;
153
+
154
+ return url;
155
+ }
156
+
127
157
  getArgsObj() {
128
158
  const obj = UtilsPargv(this.config.args);
129
159
  return obj;
130
160
  }
161
+
162
+ _generateId() {
163
+ const rid = Helper.getRandomString();
164
+ return `node:${this.pid}:${rid}`;
165
+ }
166
+
167
+ /**
168
+ * exit electron
169
+ */
170
+ _exitElectron(timeout = 1000) {
171
+ const { CoreApp } = EE;
172
+ if (this.config.appExit) {
173
+ setTimeout(() => {
174
+ // 进程退出前的一些清理工作
175
+ CoreApp.appQuit();
176
+ }, timeout)
177
+ }
178
+ }
131
179
  }
132
180
 
133
181
  module.exports = SpawnProcess;
package/ee/eeApp.js CHANGED
@@ -32,19 +32,19 @@ class EeApp extends BaseApp {
32
32
  */
33
33
  async createPorts() {
34
34
  if (Ps.isFrameworkMode() && Conf.isWebProtocol(this.config.mainServer)) {
35
- const mainPort = await GetPort({port: this.config.mainServer.port});
35
+ const mainPort = await GetPort({port: parseInt(this.config.mainServer.port)});
36
36
  process.env.EE_MAIN_PORT = mainPort;
37
37
  this.config.mainServer.port = mainPort;
38
38
  }
39
39
 
40
40
  if (this.config.socketServer.enable) {
41
- const socketPort = await GetPort({port: this.config.socketServer.port});
41
+ const socketPort = await GetPort({port: parseInt(this.config.socketServer.port)});
42
42
  process.env.EE_SOCKET_PORT = socketPort;
43
43
  this.config.socketServer.port = socketPort;
44
44
  }
45
45
 
46
46
  if (this.config.httpServer.enable) {
47
- const httpPort = await GetPort({port: this.config.httpServer.port});
47
+ const httpPort = await GetPort({port: parseInt(this.config.httpServer.port)});
48
48
  process.env.EE_HTTP_PORT = httpPort;
49
49
  this.config.httpServer.port = httpPort;
50
50
  }
package/main/index.js CHANGED
@@ -30,8 +30,6 @@ class ElectronEgg {
30
30
  if ( argsObj['env'] == 'development' || argsObj['env'] === 'dev' || argsObj['env'] === 'local' ) {
31
31
  isDev = true;
32
32
  }
33
- console.log("argsObj env:", argsObj)
34
- console.log("isDev:", isDev)
35
33
 
36
34
  // module mode
37
35
  if (Ps.isModuleMode()) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "2.8.0-beta.1",
3
+ "version": "2.8.0-beta.3",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/utils/helper.js CHANGED
@@ -5,6 +5,7 @@ const is = require('is-type-of');
5
5
  const co = require('./co');
6
6
  const path = require('path');
7
7
  const chalk = require('chalk');
8
+ const Pargv = require('./pargv');
8
9
 
9
10
  const _basePath = process.cwd();
10
11
 
@@ -172,4 +173,58 @@ exports.loadConfig = function(prop) {
172
173
 
173
174
  exports.sleep = function(ms) {
174
175
  return new Promise(resolve => setTimeout(resolve, ms));
176
+ };
177
+
178
+ exports.replaceArgsValue = function(argv, key, value) {
179
+ key = key + "=";
180
+ for (let i = 0; i < argv.length; i++) {
181
+ let item = argv[i];
182
+ let pos = item.indexOf(key);
183
+ if (pos !== -1) {
184
+ pos = pos + key.length;
185
+ console.log("pos2:", pos)
186
+ let tmpStr = item.substring(0, pos);
187
+ console.log("tmpStr:", tmpStr)
188
+ argv[i] = tmpStr + value;
189
+ break;
190
+ }
191
+ }
192
+
193
+ return argv;
194
+ };
195
+
196
+ // exports.replaceValue = function(arr, key, value) {
197
+ // arr = arr.map(item => {
198
+ // if (item.startsWith(key)) {
199
+ // let newItem = key + value;
200
+ // return newItem;
201
+ // } else {
202
+ // return item;
203
+ // }
204
+ // });
205
+ // return arr;
206
+ // };
207
+
208
+ exports.getValueFromArgv = function(argv, key) {
209
+ const argvObj = Pargv(argv);
210
+ if (argvObj.hasOwnProperty(key)) {
211
+ return argvObj[key];
212
+ }
213
+
214
+ // match search
215
+ key = key + "=";
216
+ let value;
217
+ for (let i = 0; i < argv.length; i++) {
218
+ let item = argv[i];
219
+ let pos = item.indexOf(key);
220
+ if (pos !== -1) {
221
+ pos = pos + key.length;
222
+ console.log("pos2:", pos)
223
+ value = item.substring(pos);
224
+ console.log("value:", value)
225
+ break;
226
+ }
227
+ }
228
+
229
+ return value;
175
230
  };