ee-bin 1.2.0-beta.2 → 1.2.0-beta.4

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/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const program = require('commander');
3
+ const { program } = require('commander');
4
4
 
5
5
  /**
6
6
  * rd - Moves front-end resources to a specified directory
@@ -8,9 +8,9 @@ const program = require('commander');
8
8
  program
9
9
  .command('rd')
10
10
  .description('Move frontend resources to public/dist')
11
- .option('--dist-dir <folder>', 'title to use before name', './frontend/dist') // 兼容旧的api
12
- .option('--dist <folder>', 'title to use before name', './frontend/dist')
13
- .option('--target <folder>', 'title to use before name', './public/dist')
11
+ .option('--config <folder>', 'config file', './electron/config/bin.js')
12
+ .option('--dist <folder>', 'title to use before name')
13
+ .option('--target <folder>', 'title to use before name')
14
14
  .action(function() {
15
15
  const replaceDist = require('./tools/replaceDist');
16
16
  replaceDist.run(this.opts());
@@ -57,11 +57,11 @@ program
57
57
  */
58
58
  program
59
59
  .command('dev')
60
- .description('create frontend-server and electron-server')
60
+ .description('create frontend-serve and electron-serve')
61
61
  .option('--config <folder>', 'config file', './electron/config/bin.js')
62
62
  .action(function() {
63
- const frontend = require('./tools/frontend');
64
- frontend.serve(this.opts());
63
+ const serve = require('./tools/serve');
64
+ serve.dev(this.opts());
65
65
  });
66
66
 
67
67
  /**
@@ -72,8 +72,20 @@ program
72
72
  .description('build frontend dist')
73
73
  .option('--config <folder>', 'config file', './electron/config/bin.js')
74
74
  .action(function() {
75
- const frontend = require('./tools/frontend');
76
- frontend.build(this.opts());
75
+ const serve = require('./tools/serve');
76
+ serve.build(this.opts());
77
+ });
78
+
79
+ /**
80
+ * start
81
+ */
82
+ program
83
+ .command('start')
84
+ .description('preview effect')
85
+ .option('--config <folder>', 'config file', './electron/config/bin.js')
86
+ .action(function() {
87
+ const serve = require('./tools/serve');
88
+ serve.start(this.opts());
77
89
  });
78
90
 
79
- program.parse();
91
+ program.parse();
package/lib/utils.js CHANGED
@@ -52,8 +52,60 @@ function loadEncryptConfig() {
52
52
  return ret || {};
53
53
  };
54
54
 
55
+ /**
56
+ * get electron program
57
+ */
58
+ function getElectronProgram() {
59
+ let electronPath
60
+ const electronModulePath = path.dirname(require.resolve('electron'))
61
+ const pathFile = path.join(electronModulePath, 'path.txt')
62
+ const executablePath = fs.readFileSync(pathFile, 'utf-8')
63
+ if (executablePath) {
64
+ electronPath = path.join(electronModulePath, 'dist', executablePath)
65
+ } else {
66
+ throw new Error('Check that electron is installed!')
67
+ }
68
+ return electronPath;
69
+ };
70
+
71
+ /**
72
+ * 版本号比较
73
+ */
74
+ function compareVersion(v1, v2) {
75
+ v1 = v1.split('.')
76
+ v2 = v2.split('.')
77
+ const len = Math.max(v1.length, v2.length)
78
+
79
+ while (v1.length < len) {
80
+ v1.push('0')
81
+ }
82
+ while (v2.length < len) {
83
+ v2.push('0')
84
+ }
85
+
86
+ for (let i = 0; i < len; i++) {
87
+ const num1 = parseInt(v1[i])
88
+ const num2 = parseInt(v2[i])
89
+
90
+ if (num1 > num2) {
91
+ return 1
92
+ } else if (num1 < num2) {
93
+ return -1
94
+ }
95
+ }
96
+
97
+ return 0
98
+ }
99
+
100
+ function isWindows(prop) {
101
+ return process.platform === 'win32'
102
+ }
103
+
55
104
  module.exports = {
56
105
  loadConfig,
57
106
  checkConfig,
58
- loadEncryptConfig
107
+ loadEncryptConfig,
108
+ getElectronProgram,
109
+ compareVersion,
110
+ isWindows
59
111
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-bin",
3
- "version": "1.2.0-beta.2",
3
+ "version": "1.2.0-beta.4",
4
4
  "description": "ee bin",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -17,6 +17,7 @@
17
17
  "commander": "^11.0.0",
18
18
  "fs-extra": "^10.0.0",
19
19
  "globby": "^10.0.0",
20
+ "iconv-lite": "^0.6.3",
20
21
  "is-type-of": "^1.2.1",
21
22
  "javascript-obfuscator": "^4.0.2"
22
23
  }
