ee-core 2.8.0-beta.2 → 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;
@@ -4,9 +4,10 @@ const crossSpawn = require('cross-spawn');
4
4
  const Log = require('../log');
5
5
  const Ps = require('../ps');
6
6
  const Channel = require('../const/channel');
7
+ const EE = require('../ee');
7
8
  const Helper = require('../utils/helper');
9
+ const UtilsIs = require('../utils/is');
8
10
  const UtilsPargv = require('../utils/pargv');
9
- const EE = require('../ee');
10
11
 
11
12
  class SpawnProcess {
12
13
  constructor(host, opt = {}) {
@@ -14,30 +15,73 @@ class SpawnProcess {
14
15
  this.host = host;
15
16
  this.child = undefined;
16
17
  this.pid = 0;
18
+ this.port = 0;
17
19
  this.name = "";
18
20
  this.config = {};
19
21
  this._init(opt);
20
22
  }
21
23
 
22
-
23
24
  /**
24
25
  * 初始化子进程
25
26
  */
26
27
  _init(options = {}) {
27
- const { cmdPath, cmdArgs, targetConf } = options;
28
+ const { targetConf, port } = options;
28
29
  this.config = targetConf;
29
- this.name = this.config.name;
30
+ this.port = port;
31
+ // 该名称如果在childrenMap重复,会被重写
32
+ this.name = targetConf.name;
30
33
 
31
34
  // Launch executable program
32
- let standardOutput = ['ignore', 'ignore', 'ignore', 'ipc'];
33
- if (!Ps.isPackaged()) {
34
- standardOutput = ['inherit', 'inherit', 'inherit', 'ipc'];
35
- }
35
+ let cmdPath = '';
36
+ let cmdArgs = targetConf.args;
37
+ console.log("targetConf.args:", targetConf.args);
36
38
  let execDir = Ps.getExtraResourcesDir();
37
- if (this.config.hasOwnProperty('directory')) {
38
- 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);
39
58
  }
40
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();
81
+ }
82
+ console.log("execDir:", execDir)
83
+
84
+ Log.coreLogger.info(`[ee-core] [cross/run] cmd: ${cmdPath}, args: ${cmdArgs}`);
41
85
  const coreProcess = crossSpawn(cmdPath, cmdArgs, {
42
86
  stdio: standardOutput,
43
87
  detached: false,
@@ -70,7 +114,6 @@ class SpawnProcess {
70
114
  * kill
71
115
  */
72
116
  kill(timeout = 1000) {
73
- console.log("----- spawnProcess kill ---- ");
74
117
  this.child.kill('SIGINT');
75
118
  setTimeout(() => {
76
119
  if (this.child.killed) return;
@@ -86,11 +129,6 @@ class SpawnProcess {
86
129
  return this.sendByType('close', 'close');
87
130
  }
88
131
 
89
- _generateId() {
90
- const rid = Helper.getRandomString();
91
- return `node:${this.pid}:${rid}`;
92
- }
93
-
94
132
  async sendByType(message, type) {
95
133
  const msg = typeof message === 'string' ? message : JSON.stringify(message);
96
134
  const id = this._generateId();
@@ -103,11 +141,29 @@ class SpawnProcess {
103
141
  return;
104
142
  }
105
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
+
106
157
  getArgsObj() {
107
158
  const obj = UtilsPargv(this.config.args);
108
159
  return obj;
109
160
  }
110
161
 
162
+ _generateId() {
163
+ const rid = Helper.getRandomString();
164
+ return `node:${this.pid}:${rid}`;
165
+ }
166
+
111
167
  /**
112
168
  * exit electron
113
169
  */
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.2",
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
  };