ee-core 1.2.9 → 1.2.10-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/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
+ }
@@ -2,6 +2,7 @@ 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() {
@@ -45,6 +46,13 @@ class Appliaction extends EeApp {
45
46
  options.execDir = path.dirname(app.getPath('exe'));
46
47
  }
47
48
 
49
+ // Use encryption, base directory is public/electron
50
+ const encryptDir = path.join(app.getAppPath(), 'public', 'electron');
51
+ let isEncrypted = fs.existsSync(encryptDir);
52
+ if (options.env == 'prod' && isEncrypted) {
53
+ options.baseDir = encryptDir;
54
+ }
55
+
48
56
  // normalize env
49
57
  env.NODE_ENV = 'production';
50
58
  env.EE_HOME = options.homeDir;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "1.2.9",
3
+ "version": "1.2.10-beta.1",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,182 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const fs = require('fs');
5
+ const fsPro = require('fs-extra');
6
+ const UglifyJS = require('uglify-js');
7
+
8
+ class Encrypt {
9
+ constructor() {
10
+
11
+ // argv
12
+ this.type = '';
13
+ for (let i = 0; i < process.argv.length; i++) {
14
+ let tmpArgv = process.argv[i];
15
+ if (tmpArgv.indexOf('--type=') !== -1) {
16
+ this.type = tmpArgv.substring(7);
17
+ }
18
+ }
19
+
20
+ const directory = [
21
+ 'electron',
22
+ ];
23
+ this.dirs = [];
24
+
25
+ this.basePath = process.cwd();
26
+ // if (this.type == 'uglify') {
27
+ // this.encryptCodeDir = path.join(this.basePath, 'public');
28
+ // }
29
+ this.encryptCodeDir = path.join(this.basePath, 'public');
30
+
31
+ // 检查存在的目录
32
+ for (let i = 0; i < directory.length; i++) {
33
+ let codeDirPath = path.join(this.basePath, directory[i]);
34
+ if (fs.existsSync(codeDirPath)) {
35
+ this.dirs.push(directory[i]);
36
+ }
37
+ }
38
+ console.log('dirs:', this.dirs);
39
+ }
40
+
41
+ /**
42
+ * 备份 electron目录代码
43
+ */
44
+ backup () {
45
+ console.log('[ee-core] [encrypt] backup start');
46
+
47
+ for (let i = 0; i < this.dirs.length; i++) {
48
+ // check code dir
49
+ let codeDirPath = path.join(this.basePath, this.dirs[i]);
50
+ if (!fs.existsSync(codeDirPath)) {
51
+ console.log('[ee-core] [encrypt] backup ERROR: %s is not exist', codeDirPath);
52
+ return
53
+ }
54
+
55
+ let targetDir = path.join(this.encryptCodeDir, this.dirs[i]);
56
+
57
+ // remove old
58
+ this.rmBackup(targetDir);
59
+
60
+ // copy
61
+ console.log('[ee-core] [encrypt] backup targetDir:', targetDir);
62
+ if (!fs.existsSync(targetDir)) {
63
+ this.mkdir(targetDir);
64
+ this.chmodPath(targetDir, '777');
65
+ }
66
+
67
+ fsPro.copySync(codeDirPath, targetDir);
68
+ }
69
+ console.log('[ee-core] [encrypt] backup success');
70
+ }
71
+
72
+ /**
73
+ * 压缩代码
74
+ */
75
+ compress () {
76
+ console.log('[ee-core] [encrypt] compress start');
77
+ for (let i = 0; i < this.dirs.length; i++) {
78
+ let codeDirPath = path.join(this.encryptCodeDir, this.dirs[i]);
79
+ this.compressLoop(codeDirPath);
80
+ }
81
+ console.log('[ee-core] [encrypt] compress success');
82
+ };
83
+
84
+ compressLoop (dirPath) {
85
+ let files = [];
86
+ if (fs.existsSync(dirPath)) {
87
+ files = fs.readdirSync(dirPath);
88
+ files.forEach((file, index) => {
89
+ let curPath = dirPath + '/' + file;
90
+ if (fs.statSync(curPath).isDirectory()) {
91
+ this.compressLoop(curPath);
92
+ } else {
93
+ if (path.extname(curPath) === '.js') {
94
+ this.miniFile(curPath);
95
+ }
96
+ }
97
+ });
98
+ }
99
+ }
100
+
101
+ miniFile (file) {
102
+ let code = fs.readFileSync(file, "utf8");
103
+ const options = {
104
+ mangle: {
105
+ toplevel: false,
106
+ },
107
+ };
108
+
109
+ let result = UglifyJS.minify(code, options);
110
+ fs.writeFileSync(file, result.code, "utf8");
111
+ }
112
+
113
+ /**
114
+ * 移除备份
115
+ */
116
+ rmBackup (dir) {
117
+ if (fs.existsSync(dir)) {
118
+ console.log('[ee-core] [encrypt] clean old directory:', dir);
119
+ fs.rmSync(dir, {recursive: true, force: true});
120
+ }
121
+ return;
122
+ }
123
+
124
+ /**
125
+ * 检查文件是否存在
126
+ */
127
+ fileExist (filePath) {
128
+ try {
129
+ return fs.statSync(filePath).isFile();
130
+ } catch (err) {
131
+ return false;
132
+ }
133
+ };
134
+
135
+ mkdir (dirpath, dirname) {
136
+ // 判断是否是第一次调用
137
+ if (typeof dirname === 'undefined') {
138
+ if (fs.existsSync(dirpath)) {
139
+ return;
140
+ }
141
+ this.mkdir(dirpath, path.dirname(dirpath));
142
+ } else {
143
+ // 判断第二个参数是否正常,避免调用时传入错误参数
144
+ if (dirname !== path.dirname(dirpath)) {
145
+ this.mkdir(dirpath);
146
+ return;
147
+ }
148
+ if (fs.existsSync(dirname)) {
149
+ fs.mkdirSync(dirpath);
150
+ } else {
151
+ this.mkdir(dirname, path.dirname(dirname));
152
+ fs.mkdirSync(dirpath);
153
+ }
154
+ }
155
+ };
156
+
157
+ chmodPath (path, mode) {
158
+ let files = [];
159
+ if (fs.existsSync(path)) {
160
+ files = fs.readdirSync(path);
161
+ files.forEach((file, index) => {
162
+ const curPath = path + '/' + file;
163
+ if (fs.statSync(curPath).isDirectory()) {
164
+ this.chmodPath(curPath, mode); // 递归删除文件夹
165
+ } else {
166
+ fs.chmodSync(curPath, mode);
167
+ }
168
+ });
169
+ fs.chmodSync(path, mode);
170
+ }
171
+ };
172
+ }
173
+
174
+ const run = () => {
175
+ const e = new Encrypt();
176
+ e.backup();
177
+ e.compress();
178
+ }
179
+
180
+ module.exports = {
181
+ run
182
+ };