package/tools/encrypt.js CHANGED
@@ -25,7 +25,8 @@ class Encrypt {
25
25
  if (hasConfig) {
26
26
  const cfg = Utils.loadConfig(configFile);
27
27
  this.config = cfg.encrypt;
28
- } else {
28
+ }
29
+ if (!this.config) {
29
30
  this.config = Utils.loadEncryptConfig();
30
31
  }
31
32
 
@@ -4,6 +4,7 @@ const path = require('path');
4
4
  const fs = require('fs');
5
5
  const fsPro = require('fs-extra');
6
6
  const chalk = require('chalk');
7
+ const Utils = require('../lib/utils');
7
8
 
8
9
  /**
9
10
  * 资源替换
@@ -17,11 +18,34 @@ const chalk = require('chalk');
17
18
  run(options = {}) {
18
19
  console.log('[ee-bin] [rd] Start moving resources');
19
20
  const homeDir = process.cwd();
20
- const { distDir, dist, target } = options;
21
+ let { dist, target, config } = options;
21
22
 
22
- // argv
23
- const distFolder = dist ? dist : distDir;
24
- const sourceDir = path.join(homeDir, distFolder);
23
+ let distDir = './frontend/dist';
24
+ let targetDir = './public/dist';
25
+
26
+ // 命令行优先
27
+ if (dist) {
28
+ distDir = dist;
29
+ }
30
+ if (target) {
31
+ targetDir = target;
32
+ }
33
+
34
+ // 如果命令行没参数,从bin config 获取
35
+ if (!dist && !target) {
36
+ const hasConfig = Utils.checkConfig(config);
37
+ if (hasConfig) {
38
+ const cfg = Utils.loadConfig(config);
39
+ if (cfg.rd && cfg.rd.dist) {
40
+ distDir = cfg.rd.dist;
41
+ }
42
+ if (cfg.rd && cfg.rd.target) {
43
+ targetDir = cfg.rd.target;
44
+ }
45
+ }
46
+ }
47
+
48
+ const sourceDir = path.join(homeDir, distDir);
25
49
  if (!fs.existsSync(sourceDir)) {
26
50
  const errorTips = chalk.bgRed('Error') + ' Frontend resource does not exist, please build !';
27
51
  console.error(errorTips);
@@ -29,7 +53,7 @@ const chalk = require('chalk');
29
53
  }
30
54
 
31
55
  // 清空历史资源 并 复制到ee资源目录
32
- const eeResourceDir = path.join(homeDir, target);
56
+ const eeResourceDir = path.join(homeDir, targetDir);
33
57
  if (!fs.existsSync(eeResourceDir)) {
34
58
  fs.mkdirSync(eeResourceDir, {recursive: true, mode: 0o777});
35
59
  } else {
@@ -47,39 +71,10 @@ const chalk = require('chalk');
47
71
  */
48
72
  _rmFolder(folder) {
49
73
  const nodeVersion = (process.versions && process.versions.node) || null;
50
- if (nodeVersion && this._compareVersion(nodeVersion, '14.14.0') == 1) {
74
+ if (nodeVersion && Utils.compareVersion(nodeVersion, '14.14.0') == 1) {
51
75
  fs.rmSync(folder, {recursive: true});
52
76
  } else {
53
77
  fs.rmdirSync(folder, {recursive: true});
54
78
  }
55
79
  },
56
-
57
- /**
58
- * 版本号比较
59
- */
60
- _compareVersion(v1, v2) {
61
- v1 = v1.split('.')
62
- v2 = v2.split('.')
63
- const len = Math.max(v1.length, v2.length)
64
-
65
- while (v1.length < len) {
66
- v1.push('0')
67
- }
68
- while (v2.length < len) {
69
- v2.push('0')
70
- }
71
-
72
- for (let i = 0; i < len; i++) {
73
- const num1 = parseInt(v1[i])
74
- const num2 = parseInt(v2[i])
75
-
76
- if (num1 > num2) {
77
- return 1
78
- } else if (num1 < num2) {
79
- return -1
80
- }
81
- }
82
-
83
- return 0
84
- }
85
80
  }
package/tools/serve.js ADDED
@@ -0,0 +1,156 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const { spawn, exec } = require('child_process');
5
+ const Utils = require('../lib/utils');
6
+ const is = require('is-type-of');
7
+ const chalk = require('chalk');
8
+ const iconv = require('iconv-lite');
9
+ const { Buffer } = require('buffer');
10
+
11
+ module.exports = {
12
+
13
+ frontendProcess: undefined,
14
+
15
+ electronProcess: undefined,
16
+
17
+ /**
18
+ * 启动前端、主进程服务
19
+ */
20
+ dev(options = {}) {
21
+ const { config } = options;
22
+ const cfg = Utils.loadConfig(config);
23
+ const { frontend, electron } = cfg.dev;
24
+ this.frontendServe(frontend);
25
+ this.electronServe(electron);
26
+ },
27
+
28
+ /**
29
+ * 启动主进程服务
30
+ */
31
+ start(options = {}) {
32
+ const { config } = options;
33
+ const cfg = Utils.loadConfig(config);
34
+
35
+ this.electronServe(cfg.start);
36
+ },
37
+
38
+ sleep(ms) {
39
+ return new Promise(resolve => setTimeout(resolve, ms));
40
+ },
41
+
42
+ /**
43
+ * 前端服务
44
+ */
45
+ async frontendServe(cfg) {
46
+ // 如果是 file:// 协议,则不启动
47
+ if (cfg.protocol == 'file://') {
48
+ return
49
+ }
50
+
51
+ // start frontend serve
52
+ console.log(chalk.blue('[ee-bin] [dev] ') + chalk.green('Start the frontend serve...'));
53
+ console.log(chalk.blue('[ee-bin] [dev] ') + chalk.green('config:'), JSON.stringify(cfg));
54
+
55
+ const frontendDir = path.join(process.cwd(), cfg.directory);
56
+ const isWindows = Utils.isWindows();
57
+ const cmdEncoding = isWindows ? 'binary' : 'utf8';
58
+ const msgEncoding = isWindows ? 'cp936' : 'utf8';
59
+ this.frontendProcess = exec(
60
+ cfg.cmd,
61
+ { stdio: 'inherit', cwd: frontendDir, encoding: cmdEncoding},
62
+ (err) => {
63
+ if (err) {
64
+ const errMsg = iconv.decode(new Buffer.from(err.message, cmdEncoding), msgEncoding);
65
+ console.log(chalk.blue('[ee-bin] [dev] ') + chalk.red(`Error: ${errMsg}`))
66
+ process.exit();
67
+ }
68
+ }
69
+ );
70
+
71
+ this.frontendProcess.stdout.on('data', (data) => {
72
+ let out = data;
73
+ if (isWindows) {
74
+ out = iconv.decode(new Buffer.from(data, cmdEncoding), 'utf8');
75
+ }
76
+ console.log(chalk.blue('[ee-bin] [dev] ') + `frontend ${out}`);
77
+ });
78
+ this.frontendProcess.stderr.on('data', (data) => {
79
+ let out = data;
80
+ if (isWindows) {
81
+ out = iconv.decode(new Buffer.from(data, cmdEncoding), 'utf8');
82
+ }
83
+ console.error(chalk.blue('[ee-bin] [dev] ') + `frontend ${out}`);
84
+ });
85
+ },
86
+
87
+ /**
88
+ * 主进程服务
89
+ */
90
+ electronServe(cfg) {
91
+ // start electron serve
92
+ console.log(chalk.blue('[ee-bin] [dev] ') + chalk.green('Start the electron serve...'));
93
+ console.log(chalk.blue('[ee-bin] [dev] ') + chalk.green('config:'), JSON.stringify(cfg));
94
+
95
+ const electronDir = path.join(process.cwd(), cfg.directory);
96
+ const electronArgs = is.string(cfg.args) ? [cfg.args] : cfg.args;
97
+
98
+ // 疑问,为什么直接使用 electron,spawn会报错(或许是衍生shell问题,衍生的shell有系统环境变量)
99
+ let electronProgram
100
+ if (cfg.cmd == 'electron') {
101
+ electronProgram = Utils.getElectronProgram();
102
+ }
103
+
104
+ this.electronProcess = spawn(
105
+ electronProgram,
106
+ electronArgs,
107
+ {stdio: 'inherit', cwd: electronDir,}
108
+ );
109
+
110
+ this.electronProcess.on('exit', () => {
111
+ setTimeout(() => {
112
+ process.exit();
113
+ }, 500)
114
+ });
115
+ },
116
+
117
+ /**
118
+ * 构建前端 dist
119
+ */
120
+ build(options = {}) {
121
+ const { config } = options;
122
+ const cfg = Utils.loadConfig(config);
123
+ const buildCfg = cfg.build;
124
+
125
+ // start build frontend dist
126
+ console.log(chalk.blue('[ee-bin] [build] ') + chalk.green('Build frontend dist'));
127
+ console.log(chalk.blue('[ee-bin] [build] ') + chalk.green('config:'), buildCfg);
128
+
129
+ let i = 1;
130
+ let buildProgress = setInterval(() => {
131
+ console.log(chalk.blue('[ee-bin] [build] ') + `${i}s`);
132
+ i++;
133
+ }, 1000)
134
+
135
+ const frontendDir = path.join(process.cwd(), buildCfg.directory);
136
+ const buildProcess = exec(
137
+ buildCfg.cmd,
138
+ { stdio: 'inherit', cwd: frontendDir, }, // maxBuffer: 1024 * 1024 * 1024
139
+ (err) => {
140
+ if (err) {
141
+ console.log(chalk.blue('[ee-bin] [build] ') + chalk.red(`Error: ${err.message}`))
142
+ process.exit();
143
+ }
144
+ clearInterval(buildProgress);
145
+ console.log(chalk.blue('[ee-bin] [build] ') + chalk.green('End'));
146
+ }
147
+ );
148
+
149
+ buildProcess.stdout.on('data', (data) => {
150
+ console.log(chalk.blue('[ee-bin] [build] ') + `frontend ${data}`);
151
+ });
152
+ buildProcess.stderr.on('data', (data) => {
153
+ console.error(chalk.blue('[ee-bin] [build] ') + chalk.yellow(`Warning: ${data}`));
154
+ });
155
+ },
156
+ }
package/tools/frontend.js DELETED
@@ -1,82 +0,0 @@
1
- 'use strict';
2
-
3
- const path = require('path');
4
- const fs = require('fs');
5
- const { spawn, exec } = require('child_process');
6
- const Utils = require('../lib/utils');
7
- const is = require('is-type-of');
8
- const chalk = require('chalk');
9
-
10
- module.exports = {
11
-
12
- /**
13
- * 启动前端、主进程服务
14
- */
15
- serve(options = {}) {
16
- const { config } = options;
17
- const cfg = Utils.loadConfig(config);
18
- const { frontend, main } = cfg;
19
-
20
- // start frontend serve
21
- console.log(chalk.blue('[ee-bin] [serve] ') + chalk.green('Start the frontend serve...'));
22
- const frontendDir = path.join(process.cwd(), frontend.directory);
23
- exec(frontend.devCommond, { stdio: 'inherit', cwd: frontendDir});
24
-
25
- // start electron serve
26
- console.log(chalk.blue('[ee-bin] [serve] ') + chalk.green('Start the electron serve...'));
27
- const mainDir = path.join(process.cwd(), main.directory);
28
- const mainArgs = is.string(main.args) ? [main.args] : main.args;
29
- const electronPath = this._getElectronPath();
30
- spawn(electronPath, mainArgs, {
31
- stdio: 'inherit',
32
- cwd: mainDir,
33
- });
34
- },
35
-
36
- /**
37
- * 构建前端 dist
38
- */
39
- build(options = {}) {
40
- const { config } = options;
41
- const cfg = Utils.loadConfig(config);
42
- const { frontend } = cfg;
43
-
44
- // start build frontend dist
45
- console.log(chalk.blue('[ee-bin] [build] ') + chalk.green('Build frontend dist'));
46
- let i = 1;
47
- let buildProgress = setInterval(() => {
48
- console.log(chalk.blue('[ee-bin] [build] ') + chalk.magenta(`${i}s`));
49
- i++;
50
- }, 1000)
51
-
52
- const frontendDir = path.join(process.cwd(), frontend.directory);
53
- exec(
54
- frontend.buildCommond,
55
- { stdio: 'inherit', cwd: frontendDir, maxBuffer: 1024 * 1024 * 1024},
56
- (error, stdout, stderr) => {
57
- if (error) {
58
- console.log(chalk.red('build error:') + error);
59
- return;
60
- }
61
- console.log(stdout);
62
- console.log(stderr);
63
- clearInterval(buildProgress);
64
- console.log(chalk.blue('[ee-bin] [build] ') + chalk.green('End'));
65
- }
66
- );
67
- },
68
-
69
- _getElectronPath() {
70
- let electronExecPath = ''
71
- const electronModulePath = path.dirname(require.resolve('electron'))
72
- const pathFile = path.join(electronModulePath, 'path.txt')
73
- const executablePath = fs.readFileSync(pathFile, 'utf-8')
74
- if (executablePath) {
75
- electronExecPath = path.join(electronModulePath, 'dist', executablePath)
76
- } else {
77
- throw new Error('Electron uninstall')
78
- }
79
- return electronExecPath
80
- },
81
-
82
- }