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

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
@@ -8,7 +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')
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')
12
14
  .action(function() {
13
15
  const replaceDist = require('./tools/replaceDist');
14
16
  replaceDist.run(this.opts());
@@ -20,7 +22,7 @@ program
20
22
  program
21
23
  .command('encrypt')
22
24
  .description('Code encryption')
23
- .option('--config <folder>', 'config file', './electron/config/encrypt.js')
25
+ .option('--config <folder>', 'config file')
24
26
  .option('--out <folder>', 'output directory', './public')
25
27
  .action(function() {
26
28
  const encrypt = require('./tools/encrypt');
@@ -62,4 +64,16 @@ program
62
64
  frontend.serve(this.opts());
63
65
  });
64
66
 
67
+ /**
68
+ * build
69
+ */
70
+ program
71
+ .command('build')
72
+ .description('build frontend dist')
73
+ .option('--config <folder>', 'config file', './electron/config/bin.js')
74
+ .action(function() {
75
+ const frontend = require('./tools/frontend');
76
+ frontend.build(this.opts());
77
+ });
78
+
65
79
  program.parse();
package/lib/utils.js CHANGED
@@ -7,8 +7,35 @@ const is = require('is-type-of');
7
7
 
8
8
  const _basePath = process.cwd();
9
9
 
10
+ function checkConfig(prop) {
11
+ const filepath = path.join(_basePath, prop);
12
+ if (fs.existsSync(filepath)) {
13
+ return true;
14
+ }
15
+
16
+ return false;
17
+ }
18
+
10
19
  function loadConfig(prop) {
11
- const configFile = prop || './electron/config/bin.js';
20
+ const configFile = prop;
21
+ const filepath = path.join(_basePath, configFile);
22
+ if (!fs.existsSync(filepath)) {
23
+ const errorTips = 'config file ' + chalk.blue(`${filepath}`) + ' does not exist !';
24
+ throw new Error(errorTips)
25
+ }
26
+ const obj = require(filepath);
27
+ if (!obj) return obj;
28
+
29
+ let ret = obj;
30
+ if (is.function(obj) && !is.class(obj)) {
31
+ ret = obj();
32
+ }
33
+
34
+ return ret || {};
35
+ };
36
+
37
+ function loadEncryptConfig() {
38
+ const configFile = './electron/config/encrypt.js';
12
39
  const filepath = path.join(_basePath, configFile);
13
40
  if (!fs.existsSync(filepath)) {
14
41
  const errorTips = 'config file ' + chalk.blue(`${filepath}`) + ' does not exist !';
@@ -26,5 +53,7 @@ function loadConfig(prop) {
26
53
  };
27
54
 
28
55
  module.exports = {
29
- loadConfig
56
+ loadConfig,
57
+ checkConfig,
58
+ loadEncryptConfig
30
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-bin",
3
- "version": "1.2.0-beta.1",
3
+ "version": "1.2.0-beta.2",
4
4
  "description": "ee bin",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/tools/encrypt.js CHANGED
@@ -9,16 +9,26 @@ const crypto = require('crypto');
9
9
  const JavaScriptObfuscator = require('javascript-obfuscator');
10
10
  const globby = require('globby');
11
11
  const chalk = require('chalk');
12
+ const Utils = require('../lib/utils');
12
13
 
13
14
  class Encrypt {
14
15
  constructor(options = {}) {
15
16
  // cli args
16
17
  const outputFolder = options.out || './public';
17
- const configFile = options.config || './electron/config/encrypt.js';
18
+ const configFile = options.config || './electron/config/bin.js';
18
19
 
19
20
  this.basePath = process.cwd();
20
21
  this.encryptCodeDir = path.join(this.basePath, outputFolder);
21
- this.config = this.loadConfig(configFile);
22
+
23
+ // 先从 bin config获取,没有的话从 config/encrypt.js
24
+ const hasConfig = Utils.checkConfig(configFile);
25
+ if (hasConfig) {
26
+ const cfg = Utils.loadConfig(configFile);
27
+ this.config = cfg.encrypt;
28
+ } else {
29
+ this.config = Utils.loadEncryptConfig();
30
+ }
31
+
22
32
  this.filesExt = this.config.fileExt || ['.js'];
23
33
  this.type = this.config.type || 'confusion';
24
34
  this.bOpt = this.config.bytecodeOptions || {};
@@ -38,8 +48,7 @@ class Encrypt {
38
48
  }
39
49
 
40
50
  this.codefiles = this._initCodeFiles();
41
- //console.log('[ee-core] [encrypt] codefiles:', this.codefiles);
42
- console.log('[ee-core] [encrypt] cleanFiles:', this.cleanFiles);
51
+ console.log(chalk.blue('[ee-bin] [encrypt] ') + 'cleanFiles:' + this.cleanFiles);
43
52
  }
44
53
 
45
54
  /**
@@ -59,7 +68,7 @@ class Encrypt {
59
68
  // clean
60
69
  this.cleanCode();
61
70
 
62
- console.log('[ee-core] [encrypt] backup start');
71
+ console.log(chalk.blue('[ee-bin] [encrypt] ') + 'backup start');
63
72
  if (this.patterns) {
64
73
  this.codefiles.forEach((filepath) => {
65
74
  let source = path.join(this.basePath, filepath);
@@ -74,13 +83,13 @@ class Encrypt {
74
83
  // check code dir
75
84
  let codeDirPath = path.join(this.basePath, this.dirs[i]);
76
85
  if (!fs.existsSync(codeDirPath)) {
77
- console.log('[ee-core] [encrypt] ERROR: backup %s is not exist', codeDirPath);
86
+ console.log('[ee-bin] [encrypt] ERROR: backup %s is not exist', codeDirPath);
78
87
  return
79
88
  }
80
89
 
81
90
  // copy
82
91
  let targetDir = path.join(this.encryptCodeDir, this.dirs[i]);
83
- console.log('[ee-core] [encrypt] backup target Dir:', targetDir);
92
+ console.log('[ee-bin] [encrypt] backup target Dir:', targetDir);
84
93
  if (!fs.existsSync(targetDir)) {
85
94
  this.mkdir(targetDir);
86
95
  this.chmodPath(targetDir, '777');
@@ -90,7 +99,7 @@ class Encrypt {
90
99
  }
91
100
  }
92
101
 
93
- console.log('[ee-core] [encrypt] backup end');
102
+ console.log(chalk.blue('[ee-bin] [encrypt] ') + 'backup end');
94
103
  return true;
95
104
  }
96
105
 
@@ -101,7 +110,7 @@ class Encrypt {
101
110
  this.cleanFiles.forEach((file) => {
102
111
  let tmpFile = path.join(this.basePath, file);
103
112
  this.rmBackup(tmpFile);
104
- console.log('[ee-core] [encrypt] clean up tmp files:', tmpFile);
113
+ console.log(chalk.blue('[ee-bin] [encrypt] ') + 'clean up tmp files:' + chalk.magenta(`${tmpFile}`));
105
114
  })
106
115
  }
107
116
 
@@ -109,7 +118,7 @@ class Encrypt {
109
118
  * 加密代码
110
119
  */
111
120
  encrypt() {
112
- console.log('[ee-core] [encrypt] start ciphering');
121
+ console.log(chalk.blue('[ee-bin] [encrypt] ') + 'start ciphering');
113
122
  if (this.patterns) {
114
123
  for (const file of this.codefiles) {
115
124
  const fullpath = path.join(this.encryptCodeDir, file);
@@ -125,15 +134,15 @@ class Encrypt {
125
134
  }
126
135
  } else {
127
136
  // 旧逻辑,将废弃
128
- console.log('[ee-core] [encrypt] !!!!!! please use the new encryption method !!!!!!');
137
+ console.log('[ee-bin] [encrypt] !!!!!! please use the new encryption method !!!!!!');
129
138
  for (let i = 0; i < this.dirs.length; i++) {
130
139
  let codeDirPath = path.join(this.encryptCodeDir, this.dirs[i]);
131
140
  this.loop(codeDirPath);
132
141
  }
133
- console.log('[ee-core] [encrypt] !!!!!! please use the new encryption method !!!!!!');
142
+ console.log('[ee-bin] [encrypt] !!!!!! please use the new encryption method !!!!!!');
134
143
  }
135
144
 
136
- console.log('[ee-core] [encrypt] end ciphering');
145
+ console.log(chalk.blue('[ee-bin] [encrypt] ') + 'end ciphering');
137
146
  };
138
147
 
139
148
  /**
@@ -163,7 +172,7 @@ class Encrypt {
163
172
  generate(curPath, type) {
164
173
  let encryptType = type ? type : this.type;
165
174
 
166
- let tips = '[ee-core] [encrypt] file: ' + chalk.green(`${curPath}`) + ' ' + chalk.blue(`(${encryptType})`);
175
+ let tips = chalk.blue('[ee-bin] [encrypt] ') + 'file: ' + chalk.green(`${curPath}`) + ' ' + chalk.cyan(`(${encryptType})`);
167
176
  console.log(tips);
168
177
 
169
178
  if (encryptType == 'bytecode') {
@@ -272,23 +281,6 @@ class Encrypt {
272
281
  }
273
282
  };
274
283
 
275
- loadConfig(prop) {
276
- const filepath = path.join(this.basePath, prop);
277
- if (!fs.existsSync(filepath)) {
278
- const errorTips = 'config file ' + chalk.blue(`${filepath}`) + ' does not exist !';
279
- throw new Error(errorTips)
280
- }
281
- const obj = require(filepath);
282
- if (!obj) return obj;
283
-
284
- let ret = obj;
285
- if (is.function(obj) && !is.class(obj)) {
286
- ret = obj();
287
- }
288
-
289
- return ret || {};
290
- };
291
-
292
284
  md5(file) {
293
285
  const buffer = fs.readFileSync(file);
294
286
  const hash = crypto.createHash('md5');
@@ -312,7 +304,7 @@ const clean = (options = {}) => {
312
304
  const tmpFile = path.join(process.cwd(), file);
313
305
  if (fs.existsSync(tmpFile)) {
314
306
  fsPro.removeSync(tmpFile);
315
- console.log('[ee-core] [encrypt] clean up tmp files:', tmpFile);
307
+ console.log(chalk.blue('[ee-bin] [encrypt] ') + 'clean up tmp files: ' + chalk.magenta(`${tmpFile}`));
316
308
  }
317
309
  })
318
310
  }
package/tools/frontend.js CHANGED
@@ -2,70 +2,75 @@
2
2
 
3
3
  const path = require('path');
4
4
  const fs = require('fs');
5
- const { spawn, spawnSync, exec, execFile } = require('child_process');
5
+ const { spawn, exec } = require('child_process');
6
6
  const Utils = require('../lib/utils');
7
7
  const is = require('is-type-of');
8
+ const chalk = require('chalk');
8
9
 
9
10
  module.exports = {
10
11
 
11
- electronServer: undefined,
12
-
13
- frontendServer: undefined,
14
-
15
12
  /**
16
13
  * 启动前端、主进程服务
17
14
  */
18
15
  serve(options = {}) {
19
16
  const { config } = options;
20
17
  const cfg = Utils.loadConfig(config);
21
-
22
18
  const { frontend, main } = cfg;
23
- console.log('frontend:', frontend);
24
- console.log('main:', main);
25
19
 
26
- // const frontendDir = path.join(process.cwd(), frontend.directory);
27
- // const frontendArgs = is.string(frontend.args) ? [frontend.args] : frontend.args;
28
- // console.log('frontendDir:', frontendDir);
29
- // console.log('frontendArgs:', frontendArgs);
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});
30
24
 
31
- //['--host --port 8080'], ['--host', '--port 8080'],
32
- this.frontendServer = spawnSync(
33
- 'vite',
34
- ['--host', '--port=8080'],
35
- {
36
- stdio: 'inherit',
37
- cwd: path.join(process.cwd(), 'frontend'),
38
- shell: true,
39
- }
40
- );
41
- console.log('this.frontendServer:', this.frontendServer);
42
- // todo execSync衍生了shell无法找到cmd
43
- // this.frontendServer = execSync(frontend.exec, {stdio: 'inherit', cwd: frontendDir});
44
- // spawnSync(
45
- // frontend.cmd,
46
- // frontendArgs,
47
- // {
48
- // stdio: 'inherit',
49
- // cwd: frontendDir
50
- // }
51
- // );
52
-
25
+ // start electron serve
26
+ console.log(chalk.blue('[ee-bin] [serve] ') + chalk.green('Start the electron serve...'));
53
27
  const mainDir = path.join(process.cwd(), main.directory);
54
28
  const mainArgs = is.string(main.args) ? [main.args] : main.args;
55
29
  const electronPath = this._getElectronPath();
56
- this.electronServer = spawn(electronPath, mainArgs, { stdio: 'inherit' });
57
-
58
- this._init();
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
+ );
59
67
  },
60
68
 
61
69
  _getElectronPath() {
62
70
  let electronExecPath = ''
63
71
  const electronModulePath = path.dirname(require.resolve('electron'))
64
72
  const pathFile = path.join(electronModulePath, 'path.txt')
65
- let executablePath
66
- if (fs.existsSync(pathFile)) {
67
- executablePath = fs.readFileSync(pathFile, 'utf-8')
68
- }
73
+ const executablePath = fs.readFileSync(pathFile, 'utf-8')
69
74
  if (executablePath) {
70
75
  electronExecPath = path.join(electronModulePath, 'dist', executablePath)
71
76
  } else {
@@ -73,28 +78,5 @@ module.exports = {
73
78
  }
74
79
  return electronExecPath
75
80
  },
76
-
77
- /**
78
- * 事件监听
79
- */
80
- _init() {
81
- // this.frontendServer.on('data', (data) => {
82
- // console.log(`[ee-bin] [serve] frontend-server data:${data}`);
83
- // });
84
- // this.frontendServer.on('exit', (code, signal) => {
85
- // console.log(`[ee-bin] [serve] frontend-server code:${code}, signal:${signal}`);
86
- // });
87
-
88
- // this.frontendServer.on('error', (err) => {
89
- // console.log(`[ee-bin] [serve] frontendServer error: ${err}`);
90
- // });
91
-
92
- // this.electronServer.on('exit', (code, signal) => {
93
- // console.log(`[ee-bin] [serve] electronServer code:${code}, signal:${signal}`);
94
- // });
95
-
96
- // this.electronServer.on('error', (err) => {
97
- // console.log(`[ee-bin] [serve] electronServer error: ${err}`);
98
- // });
99
- }
81
+
100
82
  }
@@ -17,42 +17,31 @@ const chalk = require('chalk');
17
17
  run(options = {}) {
18
18
  console.log('[ee-bin] [rd] Start moving resources');
19
19
  const homeDir = process.cwd();
20
+ const { distDir, dist, target } = options;
20
21
 
21
22
  // argv
22
- const distDir = options.distDir;
23
- const sourceDir = path.join(homeDir, distDir);
24
- const sourceIndexFile = path.join(sourceDir, 'index.html');
25
-
26
- if (!this._fileExist(sourceIndexFile)) {
27
- console.info(sourceIndexFile);
23
+ const distFolder = dist ? dist : distDir;
24
+ const sourceDir = path.join(homeDir, distFolder);
25
+ if (!fs.existsSync(sourceDir)) {
28
26
  const errorTips = chalk.bgRed('Error') + ' Frontend resource does not exist, please build !';
29
27
  console.error(errorTips);
30
28
  return
31
29
  }
32
30
 
33
31
  // 清空历史资源 并 复制到ee资源目录
34
- const eeResourceDir = path.join(homeDir, 'public', 'dist');
32
+ const eeResourceDir = path.join(homeDir, target);
35
33
  if (!fs.existsSync(eeResourceDir)) {
36
34
  fs.mkdirSync(eeResourceDir, {recursive: true, mode: 0o777});
35
+ } else {
36
+ this._rmFolder(eeResourceDir);
37
+ console.log('[ee-bin] [rd] Clear history resources:', eeResourceDir);
37
38
  }
38
- this._rmFolder(eeResourceDir);
39
- console.log('[ee-bin] [rd] Clear history resources:', eeResourceDir);
40
39
 
41
40
  fsPro.copySync(sourceDir, eeResourceDir);
42
41
  console.log('[ee-bin] [rd] Copy a resource to:', eeResourceDir);
43
42
  console.log('[ee-bin] [rd] End');
44
43
  },
45
44
 
46
- _fileExist(filePath) {
47
- try {
48
- return fs.statSync(filePath).isFile();
49
- } catch (err) {
50
- // const errorTips = chalk.bgRed('Error') + ' [ee-bin] [rd] ';
51
- // console.error(errorTips, err);
52
- return false;
53
- }
54
- },
55
-
56
45
  /**
57
46
  * 删除文件夹
58
47
  */