ee-bin 4.1.4 → 4.1.6-beta.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.
@@ -22,7 +22,7 @@ module.exports = {
22
22
  electron: {
23
23
  directory: './',
24
24
  cmd: 'electron',
25
- args: ['.', '--env=local'],
25
+ args: ['.', '--env=local', '--debugger=false'],
26
26
  loadingPage: '/public/html/loading.html',
27
27
  watch: false,
28
28
  sync: false,
package/lib/utils.js CHANGED
@@ -144,11 +144,11 @@ function getPackage () {
144
144
  return content;
145
145
  }
146
146
 
147
- function readJsonSync (filepath) {
147
+ function readJsonSync (filepath, encoding = 'utf8') {
148
148
  if (!fs.existsSync(filepath)) {
149
149
  throw new Error(filepath + ' is not found');
150
150
  }
151
- return JSON.parse(fs.readFileSync(filepath));
151
+ return JSON.parse(fs.readFileSync(filepath, { encoding }));
152
152
  }
153
153
 
154
154
  function writeJsonSync (filepath, str, options) {
@@ -191,6 +191,20 @@ function getPlatform(delimiter = "_", isDiffArch = false) {
191
191
  return os;
192
192
  }
193
193
 
194
+ // Get cmd parameter by name
195
+ function getArgumentByName(args, name) {
196
+ if (!args) {
197
+ args = process.argv;
198
+ }
199
+ for (let i = 0; i < args.length; i++) {
200
+ const item = args[i];
201
+ const prefixKey = `--${name}=`;
202
+ if (item.indexOf(prefixKey) !== -1) {
203
+ return item.substring(prefixKey.length);
204
+ }
205
+ }
206
+ }
207
+
194
208
  module.exports = {
195
209
  loadConfig,
196
210
  getElectronProgram,
@@ -205,5 +219,6 @@ module.exports = {
205
219
  rm,
206
220
  getPackage,
207
221
  readJsonSync,
208
- writeJsonSync
222
+ writeJsonSync,
223
+ getArgumentByName
209
224
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-bin",
3
- "version": "4.1.4",
3
+ "version": "4.1.6-beta.1",
4
4
  "description": "ee bin",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/tools/serve.js CHANGED
@@ -3,13 +3,14 @@
3
3
  const debug = require('debug')('ee-bin:serve');
4
4
  const path = require('path');
5
5
  const fsPro = require('fs-extra');
6
- const { loadConfig } = require('../lib/utils');
6
+ const { loadConfig, isWindows, getArgumentByName, readJsonSync, writeJsonSync } = require('../lib/utils');
7
7
  const is = require('is-type-of');
8
8
  const chalk = require('chalk');
9
9
  const crossSpawn = require('cross-spawn');
10
10
  const { buildSync } = require('esbuild');
11
11
  const chokidar = require('chokidar');
12
12
  const kill = require('tree-kill');
13
+ const process = require("process");
13
14
 
14
15
  class ServeProcess {
15
16
 
@@ -17,14 +18,57 @@ class ServeProcess {
17
18
  process.env.NODE_ENV = 'prod'; // dev / prod
18
19
  this.execProcess = {};
19
20
  this.electronDir = './electron';
20
- this.defaultBundleDir = './public/electron';
21
+ this.bundleDir = './public/electron';
22
+ this.pkgPath = './package.json';
23
+ this._init();
21
24
  }
22
25
 
23
26
  /**
24
- * 启动前端、主进程服务
27
+ * init
28
+ */
29
+ _init() {
30
+ // process manager
31
+ // Monitor SIGINT signal(Ctrl + C)
32
+ process.on('SIGINT', () => {
33
+ console.log(chalk.blue('[ee-bin] ') + `Received SIGINT. Closing processes...`);
34
+ this._closeProcess();
35
+ });
36
+
37
+ // Monitor SIGTERM signal
38
+ process.on('SIGTERM', () => {
39
+ console.log(chalk.blue('[ee-bin] ') + `Received SIGTERM. Closing processes...`);
40
+ this._closeProcess();
41
+ });
42
+ }
43
+
44
+ // Close process
45
+ async _closeProcess() {
46
+ const currentProcess = [];
47
+ const keys = Object.keys(this.execProcess);
48
+ const len = keys.length;
49
+ for (let i = 0; i < len; i++) {
50
+ const key = keys[i];
51
+ const p = this.execProcess[key];
52
+ currentProcess.push({
53
+ name: key,
54
+ pid: p.pid,
55
+ });
56
+ }
57
+
58
+ // Cleaning work before the end of the process
59
+ await this.sleep(500);
60
+ currentProcess.forEach((p) => {
61
+ kill(p.pid);
62
+ debug(`Kill ${chalk.blue(p.name)} server, pid: ${p.pid}`);
63
+ });
64
+ process.exit(0);
65
+ }
66
+
67
+ /**
68
+ * Start frontend and main process services
25
69
  */
26
70
  dev(options = {}) {
27
- // 设置一个环境变量
71
+ // Set an environment variable
28
72
  process.env.NODE_ENV = 'dev';
29
73
  const { config, serve } = options;
30
74
  const binCfg = loadConfig(config);
@@ -44,8 +88,13 @@ class ServeProcess {
44
88
  // build electron main code
45
89
  const cmds = this._formatCmds(command);
46
90
  if (cmds.indexOf("electron") !== -1) {
47
- // watche electron main code
48
91
  const electronConfig = binCmdConfig.electron;
92
+
93
+ // Debugging source code
94
+ const debugging = getArgumentByName(electronConfig.args, 'debuger') == 'true'? true : false;
95
+ this._switchPkgMain(debugging);
96
+
97
+ // watche electron main code
49
98
  if (electronConfig.watch) {
50
99
  let debounceTimer = null;
51
100
  const cmd = 'electron';
@@ -53,7 +102,8 @@ class ServeProcess {
53
102
  persistent: true
54
103
  });
55
104
  watcher.on('change', async (f) => {
56
- console.log(chalk.blue('[ee-bin] [dev] ') + `File ${f} has been changed`);
105
+ //console.log(chalk.blue('[ee-bin] [dev] ') + 'File ' + chalk.cyan(`[${f}]`) + 'has been changed');
106
+ console.log(chalk.blue('[ee-bin] [dev] ') + `File [${chalk.cyan(f)}] has been changed`);
57
107
 
58
108
  // 防抖
59
109
  if (debounceTimer) {
@@ -91,7 +141,7 @@ class ServeProcess {
91
141
  }
92
142
 
93
143
  /**
94
- * 启动主进程服务
144
+ * Start the main process service
95
145
  */
96
146
  start(options = {}) {
97
147
  const { config } = options;
@@ -114,10 +164,11 @@ class ServeProcess {
114
164
  }
115
165
 
116
166
  /**
117
- * 构建
167
+ * build
118
168
  */
119
169
  build(options = {}) {
120
- const { config, cmds, env } = options;
170
+ const { config, env } = options;
171
+ let { cmds } = options;
121
172
  process.env.NODE_ENV = env;
122
173
  const binCfg = loadConfig(config);
123
174
  const binCmd = 'build';
@@ -129,9 +180,18 @@ class ServeProcess {
129
180
  return
130
181
  }
131
182
 
132
- if (cmds.indexOf("electron") !== -1) {
183
+ // [todo] If there is 'electron' , then execute 'electron' first and recycle other commands
184
+ // should it be placed in multiExec() and maintain the execution order
185
+ const commands = this._formatCmds(cmds);
186
+ if (commands.indexOf("electron") !== -1) {
133
187
  this.bundle(binCmdConfig.electron);
134
- return;
188
+ // Remove electron cmd and execute others
189
+ const index = commands.indexOf("electron");
190
+ commands.splice(index, 1);
191
+ cmds = commands.join();
192
+
193
+ // switch pkg.main
194
+ this._switchPkgMain(false)
135
195
  }
136
196
 
137
197
  const opt = {
@@ -143,7 +203,7 @@ class ServeProcess {
143
203
  }
144
204
 
145
205
  /**
146
- * 执行自定义命令
206
+ * Execute custom commands
147
207
  */
148
208
  exec(options = {}) {
149
209
  const { config, cmds } = options;
@@ -160,19 +220,20 @@ class ServeProcess {
160
220
  }
161
221
 
162
222
  /**
163
- * 支持多个命令
223
+ * Support multiple commands
164
224
  */
165
225
  multiExec(opt = {}) {
166
226
  //console.log('multiExec opt:', opt)
167
227
  const { binCmd, binCmdConfig, command } = opt;
168
- const cmds = this._formatCmds(command);
228
+ const commands = this._formatCmds(command);
169
229
 
170
- for (let i = 0; i < cmds.length; i++) {
171
- let cmd = cmds[i];
230
+ for (let i = 0; i < commands.length; i++) {
231
+ let cmd = commands[i];
172
232
  const cfg = binCmdConfig[cmd];
173
233
 
174
234
  if (!cfg) {
175
- console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.red(`Error: [${binCmd} ${cmd}] config does not exist` ));
235
+ // Running the build electron code separately may be empty
236
+ //console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.yellow(`Warning: [${cmd}] config does not exist` ));
176
237
  continue;
177
238
  }
178
239
 
@@ -181,8 +242,8 @@ class ServeProcess {
181
242
  continue;
182
243
  }
183
244
 
184
- console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + "Run " + chalk.green(`[${binCmd} ${cmd}]` + " command"));
185
- console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.green('config:'), JSON.stringify(cfg));
245
+ console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + `Run ${chalk.green(cmd)} command`);
246
+ console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.magenta('Config:'), JSON.stringify(cfg));
186
247
 
187
248
  const execDir = path.join(process.cwd(), cfg.directory);
188
249
  const execArgs = is.string(cfg.args) ? [cfg.args] : cfg.args;
@@ -195,15 +256,18 @@ class ServeProcess {
195
256
  execArgs,
196
257
  { stdio: stdio, cwd: execDir, maxBuffer: 1024 * 1024 * 1024 },
197
258
  );
198
- console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + 'The ' + chalk.green(`[${binCmd} ${cmd}]`) + ` command is ${cfg.sync ? 'run completed' : 'running'}`);
259
+ console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + 'The ' + chalk.green(`${cmd}`) + ` command is ${cfg.sync ? 'run completed' : 'running'}`);
199
260
 
200
261
  if(!cfg.sync) {
201
262
  this.execProcess[cmd].on('exit', () => {
202
- if (cmd == 'electron') {
203
- console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.green('Press "CTRL+C" to exit'));
263
+ if (binCmd == 'dev') {
264
+ console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + `The ${chalk.green(cmd)} process is exiting`);
265
+ if (isWindows() && cmd == 'electron') {
266
+ console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.green('Press "CTRL+C" to exit'));
267
+ }
204
268
  return
205
269
  }
206
- console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + 'The ' + chalk.green(`[${binCmd} ${cmd}]`) + ' command has been executed and exited');
270
+ console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + `The ${chalk.green(cmd)} command has been executed and exited`);
207
271
  });
208
272
  }
209
273
  }
@@ -214,7 +278,7 @@ class ServeProcess {
214
278
  const { bundleType } = bundleConfig;
215
279
  if (bundleType == 'copy') {
216
280
  const srcResource = path.join(process.cwd(), this.electronDir);
217
- const destResource = path.join(process.cwd(), this.defaultBundleDir);
281
+ const destResource = path.join(process.cwd(), this.bundleDir);
218
282
  fsPro.removeSync(destResource);
219
283
  fsPro.copySync(srcResource, destResource);
220
284
  } else {
@@ -240,6 +304,32 @@ class ServeProcess {
240
304
 
241
305
  return cmds;
242
306
  }
307
+
308
+ // Modify the main attribute in package.json
309
+ _switchPkgMain(isDebugger = false) {
310
+ let mainFile = 'main.js';
311
+ const pkgPath = path.join(process.cwd(), this.pkgPath);
312
+ const pkg = readJsonSync(pkgPath);
313
+ const maints = path.join(process.cwd(), this.electronDir, 'main.ts');
314
+ if (fsPro.existsSync(maints)) {
315
+ mainFile = 'main.ts'
316
+ }
317
+
318
+ // [todo] Currently only supports JS
319
+ if (isDebugger && mainFile == 'main.js') {
320
+ pkg.main = path.join(this.electronDir, mainFile);
321
+ //console.log("debugger:", pkg.main)
322
+ writeJsonSync(pkgPath, pkg);
323
+ } else {
324
+ // Modify when the path is incorrect to reduce unnecessary operations
325
+ const bundleMainPath = path.join(this.bundleDir, 'main.js');
326
+ //console.log("build:", bundleMainPath)
327
+ if (pkg.main != bundleMainPath) {
328
+ pkg.main = bundleMainPath;
329
+ writeJsonSync(pkgPath, pkg);
330
+ }
331
+ }
332
+ }
243
333
 
244
334
  // env
245
335
  isDev() {