ee-core 1.0.0

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.
Files changed (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2 -0
  3. package/bin/tools.js +22 -0
  4. package/config/config.default.js +179 -0
  5. package/core/index.js +13 -0
  6. package/core/lib/ee.js +220 -0
  7. package/core/lib/loader/context_loader.js +105 -0
  8. package/core/lib/loader/ee_loader.js +427 -0
  9. package/core/lib/loader/file_loader.js +262 -0
  10. package/core/lib/loader/mixin/config.js +138 -0
  11. package/core/lib/loader/mixin/controller.js +123 -0
  12. package/core/lib/loader/mixin/service.js +29 -0
  13. package/core/lib/utils/base_context_class.js +34 -0
  14. package/core/lib/utils/index.js +100 -0
  15. package/core/lib/utils/sequencify.js +59 -0
  16. package/core/lib/utils/timing.js +77 -0
  17. package/index.js +50 -0
  18. package/lib/appLoader.js +45 -0
  19. package/lib/application.js +69 -0
  20. package/lib/baseApp.js +155 -0
  21. package/lib/constant.js +30 -0
  22. package/lib/eeApp.js +306 -0
  23. package/lib/helper.js +52 -0
  24. package/lib/httpclient.js +136 -0
  25. package/lib/logger.js +47 -0
  26. package/lib/socket/io.js +22 -0
  27. package/lib/socket/ipcServer.js +112 -0
  28. package/lib/socket/socketClient.js +51 -0
  29. package/lib/socket/socketServer.js +70 -0
  30. package/lib/socket/start.js +18 -0
  31. package/lib/storage/appStorage.js +14 -0
  32. package/lib/storage/index.js +22 -0
  33. package/lib/storage/lowdbStorage.js +144 -0
  34. package/package.json +39 -0
  35. package/resource/images/loding.gif +0 -0
  36. package/resource/images/tray_logo.png +0 -0
  37. package/resource/loading.html +22 -0
  38. package/resource/view_example.html +22 -0
  39. package/tools/codeCompress.js +202 -0
  40. package/tools/replaceDist.js +71 -0
  41. package/utils/index.js +141 -0
  42. package/utils/wrap.js +38 -0
@@ -0,0 +1,22 @@
1
+ <html>
2
+ <head>
3
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4
+ <style type="text/css">
5
+ body{
6
+ margin:0px auto;
7
+ }
8
+ #picture1 {
9
+ position: absolute;
10
+ left: 50%;
11
+ top: 35%;
12
+ transform: translate(-50%, -50%);
13
+ }
14
+ </style>
15
+ <title></title>
16
+ </head>
17
+ <body>
18
+ <div id="picture1">
19
+ <img src="./images/loding.gif" />
20
+ </div>
21
+ </body>
22
+ </html>
@@ -0,0 +1,22 @@
1
+ <html>
2
+ <head>
3
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4
+ <style type="text/css">
5
+ body{
6
+ margin:0px auto;
7
+ }
8
+ #content {
9
+ position: absolute;
10
+ left: 50%;
11
+ top: 35%;
12
+ transform: translate(-50%, -50%);
13
+ }
14
+ </style>
15
+ <title></title>
16
+ </head>
17
+ <body>
18
+ <div id="content">
19
+ 这是一个html页面
20
+ </div>
21
+ </body>
22
+ </html>
@@ -0,0 +1,202 @@
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 CodeCompress {
9
+ constructor() {
10
+ const directory = [
11
+ 'app',
12
+ 'electron',
13
+ 'config'
14
+ ];
15
+ this.dirs = [];
16
+
17
+ this.basePath = process.cwd();
18
+ this.backupCodeDir = path.join(this.basePath, 'run', 'backup_code');
19
+
20
+ // 检查存在的目录
21
+ for (let i = 0; i < directory.length; i++) {
22
+ let codeDirPath = path.join(this.basePath, directory[i]);
23
+ if (fs.existsSync(codeDirPath)) {
24
+ this.dirs.push(directory[i]);
25
+ }
26
+ }
27
+ console.log('dirs:', this.dirs);
28
+ }
29
+
30
+ /**
31
+ * 备份 app、electron目录代码
32
+ */
33
+ backup () {
34
+ console.log('[ee-core] [code_compress] [backup] start');
35
+ this.rmBackup();
36
+
37
+ for (let i = 0; i < this.dirs.length; i++) {
38
+ // check code dir
39
+ let codeDirPath = path.join(this.basePath, this.dirs[i]);
40
+ if (!fs.existsSync(codeDirPath)) {
41
+ console.log('[ee-core] [code_compress] [backup] ERROR: %s is not exist', codeDirPath);
42
+ return
43
+ }
44
+
45
+ // copy
46
+ let targetDir = path.join(this.backupCodeDir, this.dirs[i]);
47
+ console.log('[ee-core] [code_compress] [backup] targetDir:', targetDir);
48
+ if (!fs.existsSync(targetDir)) {
49
+ this.mkdir(targetDir);
50
+ this.chmodPath(targetDir, '777');
51
+ }
52
+
53
+ fsPro.copySync(codeDirPath, targetDir);
54
+ }
55
+ console.log('[ee-core] [code_compress] [backup] success');
56
+ }
57
+
58
+ /**
59
+ * 还原代码
60
+ */
61
+ restore () {
62
+ console.log('[ee-core] [code_compress] [restore] start');
63
+ for (let i = 0; i < this.dirs.length; i++) {
64
+ let codeDirPath = path.join(this.backupCodeDir, this.dirs[i]);
65
+ let targetDir = path.join(this.basePath, this.dirs[i]);
66
+ fsPro.copySync(codeDirPath, targetDir);
67
+ }
68
+ console.log('[ee-core] [code_compress] [restore] success');
69
+ };
70
+
71
+ /**
72
+ * 压缩代码
73
+ */
74
+ compress () {
75
+ console.log('[ee-core] [code_compress] [compress] start');
76
+ for (let i = 0; i < this.dirs.length; i++) {
77
+ let codeDirPath = path.join(this.basePath, this.dirs[i]);
78
+ this.compressLoop(codeDirPath);
79
+ }
80
+ console.log('[ee-core] [code_compress] [compress] success');
81
+ };
82
+
83
+ compressLoop (dirPath) {
84
+ let files = [];
85
+ if (fs.existsSync(dirPath)) {
86
+ files = fs.readdirSync(dirPath);
87
+ files.forEach((file, index) => {
88
+ let curPath = dirPath + '/' + file;
89
+ if (fs.statSync(curPath).isDirectory()) {
90
+ this.compressLoop(curPath);
91
+ } else {
92
+ if (path.extname(curPath) === '.js') {
93
+ this.miniFile(curPath);
94
+ }
95
+ }
96
+ });
97
+ }
98
+ }
99
+
100
+ miniFile (file) {
101
+ let code = fs.readFileSync(file, "utf8");
102
+ const options = {
103
+ mangle: {
104
+ toplevel: false,
105
+ },
106
+ };
107
+
108
+ let result = UglifyJS.minify(code, options);
109
+ fs.writeFileSync(file, result.code, "utf8");
110
+ }
111
+
112
+ /**
113
+ * 格式化参数
114
+ */
115
+ formatArgvs () {
116
+ // argv
117
+ let argvs = [];
118
+ for (let i = 0; i < process.argv.length; i++) {
119
+ const tmpArgv = process.argv[i]
120
+ if (tmpArgv.indexOf('--') !== -1) {
121
+ argvs.push(tmpArgv.substring(2))
122
+ }
123
+ }
124
+ return argvs;
125
+ }
126
+
127
+ /**
128
+ * 移除备份
129
+ */
130
+ rmBackup () {
131
+ fs.rmdirSync(this.backupCodeDir, {recursive: true});
132
+ return;
133
+ }
134
+
135
+ /**
136
+ * 检查文件是否存在
137
+ */
138
+ fileExist (filePath) {
139
+ try {
140
+ return fs.statSync(filePath).isFile();
141
+ } catch (err) {
142
+ return false;
143
+ }
144
+ };
145
+
146
+ mkdir (dirpath, dirname) {
147
+ // 判断是否是第一次调用
148
+ if (typeof dirname === 'undefined') {
149
+ if (fs.existsSync(dirpath)) {
150
+ return;
151
+ }
152
+ this.mkdir(dirpath, path.dirname(dirpath));
153
+ } else {
154
+ // 判断第二个参数是否正常,避免调用时传入错误参数
155
+ if (dirname !== path.dirname(dirpath)) {
156
+ this.mkdir(dirpath);
157
+ return;
158
+ }
159
+ if (fs.existsSync(dirname)) {
160
+ fs.mkdirSync(dirpath);
161
+ } else {
162
+ this.mkdir(dirname, path.dirname(dirname));
163
+ fs.mkdirSync(dirpath);
164
+ }
165
+ }
166
+ };
167
+
168
+ chmodPath (path, mode) {
169
+ let files = [];
170
+ if (fs.existsSync(path)) {
171
+ files = fs.readdirSync(path);
172
+ files.forEach((file, index) => {
173
+ const curPath = path + '/' + file;
174
+ if (fs.statSync(curPath).isDirectory()) {
175
+ this.chmodPath(curPath, mode); // 递归删除文件夹
176
+ } else {
177
+ fs.chmodSync(curPath, mode);
178
+ }
179
+ });
180
+ fs.chmodSync(path, mode);
181
+ }
182
+ };
183
+ }
184
+
185
+ const compress = () => {
186
+ const cc = new CodeCompress();
187
+ cc.backup();
188
+ cc.compress();
189
+ }
190
+
191
+ const restore = () => {
192
+ const cc = new CodeCompress();
193
+ cc.restore();
194
+ }
195
+
196
+ module.exports = {
197
+ compress,
198
+ restore
199
+ };
200
+
201
+
202
+
@@ -0,0 +1,71 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const fs = require('fs');
5
+ const fsPro = require('fs-extra');
6
+
7
+ /**
8
+ * 资源替换
9
+ */
10
+
11
+ module.exports = {
12
+
13
+ /**
14
+ * 执行
15
+ */
16
+ run () {
17
+ console.log('[ee-core] [replace_dist] 开始移动资源');
18
+ const homeDir = process.cwd();
19
+
20
+ // argv
21
+ let distDir = '';
22
+ for (let i = 0; i < process.argv.length; i++) {
23
+ const tmpArgv = process.argv[i]
24
+ if (tmpArgv.indexOf('--dist_dir=') !== -1) {
25
+ distDir = tmpArgv.substring(11)
26
+ }
27
+ }
28
+
29
+ const fileExist = (filePath) => {
30
+ try {
31
+ return fs.statSync(filePath).isFile();
32
+ } catch (err) {
33
+ console.error('[ee-core] [replace_dist] ERROR ', err);
34
+ return false;
35
+ }
36
+ };
37
+
38
+ const sourceDir = path.join(homeDir, distDir);
39
+ const targetDir = path.join(homeDir, 'app', 'public');
40
+ const sourceIndexFile = path.join(sourceDir, 'index.html');
41
+ const targetIndexFile = path.join(homeDir, 'app', 'view', 'index.ejs');
42
+
43
+ if (!fileExist(sourceIndexFile)) {
44
+ console.error('[ee-core] [replace_dist] ERROR 前端资源不存在,请构建!!!');
45
+ return
46
+ }
47
+
48
+ // 复制到ee资源目录
49
+ const eeResourceDir = path.join(homeDir, 'public', 'dist');
50
+ fsPro.copySync(sourceDir, eeResourceDir);
51
+ console.log('[ee-core] [replace_dist] 复制资源到:', eeResourceDir);
52
+
53
+ // 复制到egg资源目录
54
+ if (fs.existsSync(targetDir)) {
55
+ console.log('[ee-core] [replace_dist] 重置egg资源:', targetDir);
56
+ fs.rmdirSync(targetDir, {recursive: true});
57
+
58
+ console.log('[ee-core] [replace_dist] 复制资源到egg:', sourceDir);
59
+ fsPro.copySync(sourceDir, targetDir);
60
+
61
+ // replace ejs
62
+ fsPro.copySync(sourceIndexFile, targetIndexFile);
63
+ console.log('[ee-core] [replace_dist] 替换 egg index.ejs');
64
+ }
65
+
66
+ console.log('[ee-core] [replace_dist] 结束');
67
+ }
68
+ }
69
+
70
+
71
+
package/utils/index.js ADDED
@@ -0,0 +1,141 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const constant = require('../lib/constant');
6
+ const convert = require('koa-convert');
7
+ const is = require('is-type-of');
8
+ const co = require('co');
9
+
10
+ exports.mkdir = function(dirpath, dirname) {
11
+ // 判断是否是第一次调用
12
+ if (typeof dirname === 'undefined') {
13
+ if (fs.existsSync(dirpath)) {
14
+ return;
15
+ }
16
+ this.mkdir(dirpath, path.dirname(dirpath));
17
+ } else {
18
+ // 判断第二个参数是否正常,避免调用时传入错误参数
19
+ if (dirname !== path.dirname(dirpath)) {
20
+ this.mkdir(dirpath);
21
+ return;
22
+ }
23
+ if (fs.existsSync(dirname)) {
24
+ fs.mkdirSync(dirpath);
25
+ } else {
26
+ this.mkdir(dirname, path.dirname(dirname));
27
+ fs.mkdirSync(dirpath);
28
+ }
29
+ }
30
+ };
31
+
32
+ exports.chmodPath = function(path, mode) {
33
+ let files = [];
34
+ if (fs.existsSync(path)) {
35
+ files = fs.readdirSync(path);
36
+ files.forEach((file, index) => {
37
+ const curPath = path + '/' + file;
38
+ if (fs.statSync(curPath).isDirectory()) {
39
+ this.chmodPath(curPath, mode); // 递归删除文件夹
40
+ } else {
41
+ fs.chmodSync(curPath, mode);
42
+ }
43
+ });
44
+ fs.chmodSync(path, mode);
45
+ }
46
+ };
47
+
48
+
49
+ /**
50
+ * 获取项目根目录package.json
51
+ */
52
+ exports.getPackage = function() {
53
+ const filePath = path.join(process.cwd(), 'package.json');
54
+ const json = require(filePath);
55
+
56
+ return json;
57
+ };
58
+
59
+ /**
60
+ * 获取 coredb
61
+ */
62
+ exports.getCoreDB = function() {
63
+ const coreDB = require('../lib/storage/index').JsonDB.connection('system');
64
+ return coreDB;
65
+ }
66
+
67
+ /**
68
+ * 获取 ee配置
69
+ */
70
+ exports.getEeConfig = function() {
71
+ const cdb = this.getCoreDB();
72
+ const config = cdb.getItem('config');
73
+
74
+ return config;
75
+ }
76
+
77
+ /**
78
+ * 获取 egg配置
79
+ */
80
+ exports.getEggConfig = function() {
81
+ const cdb = this.getCoreDB();
82
+ const config = cdb.getItem('config');
83
+
84
+ return config.egg;
85
+ }
86
+
87
+ /**
88
+ * 获取 数据库存储路径
89
+ */
90
+ exports.getStorageDir = function() {
91
+ const cdb = this.getCoreDB();
92
+ const dirPath = cdb.getStorageDir();
93
+
94
+ return dirPath;
95
+ }
96
+
97
+ /**
98
+ * 获取 应用程序数据目录 (开发环境时,为项目根目录)
99
+ */
100
+ exports.getAppUserDataDir = function() {
101
+ const cdb = this.getCoreDB();
102
+ const config = cdb.getItem('config');
103
+ const env = config.env;
104
+ const dir = env === 'local' || env === 'unittest' ? config.homeDir : config.appUserDataDir;
105
+ return dir;
106
+ }
107
+
108
+ /**
109
+ * 获取 日志目录
110
+ */
111
+ exports.getLogDir = function() {
112
+ const logPath = path.join(this.getAppUserDataDir(), 'logs');
113
+ return logPath;
114
+ }
115
+
116
+ /**
117
+ * 获取 socketio port
118
+ */
119
+ exports.getIpcPort = function() {
120
+ const cdb = this.getCoreDB();
121
+ const port = cdb.getItem('ipc_port');
122
+ return port;
123
+ }
124
+
125
+ /**
126
+ * 获取 ipc channel
127
+ */
128
+ exports.getIpcChannel = function() {
129
+ return constant.socketIo.channel;
130
+ }
131
+
132
+ exports.callFn = async function (fn, args, ctx) {
133
+ args = args || [];
134
+ if (!is.function(fn)) return;
135
+ if (is.generatorFunction(fn)) fn = co.wrap(fn);
136
+ return ctx ? fn.call(ctx, ...args) : fn(...args);
137
+ }
138
+
139
+ exports.middleware = function (fn) {
140
+ return is.generatorFunction(fn) ? convert(fn) : fn;
141
+ }
package/utils/wrap.js ADDED
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ const assert = require('assert');
4
+ const is = require('is-type-of');
5
+
6
+ exports.getProperties = (filepath, { caseStyle }) => {
7
+ // if caseStyle is function, return the result of function
8
+ if (is.function(caseStyle)) {
9
+ const result = caseStyle(filepath);
10
+ assert(is.array(result), `caseStyle expect an array, but got ${result}`);
11
+ return result;
12
+ }
13
+ // use default camelize
14
+ return this.defaultCamelize(filepath, caseStyle);
15
+ }
16
+
17
+ exports.defaultCamelize = (filepath, caseStyle) => {
18
+ const properties = filepath.substring(0, filepath.lastIndexOf('.')).split('/');
19
+ return properties.map(property => {
20
+ if (!/^[a-z][a-z0-9_-]*$/i.test(property)) {
21
+ throw new Error(`${property} is not match 'a-z0-9_-' in ${filepath}`);
22
+ }
23
+
24
+ property = property.replace(/[_-][a-z]/ig, s => s.substring(1).toUpperCase());
25
+ let first = property[0];
26
+ switch (caseStyle) {
27
+ case 'lower':
28
+ first = first.toLowerCase();
29
+ break;
30
+ case 'upper':
31
+ first = first.toUpperCase();
32
+ break;
33
+ case 'camel':
34
+ default:
35
+ }
36
+ return first + property.substring(1);
37
+ });
38
+ }