ee-bin 1.2.0-bete.1 → 1.3.0-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.
- package/index.js +47 -7
- package/lib/utils.js +83 -2
- package/package.json +2 -1
- package/tools/encrypt.js +25 -32
- package/tools/replaceDist.js +33 -49
- package/tools/serve.js +165 -0
- package/tools/frontend.js +0 -100
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,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('--
|
|
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')
|
|
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'
|
|
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');
|
|
@@ -55,11 +57,49 @@ program
|
|
|
55
57
|
*/
|
|
56
58
|
program
|
|
57
59
|
.command('dev')
|
|
58
|
-
.description('create frontend-
|
|
60
|
+
.description('create frontend-serve and electron-serve')
|
|
61
|
+
.option('--config <folder>', 'config file', './electron/config/bin.js')
|
|
62
|
+
.option('--serve <mode>', 'serve mode')
|
|
63
|
+
.action(function() {
|
|
64
|
+
const serve = require('./tools/serve');
|
|
65
|
+
serve.dev(this.opts());
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* build
|
|
70
|
+
*/
|
|
71
|
+
program
|
|
72
|
+
.command('build')
|
|
73
|
+
.description('build frontend dist')
|
|
74
|
+
.option('--config <folder>', 'config file', './electron/config/bin.js')
|
|
75
|
+
.action(function() {
|
|
76
|
+
const serve = require('./tools/serve');
|
|
77
|
+
serve.build(this.opts());
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* start
|
|
82
|
+
*/
|
|
83
|
+
program
|
|
84
|
+
.command('start')
|
|
85
|
+
.description('preview effect')
|
|
86
|
+
.option('--config <folder>', 'config file', './electron/config/bin.js')
|
|
87
|
+
.action(function() {
|
|
88
|
+
const serve = require('./tools/serve');
|
|
89
|
+
serve.start(this.opts());
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* exec
|
|
94
|
+
*/
|
|
95
|
+
program
|
|
96
|
+
.command('exec')
|
|
97
|
+
.description('create frontend-serve and electron-serve')
|
|
59
98
|
.option('--config <folder>', 'config file', './electron/config/bin.js')
|
|
99
|
+
.option('--command <command>', 'Custom command')
|
|
60
100
|
.action(function() {
|
|
61
|
-
const
|
|
62
|
-
|
|
101
|
+
const serve = require('./tools/serve');
|
|
102
|
+
serve.exec(this.opts());
|
|
63
103
|
});
|
|
64
104
|
|
|
65
|
-
program.parse();
|
|
105
|
+
program.parse();
|
package/lib/utils.js
CHANGED
|
@@ -7,8 +7,17 @@ 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
|
|
20
|
+
const configFile = prop;
|
|
12
21
|
const filepath = path.join(_basePath, configFile);
|
|
13
22
|
if (!fs.existsSync(filepath)) {
|
|
14
23
|
const errorTips = 'config file ' + chalk.blue(`${filepath}`) + ' does not exist !';
|
|
@@ -25,6 +34,78 @@ function loadConfig(prop) {
|
|
|
25
34
|
return ret || {};
|
|
26
35
|
};
|
|
27
36
|
|
|
37
|
+
function loadEncryptConfig() {
|
|
38
|
+
const configFile = './electron/config/encrypt.js';
|
|
39
|
+
const filepath = path.join(_basePath, configFile);
|
|
40
|
+
if (!fs.existsSync(filepath)) {
|
|
41
|
+
const errorTips = 'config file ' + chalk.blue(`${filepath}`) + ' does not exist !';
|
|
42
|
+
throw new Error(errorTips)
|
|
43
|
+
}
|
|
44
|
+
const obj = require(filepath);
|
|
45
|
+
if (!obj) return obj;
|
|
46
|
+
|
|
47
|
+
let ret = obj;
|
|
48
|
+
if (is.function(obj) && !is.class(obj)) {
|
|
49
|
+
ret = obj();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return ret || {};
|
|
53
|
+
};
|
|
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
|
+
|
|
28
104
|
module.exports = {
|
|
29
|
-
loadConfig
|
|
105
|
+
loadConfig,
|
|
106
|
+
checkConfig,
|
|
107
|
+
loadEncryptConfig,
|
|
108
|
+
getElectronProgram,
|
|
109
|
+
compareVersion,
|
|
110
|
+
isWindows
|
|
30
111
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ee-bin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0-beta.1",
|
|
4
4
|
"description": "ee bin",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"bytenode": "^1.3.6",
|
|
16
16
|
"chalk": "^4.1.2",
|
|
17
17
|
"commander": "^11.0.0",
|
|
18
|
+
"cross-spawn": "^7.0.3",
|
|
18
19
|
"fs-extra": "^10.0.0",
|
|
19
20
|
"globby": "^10.0.0",
|
|
20
21
|
"is-type-of": "^1.2.1",
|
package/tools/encrypt.js
CHANGED
|
@@ -9,16 +9,27 @@ 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/
|
|
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
|
-
|
|
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
|
+
}
|
|
29
|
+
if (!this.config) {
|
|
30
|
+
this.config = Utils.loadEncryptConfig();
|
|
31
|
+
}
|
|
32
|
+
|
|
22
33
|
this.filesExt = this.config.fileExt || ['.js'];
|
|
23
34
|
this.type = this.config.type || 'confusion';
|
|
24
35
|
this.bOpt = this.config.bytecodeOptions || {};
|
|
@@ -38,8 +49,7 @@ class Encrypt {
|
|
|
38
49
|
}
|
|
39
50
|
|
|
40
51
|
this.codefiles = this._initCodeFiles();
|
|
41
|
-
|
|
42
|
-
console.log('[ee-core] [encrypt] cleanFiles:', this.cleanFiles);
|
|
52
|
+
console.log(chalk.blue('[ee-bin] [encrypt] ') + 'cleanFiles:' + this.cleanFiles);
|
|
43
53
|
}
|
|
44
54
|
|
|
45
55
|
/**
|
|
@@ -59,7 +69,7 @@ class Encrypt {
|
|
|
59
69
|
// clean
|
|
60
70
|
this.cleanCode();
|
|
61
71
|
|
|
62
|
-
console.log('[ee-
|
|
72
|
+
console.log(chalk.blue('[ee-bin] [encrypt] ') + 'backup start');
|
|
63
73
|
if (this.patterns) {
|
|
64
74
|
this.codefiles.forEach((filepath) => {
|
|
65
75
|
let source = path.join(this.basePath, filepath);
|
|
@@ -74,13 +84,13 @@ class Encrypt {
|
|
|
74
84
|
// check code dir
|
|
75
85
|
let codeDirPath = path.join(this.basePath, this.dirs[i]);
|
|
76
86
|
if (!fs.existsSync(codeDirPath)) {
|
|
77
|
-
console.log('[ee-
|
|
87
|
+
console.log('[ee-bin] [encrypt] ERROR: backup %s is not exist', codeDirPath);
|
|
78
88
|
return
|
|
79
89
|
}
|
|
80
90
|
|
|
81
91
|
// copy
|
|
82
92
|
let targetDir = path.join(this.encryptCodeDir, this.dirs[i]);
|
|
83
|
-
console.log('[ee-
|
|
93
|
+
console.log('[ee-bin] [encrypt] backup target Dir:', targetDir);
|
|
84
94
|
if (!fs.existsSync(targetDir)) {
|
|
85
95
|
this.mkdir(targetDir);
|
|
86
96
|
this.chmodPath(targetDir, '777');
|
|
@@ -90,7 +100,7 @@ class Encrypt {
|
|
|
90
100
|
}
|
|
91
101
|
}
|
|
92
102
|
|
|
93
|
-
console.log('[ee-
|
|
103
|
+
console.log(chalk.blue('[ee-bin] [encrypt] ') + 'backup end');
|
|
94
104
|
return true;
|
|
95
105
|
}
|
|
96
106
|
|
|
@@ -101,7 +111,7 @@ class Encrypt {
|
|
|
101
111
|
this.cleanFiles.forEach((file) => {
|
|
102
112
|
let tmpFile = path.join(this.basePath, file);
|
|
103
113
|
this.rmBackup(tmpFile);
|
|
104
|
-
console.log('[ee-
|
|
114
|
+
console.log(chalk.blue('[ee-bin] [encrypt] ') + 'clean up tmp files:' + chalk.magenta(`${tmpFile}`));
|
|
105
115
|
})
|
|
106
116
|
}
|
|
107
117
|
|
|
@@ -109,7 +119,7 @@ class Encrypt {
|
|
|
109
119
|
* 加密代码
|
|
110
120
|
*/
|
|
111
121
|
encrypt() {
|
|
112
|
-
console.log('[ee-
|
|
122
|
+
console.log(chalk.blue('[ee-bin] [encrypt] ') + 'start ciphering');
|
|
113
123
|
if (this.patterns) {
|
|
114
124
|
for (const file of this.codefiles) {
|
|
115
125
|
const fullpath = path.join(this.encryptCodeDir, file);
|
|
@@ -125,15 +135,15 @@ class Encrypt {
|
|
|
125
135
|
}
|
|
126
136
|
} else {
|
|
127
137
|
// 旧逻辑,将废弃
|
|
128
|
-
console.log('[ee-
|
|
138
|
+
console.log('[ee-bin] [encrypt] !!!!!! please use the new encryption method !!!!!!');
|
|
129
139
|
for (let i = 0; i < this.dirs.length; i++) {
|
|
130
140
|
let codeDirPath = path.join(this.encryptCodeDir, this.dirs[i]);
|
|
131
141
|
this.loop(codeDirPath);
|
|
132
142
|
}
|
|
133
|
-
console.log('[ee-
|
|
143
|
+
console.log('[ee-bin] [encrypt] !!!!!! please use the new encryption method !!!!!!');
|
|
134
144
|
}
|
|
135
145
|
|
|
136
|
-
console.log('[ee-
|
|
146
|
+
console.log(chalk.blue('[ee-bin] [encrypt] ') + 'end ciphering');
|
|
137
147
|
};
|
|
138
148
|
|
|
139
149
|
/**
|
|
@@ -163,7 +173,7 @@ class Encrypt {
|
|
|
163
173
|
generate(curPath, type) {
|
|
164
174
|
let encryptType = type ? type : this.type;
|
|
165
175
|
|
|
166
|
-
let tips = '[ee-
|
|
176
|
+
let tips = chalk.blue('[ee-bin] [encrypt] ') + 'file: ' + chalk.green(`${curPath}`) + ' ' + chalk.cyan(`(${encryptType})`);
|
|
167
177
|
console.log(tips);
|
|
168
178
|
|
|
169
179
|
if (encryptType == 'bytecode') {
|
|
@@ -272,23 +282,6 @@ class Encrypt {
|
|
|
272
282
|
}
|
|
273
283
|
};
|
|
274
284
|
|
|
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
285
|
md5(file) {
|
|
293
286
|
const buffer = fs.readFileSync(file);
|
|
294
287
|
const hash = crypto.createHash('md5');
|
|
@@ -312,7 +305,7 @@ const clean = (options = {}) => {
|
|
|
312
305
|
const tmpFile = path.join(process.cwd(), file);
|
|
313
306
|
if (fs.existsSync(tmpFile)) {
|
|
314
307
|
fsPro.removeSync(tmpFile);
|
|
315
|
-
console.log('[ee-
|
|
308
|
+
console.log(chalk.blue('[ee-bin] [encrypt] ') + 'clean up tmp files: ' + chalk.magenta(`${tmpFile}`));
|
|
316
309
|
}
|
|
317
310
|
})
|
|
318
311
|
}
|
package/tools/replaceDist.js
CHANGED
|
@@ -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,80 +18,63 @@ const chalk = require('chalk');
|
|
|
17
18
|
run(options = {}) {
|
|
18
19
|
console.log('[ee-bin] [rd] Start moving resources');
|
|
19
20
|
const homeDir = process.cwd();
|
|
21
|
+
let { dist, target, config } = options;
|
|
22
|
+
|
|
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
|
+
}
|
|
20
47
|
|
|
21
|
-
// argv
|
|
22
|
-
const distDir = options.distDir;
|
|
23
48
|
const sourceDir = path.join(homeDir, distDir);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (!this._fileExist(sourceIndexFile)) {
|
|
27
|
-
console.info(sourceIndexFile);
|
|
49
|
+
if (!fs.existsSync(sourceDir)) {
|
|
28
50
|
const errorTips = chalk.bgRed('Error') + ' Frontend resource does not exist, please build !';
|
|
29
51
|
console.error(errorTips);
|
|
30
52
|
return
|
|
31
53
|
}
|
|
32
54
|
|
|
33
55
|
// 清空历史资源 并 复制到ee资源目录
|
|
34
|
-
const eeResourceDir = path.join(homeDir,
|
|
56
|
+
const eeResourceDir = path.join(homeDir, targetDir);
|
|
35
57
|
if (!fs.existsSync(eeResourceDir)) {
|
|
36
58
|
fs.mkdirSync(eeResourceDir, {recursive: true, mode: 0o777});
|
|
59
|
+
} else {
|
|
60
|
+
this._rmFolder(eeResourceDir);
|
|
61
|
+
console.log('[ee-bin] [rd] Clear history resources:', eeResourceDir);
|
|
37
62
|
}
|
|
38
|
-
this._rmFolder(eeResourceDir);
|
|
39
|
-
console.log('[ee-bin] [rd] Clear history resources:', eeResourceDir);
|
|
40
63
|
|
|
41
64
|
fsPro.copySync(sourceDir, eeResourceDir);
|
|
42
65
|
console.log('[ee-bin] [rd] Copy a resource to:', eeResourceDir);
|
|
43
66
|
console.log('[ee-bin] [rd] End');
|
|
44
67
|
},
|
|
45
68
|
|
|
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
69
|
/**
|
|
57
70
|
* 删除文件夹
|
|
58
71
|
*/
|
|
59
72
|
_rmFolder(folder) {
|
|
60
73
|
const nodeVersion = (process.versions && process.versions.node) || null;
|
|
61
|
-
if (nodeVersion &&
|
|
74
|
+
if (nodeVersion && Utils.compareVersion(nodeVersion, '14.14.0') == 1) {
|
|
62
75
|
fs.rmSync(folder, {recursive: true});
|
|
63
76
|
} else {
|
|
64
77
|
fs.rmdirSync(folder, {recursive: true});
|
|
65
78
|
}
|
|
66
79
|
},
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* 版本号比较
|
|
70
|
-
*/
|
|
71
|
-
_compareVersion(v1, v2) {
|
|
72
|
-
v1 = v1.split('.')
|
|
73
|
-
v2 = v2.split('.')
|
|
74
|
-
const len = Math.max(v1.length, v2.length)
|
|
75
|
-
|
|
76
|
-
while (v1.length < len) {
|
|
77
|
-
v1.push('0')
|
|
78
|
-
}
|
|
79
|
-
while (v2.length < len) {
|
|
80
|
-
v2.push('0')
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
for (let i = 0; i < len; i++) {
|
|
84
|
-
const num1 = parseInt(v1[i])
|
|
85
|
-
const num2 = parseInt(v2[i])
|
|
86
|
-
|
|
87
|
-
if (num1 > num2) {
|
|
88
|
-
return 1
|
|
89
|
-
} else if (num1 < num2) {
|
|
90
|
-
return -1
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return 0
|
|
95
|
-
}
|
|
96
80
|
}
|
package/tools/serve.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const Utils = require('../lib/utils');
|
|
5
|
+
const is = require('is-type-of');
|
|
6
|
+
const chalk = require('chalk');
|
|
7
|
+
const crossSpawn = require('cross-spawn');
|
|
8
|
+
|
|
9
|
+
module.exports = {
|
|
10
|
+
|
|
11
|
+
frontendProcess: undefined,
|
|
12
|
+
|
|
13
|
+
electronProcess: undefined,
|
|
14
|
+
|
|
15
|
+
execProcess: {},
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 启动前端、主进程服务
|
|
19
|
+
*/
|
|
20
|
+
dev(options = {}) {
|
|
21
|
+
const { config, serve } = options;
|
|
22
|
+
const binCfg = Utils.loadConfig(config);
|
|
23
|
+
const { frontend, electron } = binCfg.dev;
|
|
24
|
+
|
|
25
|
+
if (serve == 'frontend') {
|
|
26
|
+
this.frontendServe(frontend);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (serve == 'electron') {
|
|
31
|
+
this.electronServe(electron);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
this.frontendServe(frontend);
|
|
36
|
+
this.electronServe(electron);
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 启动主进程服务
|
|
41
|
+
*/
|
|
42
|
+
start(options = {}) {
|
|
43
|
+
const { config } = options;
|
|
44
|
+
const binCfg = Utils.loadConfig(config);
|
|
45
|
+
|
|
46
|
+
this.electronServe(binCfg.start);
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
sleep(ms) {
|
|
50
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* start frontend serve
|
|
55
|
+
*/
|
|
56
|
+
frontendServe(cfg) {
|
|
57
|
+
// 如果是 file:// 协议,则不启动
|
|
58
|
+
if (cfg.protocol == 'file://') {
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
// 模拟前端启动慢
|
|
62
|
+
// await this.sleep(5 * 1000);
|
|
63
|
+
console.log(chalk.blue('[ee-bin] [dev] ') + chalk.green('Start the frontend serve...'));
|
|
64
|
+
console.log(chalk.blue('[ee-bin] [dev] ') + chalk.green('config:'), JSON.stringify(cfg));
|
|
65
|
+
|
|
66
|
+
const frontendDir = path.join(process.cwd(), cfg.directory);
|
|
67
|
+
const frontendArgs = is.string(cfg.args) ? [cfg.args] : cfg.args;
|
|
68
|
+
this.frontendProcess = crossSpawn(
|
|
69
|
+
cfg.cmd,
|
|
70
|
+
frontendArgs,
|
|
71
|
+
{ stdio: 'inherit', cwd: frontendDir, maxBuffer: 1024 * 1024 * 1024 },
|
|
72
|
+
);
|
|
73
|
+
this.frontendProcess.on('exit', () => {
|
|
74
|
+
console.log(chalk.blue('[ee-bin] [dev] ') + chalk.green('frontend serve exit'));
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* start electron serve
|
|
80
|
+
*/
|
|
81
|
+
electronServe(cfg) {
|
|
82
|
+
console.log(chalk.blue('[ee-bin] [dev] ') + chalk.green('Start the electron serve...'));
|
|
83
|
+
console.log(chalk.blue('[ee-bin] [dev] ') + chalk.green('config:'), JSON.stringify(cfg));
|
|
84
|
+
|
|
85
|
+
const electronDir = path.join(process.cwd(), cfg.directory);
|
|
86
|
+
const electronArgs = is.string(cfg.args) ? [cfg.args] : cfg.args;
|
|
87
|
+
|
|
88
|
+
this.electronProcess = crossSpawn(
|
|
89
|
+
cfg.cmd,
|
|
90
|
+
electronArgs,
|
|
91
|
+
{stdio: 'inherit', cwd: electronDir, maxBuffer: 1024 * 1024 * 1024 }
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
this.electronProcess.on('exit', () => {
|
|
95
|
+
console.log(chalk.blue('[ee-bin] [dev] ') + chalk.green('Press "CTRL+C" to exit'));
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 构建前端 dist
|
|
101
|
+
*/
|
|
102
|
+
build(options = {}) {
|
|
103
|
+
const { config } = options;
|
|
104
|
+
const binCfg = Utils.loadConfig(config);
|
|
105
|
+
const cfg = binCfg.build;
|
|
106
|
+
|
|
107
|
+
// start build frontend dist
|
|
108
|
+
console.log(chalk.blue('[ee-bin] [build] ') + chalk.green('Build frontend dist'));
|
|
109
|
+
console.log(chalk.blue('[ee-bin] [build] ') + chalk.green('config:'), cfg);
|
|
110
|
+
|
|
111
|
+
const frontendDir = path.join(process.cwd(), cfg.directory);
|
|
112
|
+
const buildArgs = is.string(cfg.args) ? [cfg.args] : cfg.args;
|
|
113
|
+
|
|
114
|
+
const buildProcess = crossSpawn(
|
|
115
|
+
cfg.cmd,
|
|
116
|
+
buildArgs,
|
|
117
|
+
{ stdio: 'inherit', cwd: frontendDir, maxBuffer: 1024 * 1024 * 1024 },
|
|
118
|
+
);
|
|
119
|
+
buildProcess.on('exit', () => {
|
|
120
|
+
console.log(chalk.blue('[ee-bin] [build] ') + chalk.green('End'));
|
|
121
|
+
});
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 执行自定义命令
|
|
126
|
+
* 支持多个命令
|
|
127
|
+
*/
|
|
128
|
+
exec(options = {}) {
|
|
129
|
+
const { config, command } = options;
|
|
130
|
+
const binCfg = Utils.loadConfig(config);
|
|
131
|
+
|
|
132
|
+
let cmds;
|
|
133
|
+
const cmdString = command.trim();
|
|
134
|
+
if (cmdString.indexOf(',') !== -1) {
|
|
135
|
+
cmds = cmdString.split(',');
|
|
136
|
+
} else {
|
|
137
|
+
cmds = [cmdString];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
for (let i = 0; i < cmds.length; i++) {
|
|
141
|
+
let cmd = cmds[i];
|
|
142
|
+
let cfg = binCfg.exec[cmd];
|
|
143
|
+
|
|
144
|
+
if (!cfg) {
|
|
145
|
+
console.log(chalk.blue('[ee-bin] [exec] ') + chalk.red(`Error: ${cmd} config does not exist` ));
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
console.log(chalk.blue('[ee-bin] [exec] ') + chalk.green('Run custom command'));
|
|
150
|
+
console.log(chalk.blue('[ee-bin] [exec] ') + chalk.green('config:'), cfg);
|
|
151
|
+
|
|
152
|
+
let execDir = path.join(process.cwd(), cfg.directory);
|
|
153
|
+
let execArgs = is.string(cfg.args) ? [cfg.args] : cfg.args;
|
|
154
|
+
|
|
155
|
+
this.execProcess[cmd] = crossSpawn(
|
|
156
|
+
cfg.cmd,
|
|
157
|
+
execArgs,
|
|
158
|
+
{ stdio: 'inherit', cwd: execDir, maxBuffer: 1024 * 1024 * 1024 },
|
|
159
|
+
);
|
|
160
|
+
this.execProcess[cmd].on('exit', () => {
|
|
161
|
+
console.log(chalk.blue('[ee-bin] [exec] ') + 'the ' + chalk.green(`${cmd}`) + ' is execution completed');
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
}
|
package/tools/frontend.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const fs = require('fs');
|
|
5
|
-
const { spawn, spawnSync, exec, execFile } = require('child_process');
|
|
6
|
-
const Utils = require('../lib/utils');
|
|
7
|
-
const is = require('is-type-of');
|
|
8
|
-
|
|
9
|
-
module.exports = {
|
|
10
|
-
|
|
11
|
-
electronServer: undefined,
|
|
12
|
-
|
|
13
|
-
frontendServer: undefined,
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* 启动前端、主进程服务
|
|
17
|
-
*/
|
|
18
|
-
serve(options = {}) {
|
|
19
|
-
const { config } = options;
|
|
20
|
-
const cfg = Utils.loadConfig(config);
|
|
21
|
-
|
|
22
|
-
const { frontend, main } = cfg;
|
|
23
|
-
console.log('frontend:', frontend);
|
|
24
|
-
console.log('main:', main);
|
|
25
|
-
|
|
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);
|
|
30
|
-
|
|
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
|
-
|
|
53
|
-
const mainDir = path.join(process.cwd(), main.directory);
|
|
54
|
-
const mainArgs = is.string(main.args) ? [main.args] : main.args;
|
|
55
|
-
const electronPath = this._getElectronPath();
|
|
56
|
-
this.electronServer = spawn(electronPath, mainArgs, { stdio: 'inherit' });
|
|
57
|
-
|
|
58
|
-
this._init();
|
|
59
|
-
},
|
|
60
|
-
|
|
61
|
-
_getElectronPath() {
|
|
62
|
-
let electronExecPath = ''
|
|
63
|
-
const electronModulePath = path.dirname(require.resolve('electron'))
|
|
64
|
-
const pathFile = path.join(electronModulePath, 'path.txt')
|
|
65
|
-
let executablePath
|
|
66
|
-
if (fs.existsSync(pathFile)) {
|
|
67
|
-
executablePath = fs.readFileSync(pathFile, 'utf-8')
|
|
68
|
-
}
|
|
69
|
-
if (executablePath) {
|
|
70
|
-
electronExecPath = path.join(electronModulePath, 'dist', executablePath)
|
|
71
|
-
} else {
|
|
72
|
-
throw new Error('Electron uninstall')
|
|
73
|
-
}
|
|
74
|
-
return electronExecPath
|
|
75
|
-
},
|
|
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
|
-
}
|
|
100
|
-
}
|