ee-core 1.2.9 → 1.2.10-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/bin/tools.js +5 -0
- package/config/config.default.js +1 -0
- package/core/lib/loader/context_loader.js +2 -1
- package/core/lib/loader/ee_loader.js +5 -2
- package/core/lib/loader/file_loader.js +8 -9
- package/core/lib/loader/mixin/config.js +0 -3
- package/core/lib/loader/mixin/controller.js +4 -3
- package/core/lib/loader/mixin/service.js +1 -0
- package/core/lib/utils/index.js +17 -0
- package/lib/application.js +8 -5
- package/lib/eeApp.js +4 -7
- package/lib/socket/ipcServer.js +6 -24
- package/package.json +2 -1
- package/tools/encrypt.js +246 -0
package/bin/tools.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const codeCompress = require('../tools/codeCompress');
|
|
4
4
|
const replaceDist = require('../tools/replaceDist');
|
|
5
|
+
const encrypt = require('../tools/encrypt');
|
|
5
6
|
|
|
6
7
|
// argv
|
|
7
8
|
const args = process.argv;
|
|
@@ -20,3 +21,7 @@ if (cmd == 'compress') {
|
|
|
20
21
|
if (cmd == 'restore') {
|
|
21
22
|
codeCompress.restore();
|
|
22
23
|
}
|
|
24
|
+
|
|
25
|
+
if (cmd == 'encrypt') {
|
|
26
|
+
encrypt.run();
|
|
27
|
+
}
|
package/config/config.default.js
CHANGED
|
@@ -5,6 +5,7 @@ const is = require('is-type-of');
|
|
|
5
5
|
const FileLoader = require('./file_loader');
|
|
6
6
|
const CLASSLOADER = Symbol('classLoader');
|
|
7
7
|
const EXPORTS = FileLoader.EXPORTS;
|
|
8
|
+
const utils = require('../utils');
|
|
8
9
|
|
|
9
10
|
class ClassLoader {
|
|
10
11
|
|
|
@@ -88,7 +89,7 @@ function getInstance(values, ctx) {
|
|
|
88
89
|
const Class = values[EXPORTS] ? values : null;
|
|
89
90
|
let instance;
|
|
90
91
|
if (Class) {
|
|
91
|
-
if (is.class(Class)) {
|
|
92
|
+
if (is.class(Class) || utils.isBytecodeClass(Class)) {
|
|
92
93
|
instance = new Class(ctx);
|
|
93
94
|
} else {
|
|
94
95
|
// it's just an object
|
|
@@ -272,7 +272,7 @@ class EeLoader {
|
|
|
272
272
|
if (inject.length === 0) inject = [ this.app ];
|
|
273
273
|
|
|
274
274
|
let ret = this.requireFile(filepath);
|
|
275
|
-
if (is.function(ret) && !is.class(ret)) {
|
|
275
|
+
if (is.function(ret) && !is.class(ret) && !utils.isBytecodeClass(ret)) {
|
|
276
276
|
ret = ret(...inject);
|
|
277
277
|
}
|
|
278
278
|
return ret;
|
|
@@ -358,7 +358,6 @@ class EeLoader {
|
|
|
358
358
|
const timingKey = `Load "${String(property)}" to Application`;
|
|
359
359
|
this.timing.start(timingKey);
|
|
360
360
|
new FileLoader(opt).load();
|
|
361
|
-
//console.log('app property:', this.app[property]);
|
|
362
361
|
this.timing.end(timingKey);
|
|
363
362
|
}
|
|
364
363
|
|
|
@@ -412,6 +411,10 @@ class EeLoader {
|
|
|
412
411
|
try {
|
|
413
412
|
fullPath = require.resolve(filepath);
|
|
414
413
|
} catch (e) {
|
|
414
|
+
let jscFile = filepath + '.jsc';
|
|
415
|
+
if (fs.existsSync(jscFile)) {
|
|
416
|
+
return jscFile;
|
|
417
|
+
}
|
|
415
418
|
return undefined;
|
|
416
419
|
}
|
|
417
420
|
|
|
@@ -65,7 +65,6 @@ class FileLoader {
|
|
|
65
65
|
*/
|
|
66
66
|
load() {
|
|
67
67
|
const items = this.parse();
|
|
68
|
-
//console.log('FileLoader load items:', items);
|
|
69
68
|
const target = this.options.target;
|
|
70
69
|
for (const item of items) {
|
|
71
70
|
// item { properties: [ 'a', 'b', 'c'], exports }
|
|
@@ -123,11 +122,10 @@ class FileLoader {
|
|
|
123
122
|
*/
|
|
124
123
|
parse() {
|
|
125
124
|
let files = this.options.match;
|
|
126
|
-
|
|
127
125
|
if (!files) {
|
|
128
126
|
files = (process.env.EE_TYPESCRIPT === 'true' && utils.extensions['.ts'])
|
|
129
127
|
? [ '**/*.(js|ts)', '!**/*.d.ts' ]
|
|
130
|
-
: [ '**/*.js' ];
|
|
128
|
+
: [ '**/*.js', '**/*.jsc'];
|
|
131
129
|
} else {
|
|
132
130
|
files = Array.isArray(files) ? files : [ files ];
|
|
133
131
|
}
|
|
@@ -150,7 +148,6 @@ class FileLoader {
|
|
|
150
148
|
|
|
151
149
|
for (const directory of directories) {
|
|
152
150
|
const filepaths = globby.sync(files, { cwd: directory });
|
|
153
|
-
|
|
154
151
|
for (const filepath of filepaths) {
|
|
155
152
|
const fullpath = path.join(directory, filepath);
|
|
156
153
|
if (!fs.statSync(fullpath).isFile()) continue;
|
|
@@ -160,17 +157,16 @@ class FileLoader {
|
|
|
160
157
|
// app/service/foo/bar.js => service.foo.bar
|
|
161
158
|
const pathName = directory.split(/[/\\]/).slice(-1) + '.' + properties.join('.');
|
|
162
159
|
// get exports from the file
|
|
163
|
-
|
|
164
|
-
|
|
160
|
+
let exports = getExports(fullpath, this.options, pathName);
|
|
165
161
|
// ignore exports when it's null or false returned by filter function
|
|
166
162
|
if (exports == null || (filter && filter(exports) === false)) continue;
|
|
167
163
|
|
|
168
164
|
// set properties of class
|
|
169
|
-
if (is.class(exports)) {
|
|
165
|
+
if (is.class(exports) || utils.isBytecodeClass(exports)) {
|
|
170
166
|
exports.prototype.pathName = pathName;
|
|
171
167
|
exports.prototype.fullPath = fullpath;
|
|
172
168
|
}
|
|
173
|
-
|
|
169
|
+
|
|
174
170
|
items.push({ fullpath, properties, exports });
|
|
175
171
|
debug('parse %s, properties %j, export %O', fullpath, properties, exports);
|
|
176
172
|
}
|
|
@@ -202,6 +198,7 @@ function getProperties(filepath, { caseStyle }) {
|
|
|
202
198
|
// If exports is null/undefined, it will be ignored
|
|
203
199
|
function getExports(fullpath, { initializer, call, inject }, pathName) {
|
|
204
200
|
let exports = utils.loadFile(fullpath);
|
|
201
|
+
|
|
205
202
|
// process exports as you like
|
|
206
203
|
if (initializer) {
|
|
207
204
|
exports = initializer(exports, { path: fullpath, pathName });
|
|
@@ -212,7 +209,9 @@ function getExports(fullpath, { initializer, call, inject }, pathName) {
|
|
|
212
209
|
// module.exports = class Service {};
|
|
213
210
|
// or
|
|
214
211
|
// module.exports = function*() {}
|
|
215
|
-
|
|
212
|
+
//new exports;
|
|
213
|
+
|
|
214
|
+
if (is.class(exports) || is.generatorFunction(exports) || is.asyncFunction(exports) || utils.isBytecodeClass(exports)) {
|
|
216
215
|
return exports;
|
|
217
216
|
}
|
|
218
217
|
|
|
@@ -75,11 +75,8 @@ module.exports = {
|
|
|
75
75
|
_loadConfig(dirpath, filename, extraInject, type) {
|
|
76
76
|
const isPlugin = type === 'plugin';
|
|
77
77
|
const isApp = type === 'app';
|
|
78
|
-
|
|
79
78
|
let filepath = this.resolveModule(path.join(dirpath, 'config', filename));
|
|
80
79
|
|
|
81
|
-
//console.log("_loadConfig filepath:", filepath);
|
|
82
|
-
|
|
83
80
|
// let config.js compatible
|
|
84
81
|
if (filename === 'config.default' && !filepath) {
|
|
85
82
|
filepath = this.resolveModule(path.join(dirpath, 'config/config'));
|
|
@@ -6,7 +6,6 @@ const utility = require('utility');
|
|
|
6
6
|
const utils = require('../../utils');
|
|
7
7
|
const FULLPATH = require('../file_loader').FULLPATH;
|
|
8
8
|
|
|
9
|
-
|
|
10
9
|
module.exports = {
|
|
11
10
|
|
|
12
11
|
/**
|
|
@@ -26,10 +25,11 @@ module.exports = {
|
|
|
26
25
|
// return class HomeController extends app.Controller {};
|
|
27
26
|
// }
|
|
28
27
|
// ```
|
|
29
|
-
|
|
28
|
+
|
|
29
|
+
if (is.function(obj) && !is.generatorFunction(obj) && !is.class(obj) && !is.asyncFunction(obj) && !utils.isBytecodeClass(obj)) {
|
|
30
30
|
obj = obj(this.app);
|
|
31
31
|
}
|
|
32
|
-
if (is.class(obj)) {
|
|
32
|
+
if (is.class(obj) || utils.isBytecodeClass(obj)) {
|
|
33
33
|
obj.prototype.pathName = opt.pathName;
|
|
34
34
|
obj.prototype.fullPath = opt.path;
|
|
35
35
|
return wrapClass(obj);
|
|
@@ -76,6 +76,7 @@ function wrapClass(Controller) {
|
|
|
76
76
|
}
|
|
77
77
|
proto = Object.getPrototypeOf(proto);
|
|
78
78
|
}
|
|
79
|
+
|
|
79
80
|
return ret;
|
|
80
81
|
|
|
81
82
|
function methodToMiddleware(Controller, key) {
|
package/core/lib/utils/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
require('bytenode');
|
|
3
4
|
const convert = require('koa-convert');
|
|
4
5
|
const is = require('is-type-of');
|
|
5
6
|
const path = require('path');
|
|
@@ -23,6 +24,7 @@ module.exports = {
|
|
|
23
24
|
if (extname && !Module._extensions[extname]) {
|
|
24
25
|
return fs.readFileSync(filepath);
|
|
25
26
|
}
|
|
27
|
+
|
|
26
28
|
// require js module
|
|
27
29
|
const obj = require(filepath);
|
|
28
30
|
if (!obj) return obj;
|
|
@@ -87,6 +89,21 @@ module.exports = {
|
|
|
87
89
|
const reg = /[/\\]/g;
|
|
88
90
|
return filepath.replace(baseDir + path.sep, '').replace(reg, '/');
|
|
89
91
|
},
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 字节码类
|
|
95
|
+
*/
|
|
96
|
+
isBytecodeClass (exports) {
|
|
97
|
+
let isClass = false;
|
|
98
|
+
|
|
99
|
+
// 标识
|
|
100
|
+
if (exports.toString().indexOf('[class') != -1) {
|
|
101
|
+
isClass = true;
|
|
102
|
+
}
|
|
103
|
+
// TODO 更严谨的判断,应该加上文件名和路径
|
|
104
|
+
|
|
105
|
+
return isClass;
|
|
106
|
+
},
|
|
90
107
|
};
|
|
91
108
|
|
|
92
109
|
|
package/lib/application.js
CHANGED
|
@@ -2,15 +2,11 @@ const {app} = require('electron');
|
|
|
2
2
|
const path = require('path');
|
|
3
3
|
const EeApp = require('./eeApp');
|
|
4
4
|
const debug = require('debug')('ee-core:Appliaction');
|
|
5
|
+
const fs = require('fs');
|
|
5
6
|
|
|
6
7
|
class Appliaction extends EeApp {
|
|
7
8
|
constructor() {
|
|
8
|
-
|
|
9
|
-
// 初始化环境变量
|
|
10
|
-
// const opt = this.initEnv();
|
|
11
9
|
const { env } = process;
|
|
12
|
-
|
|
13
|
-
// 路径不能使用绝对,打包前后有问题
|
|
14
10
|
let options = {
|
|
15
11
|
env: 'prod',
|
|
16
12
|
serverScope: '',
|
|
@@ -45,6 +41,13 @@ class Appliaction extends EeApp {
|
|
|
45
41
|
options.execDir = path.dirname(app.getPath('exe'));
|
|
46
42
|
}
|
|
47
43
|
|
|
44
|
+
// Use encryption, base directory is public/electron
|
|
45
|
+
const encryptDir = path.join(app.getAppPath(), 'public', 'electron');
|
|
46
|
+
let isEncrypted = fs.existsSync(encryptDir);
|
|
47
|
+
if (options.env == 'prod' && isEncrypted) {
|
|
48
|
+
options.baseDir = encryptDir;
|
|
49
|
+
}
|
|
50
|
+
|
|
48
51
|
// normalize env
|
|
49
52
|
env.NODE_ENV = 'production';
|
|
50
53
|
env.EE_HOME = options.homeDir;
|
package/lib/eeApp.js
CHANGED
|
@@ -70,12 +70,12 @@ class EeApp extends BaseApp {
|
|
|
70
70
|
|
|
71
71
|
app.whenReady().then(() => {
|
|
72
72
|
self.createWindow();
|
|
73
|
-
app.on('activate',
|
|
73
|
+
app.on('activate', () => {
|
|
74
74
|
self.restoreMainWindow();
|
|
75
75
|
})
|
|
76
76
|
})
|
|
77
77
|
|
|
78
|
-
app.on('window-all-closed',
|
|
78
|
+
app.on('window-all-closed', () => {
|
|
79
79
|
if (process.platform !== 'darwin') {
|
|
80
80
|
self.coreLogger.info('[Appliaction] [initialize] window-all-closed quit');
|
|
81
81
|
self.appQuit();
|
|
@@ -243,8 +243,9 @@ class EeApp extends BaseApp {
|
|
|
243
243
|
* 主页面
|
|
244
244
|
*/
|
|
245
245
|
loadMainUrl (type, url) {
|
|
246
|
+
const mainServer = this.config.mainServer;
|
|
246
247
|
this.logger.info('main page is env: %s, type: %s, App running at: %s', this.config.env, type, url);
|
|
247
|
-
this.electron.mainWindow.loadURL(url);
|
|
248
|
+
this.electron.mainWindow.loadURL(url, mainServer.options);
|
|
248
249
|
}
|
|
249
250
|
|
|
250
251
|
/**
|
|
@@ -252,7 +253,6 @@ class EeApp extends BaseApp {
|
|
|
252
253
|
*/
|
|
253
254
|
async appQuit () {
|
|
254
255
|
await this.beforeClose();
|
|
255
|
-
|
|
256
256
|
app.quit();
|
|
257
257
|
}
|
|
258
258
|
|
|
@@ -299,7 +299,6 @@ class EeApp extends BaseApp {
|
|
|
299
299
|
*/
|
|
300
300
|
async electronAppReady () {
|
|
301
301
|
// do some things
|
|
302
|
-
|
|
303
302
|
}
|
|
304
303
|
|
|
305
304
|
/**
|
|
@@ -307,7 +306,6 @@ class EeApp extends BaseApp {
|
|
|
307
306
|
*/
|
|
308
307
|
async windowReady () {
|
|
309
308
|
// do some things
|
|
310
|
-
|
|
311
309
|
}
|
|
312
310
|
|
|
313
311
|
/**
|
|
@@ -315,7 +313,6 @@ class EeApp extends BaseApp {
|
|
|
315
313
|
*/
|
|
316
314
|
async beforeClose () {
|
|
317
315
|
// do some things
|
|
318
|
-
|
|
319
316
|
}
|
|
320
317
|
}
|
|
321
318
|
|
package/lib/socket/ipcServer.js
CHANGED
|
@@ -9,7 +9,6 @@ const fs = require('fs');
|
|
|
9
9
|
const globby = require('globby');
|
|
10
10
|
const utils = require('../../core/lib/utils');
|
|
11
11
|
const wrap = require('../../utils/wrap');
|
|
12
|
-
const utility = require('utility');
|
|
13
12
|
|
|
14
13
|
class IpcServer {
|
|
15
14
|
constructor (app) {
|
|
@@ -27,23 +26,23 @@ class IpcServer {
|
|
|
27
26
|
// 遍历方法
|
|
28
27
|
const files = (process.env.EE_TYPESCRIPT === 'true' && utils.extensions['.ts'])
|
|
29
28
|
? [ '**/*.(js|ts)', '!**/*.d.ts' ]
|
|
30
|
-
: [ '**/*.js' ];
|
|
29
|
+
: [ '**/*.js','**/*.jsc' ];
|
|
31
30
|
const directory = path.join(this.app.config.baseDir, 'controller');
|
|
32
31
|
const filepaths = globby.sync(files, { cwd: directory });
|
|
32
|
+
|
|
33
33
|
for (const filepath of filepaths) {
|
|
34
34
|
const fullpath = path.join(directory, filepath);
|
|
35
35
|
if (!fs.statSync(fullpath).isFile()) continue;
|
|
36
36
|
|
|
37
37
|
const properties = wrap.getProperties(filepath, {caseStyle: 'lower'});
|
|
38
38
|
const pathName = directory.split(/[/\\]/).slice(-1) + '.' + properties.join('.');
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
let fileObj = utils.loadFile(fullpath);
|
|
41
41
|
const fns = {};
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
if (is.class(fileObj)) {
|
|
42
|
+
// 为了统一,仅支持class文件
|
|
43
|
+
if (is.class(fileObj) || utils.isBytecodeClass(fileObj)) {
|
|
46
44
|
let proto = fileObj.prototype;
|
|
45
|
+
// 不遍历父类的方法
|
|
47
46
|
//while (proto !== Object.prototype) {
|
|
48
47
|
const keys = Object.getOwnPropertyNames(proto);
|
|
49
48
|
for (const key of keys) {
|
|
@@ -58,23 +57,6 @@ class IpcServer {
|
|
|
58
57
|
//proto = Object.getPrototypeOf(proto);
|
|
59
58
|
//}
|
|
60
59
|
}
|
|
61
|
-
if (is.object(fileObj)) {
|
|
62
|
-
const keys = Object.keys(fileObj);
|
|
63
|
-
for (const key of keys) {
|
|
64
|
-
if (is.function(fileObj[key])) {
|
|
65
|
-
const names = utility.getParamNames(fileObj[key]);
|
|
66
|
-
if (names[0] === 'next') {
|
|
67
|
-
throw new Error(`controller \`${prefix || ''}${key}\` should not use next as argument from file ${path}`);
|
|
68
|
-
}
|
|
69
|
-
fns[key] = 1;
|
|
70
|
-
}
|
|
71
|
-
// else if (is.object(fileObj[key])) {
|
|
72
|
-
// ret[key] = wrapObject(obj[key], path, `${prefix || ''}${key}.`);
|
|
73
|
-
// }
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
// if (is.generatorFunction(obj) || is.asyncFunction(obj)) {
|
|
77
|
-
// }
|
|
78
60
|
|
|
79
61
|
debug('register class %s fns %j', pathName, fns);
|
|
80
62
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ee-core",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.10-beta.3",
|
|
4
4
|
"description": "ee core",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"agentkeepalive": "^4.2.0",
|
|
16
16
|
"better-sqlite3": "^7.6.0",
|
|
17
|
+
"bytenode": "^1.3.6",
|
|
17
18
|
"co": "^4.6.0",
|
|
18
19
|
"debug": "^4.3.3",
|
|
19
20
|
"depd": "^2.0.0",
|
package/tools/encrypt.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const fsPro = require('fs-extra');
|
|
6
|
+
const is = require('is-type-of');
|
|
7
|
+
const UglifyJS = require('uglify-js');
|
|
8
|
+
const bytenode = require('bytenode');
|
|
9
|
+
|
|
10
|
+
class Encrypt {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.basePath = process.cwd();
|
|
13
|
+
const directory = [
|
|
14
|
+
'electron',
|
|
15
|
+
];
|
|
16
|
+
this.dirs = [];
|
|
17
|
+
this.type = '';
|
|
18
|
+
this.configPath = '';
|
|
19
|
+
this.config = null;
|
|
20
|
+
this.filesExt = ['.js', '.json', '.node'];
|
|
21
|
+
this.encryptCodeDir = path.join(this.basePath, 'public');
|
|
22
|
+
|
|
23
|
+
// argv
|
|
24
|
+
for (let i = 0; i < process.argv.length; i++) {
|
|
25
|
+
let tmpArgv = process.argv[i];
|
|
26
|
+
if (tmpArgv.indexOf('--type=') !== -1) {
|
|
27
|
+
this.type = tmpArgv.substring(7);
|
|
28
|
+
}
|
|
29
|
+
if (tmpArgv.indexOf('--config=') !== -1) {
|
|
30
|
+
let configPathStr = tmpArgv.substring(9);
|
|
31
|
+
this.configPath = path.join(this.basePath, configPathStr);
|
|
32
|
+
this.config = fs.existsSync(this.configPath) ? require(this.configPath) : null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 检查存在的目录
|
|
37
|
+
for (let i = 0; i < directory.length; i++) {
|
|
38
|
+
let codeDirPath = path.join(this.basePath, directory[i]);
|
|
39
|
+
if (fs.existsSync(codeDirPath)) {
|
|
40
|
+
this.dirs.push(directory[i]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
console.log('[ee-core] [encrypt] dirs:', this.dirs);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 检查
|
|
48
|
+
*/
|
|
49
|
+
check () {
|
|
50
|
+
if (this.configPath.length > 0 && !is.object(this.config)) {
|
|
51
|
+
console.log('[ee-core] [encrypt] ERROR: config file is invalid');
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 备份 electron 目录代码
|
|
59
|
+
*/
|
|
60
|
+
backup () {
|
|
61
|
+
console.log('[ee-core] [encrypt] backup start');
|
|
62
|
+
|
|
63
|
+
for (let i = 0; i < this.dirs.length; i++) {
|
|
64
|
+
// check code dir
|
|
65
|
+
let codeDirPath = path.join(this.basePath, this.dirs[i]);
|
|
66
|
+
if (!fs.existsSync(codeDirPath)) {
|
|
67
|
+
console.log('[ee-core] [encrypt] ERROR: backup %s is not exist', codeDirPath);
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let targetDir = path.join(this.encryptCodeDir, this.dirs[i]);
|
|
72
|
+
|
|
73
|
+
// remove old
|
|
74
|
+
this.rmBackup(targetDir);
|
|
75
|
+
|
|
76
|
+
// copy
|
|
77
|
+
console.log('[ee-core] [encrypt] backup target Dir:', targetDir);
|
|
78
|
+
if (!fs.existsSync(targetDir)) {
|
|
79
|
+
this.mkdir(targetDir);
|
|
80
|
+
this.chmodPath(targetDir, '777');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
fsPro.copySync(codeDirPath, targetDir);
|
|
84
|
+
}
|
|
85
|
+
console.log('[ee-core] [encrypt] backup end');
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 加密代码
|
|
91
|
+
*/
|
|
92
|
+
encrypt () {
|
|
93
|
+
console.log('[ee-core] [encrypt] start ciphering');
|
|
94
|
+
for (let i = 0; i < this.dirs.length; i++) {
|
|
95
|
+
let codeDirPath = path.join(this.encryptCodeDir, this.dirs[i]);
|
|
96
|
+
this.loop(codeDirPath);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
console.log('[ee-core] [encrypt] end ciphering');
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 递归
|
|
104
|
+
*/
|
|
105
|
+
loop (dirPath) {
|
|
106
|
+
let files = [];
|
|
107
|
+
if (fs.existsSync(dirPath)) {
|
|
108
|
+
files = fs.readdirSync(dirPath);
|
|
109
|
+
files.forEach((file, index) => {
|
|
110
|
+
let curPath = dirPath + '/' + file;
|
|
111
|
+
if (fs.statSync(curPath).isDirectory()) {
|
|
112
|
+
this.loop(curPath);
|
|
113
|
+
} else {
|
|
114
|
+
const extname = path.extname(curPath);
|
|
115
|
+
if (this.filesExt.indexOf(extname) !== -1) {
|
|
116
|
+
this.generate(curPath);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 生成文件
|
|
125
|
+
*/
|
|
126
|
+
generate (curPath) {
|
|
127
|
+
if (this.type == 'bytecode') {
|
|
128
|
+
this.generateBytecodeFile(curPath);
|
|
129
|
+
} else {
|
|
130
|
+
this.generateConfuseFile(curPath);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 生成压缩/混淆文件
|
|
136
|
+
*/
|
|
137
|
+
generateConfuseFile (file) {
|
|
138
|
+
let defaultOpt = {
|
|
139
|
+
mangle: {
|
|
140
|
+
toplevel: false,
|
|
141
|
+
},
|
|
142
|
+
compress: {
|
|
143
|
+
drop_console: true,
|
|
144
|
+
passes: 2
|
|
145
|
+
},
|
|
146
|
+
output: {
|
|
147
|
+
beautify: false
|
|
148
|
+
},
|
|
149
|
+
}
|
|
150
|
+
let options = defaultOpt;
|
|
151
|
+
if (is.object(this.config)) {
|
|
152
|
+
options = Object.assign(defaultOpt, this.config);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let code = fs.readFileSync(file, "utf8");
|
|
156
|
+
let result = UglifyJS.minify(code, options);
|
|
157
|
+
fs.writeFileSync(file, result.code, "utf8");
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 生成字节码文件
|
|
162
|
+
*/
|
|
163
|
+
generateBytecodeFile (curPath) {
|
|
164
|
+
if (path.extname(curPath) !== '.js') {
|
|
165
|
+
return
|
|
166
|
+
}
|
|
167
|
+
//let jscFile = curPath.replace(/.js/g, '.jsc');
|
|
168
|
+
let jscFile = curPath + 'c';
|
|
169
|
+
bytenode.compileFile({
|
|
170
|
+
filename: curPath,
|
|
171
|
+
output: jscFile,
|
|
172
|
+
});
|
|
173
|
+
fs.rmSync(curPath, {force: true});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 移除备份
|
|
178
|
+
*/
|
|
179
|
+
rmBackup (dir) {
|
|
180
|
+
if (fs.existsSync(dir)) {
|
|
181
|
+
console.log('[ee-core] [encrypt] clean old directory:', dir);
|
|
182
|
+
fs.rmSync(dir, {recursive: true, force: true});
|
|
183
|
+
}
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 检查文件是否存在
|
|
189
|
+
*/
|
|
190
|
+
fileExist (filePath) {
|
|
191
|
+
try {
|
|
192
|
+
return fs.statSync(filePath).isFile();
|
|
193
|
+
} catch (err) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
mkdir (dirpath, dirname) {
|
|
199
|
+
// 判断是否是第一次调用
|
|
200
|
+
if (typeof dirname === 'undefined') {
|
|
201
|
+
if (fs.existsSync(dirpath)) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
this.mkdir(dirpath, path.dirname(dirpath));
|
|
205
|
+
} else {
|
|
206
|
+
// 判断第二个参数是否正常,避免调用时传入错误参数
|
|
207
|
+
if (dirname !== path.dirname(dirpath)) {
|
|
208
|
+
this.mkdir(dirpath);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (fs.existsSync(dirname)) {
|
|
212
|
+
fs.mkdirSync(dirpath);
|
|
213
|
+
} else {
|
|
214
|
+
this.mkdir(dirname, path.dirname(dirname));
|
|
215
|
+
fs.mkdirSync(dirpath);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
chmodPath (path, mode) {
|
|
221
|
+
let files = [];
|
|
222
|
+
if (fs.existsSync(path)) {
|
|
223
|
+
files = fs.readdirSync(path);
|
|
224
|
+
files.forEach((file, index) => {
|
|
225
|
+
const curPath = path + '/' + file;
|
|
226
|
+
if (fs.statSync(curPath).isDirectory()) {
|
|
227
|
+
this.chmodPath(curPath, mode); // 递归删除文件夹
|
|
228
|
+
} else {
|
|
229
|
+
fs.chmodSync(curPath, mode);
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
fs.chmodSync(path, mode);
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const run = () => {
|
|
238
|
+
const e = new Encrypt();
|
|
239
|
+
if (!e.check()) return;
|
|
240
|
+
if (!e.backup()) return;
|
|
241
|
+
e.encrypt();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
module.exports = {
|
|
245
|
+
run
|
|
246
|
+
};
|