ee-core 2.0.0-beta.1 → 2.0.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.
@@ -83,25 +83,8 @@ class EeLoader {
83
83
  getServerEnv() {
84
84
  let serverEnv = this.options.env;
85
85
 
86
- const envPath = path.join(this.options.baseDir, 'config/env');
87
- if (!serverEnv && fs.existsSync(envPath)) {
88
- serverEnv = fs.readFileSync(envPath, 'utf8').trim();
89
- }
90
-
91
- if (!serverEnv) {
92
- serverEnv = process.env.EE_SERVER_ENV;
93
- }
94
-
95
86
  if (!serverEnv) {
96
- if (process.env.NODE_ENV === 'test') {
97
- serverEnv = 'unittest';
98
- } else if (process.env.NODE_ENV === 'production') {
99
- serverEnv = 'prod';
100
- } else {
101
- serverEnv = 'local';
102
- }
103
- } else {
104
- serverEnv = serverEnv.trim();
87
+ throw new Error('[core] [lib] [loader] getServerEnv serverEnv can not be empty!');
105
88
  }
106
89
 
107
90
  return serverEnv;
@@ -313,15 +296,6 @@ class EeLoader {
313
296
 
314
297
  const dirs = this.dirs = [];
315
298
 
316
- if (this.orderPlugins) {
317
- for (const plugin of this.orderPlugins) {
318
- dirs.push({
319
- path: plugin.path,
320
- type: 'plugin',
321
- });
322
- }
323
- }
324
-
325
299
  // framework or Ee path
326
300
  for (const EePath of this.EePaths) {
327
301
  dirs.push({
@@ -404,31 +378,31 @@ class EeLoader {
404
378
 
405
379
  getTypeFiles(filename) {
406
380
  const files = [ `${filename}.default` ];
407
- if (this.serverScope) files.push(`${filename}.${this.serverScope}`);
408
- if (this.serverEnv === 'default') return files;
409
-
410
381
  files.push(`${filename}.${this.serverEnv}`);
411
- if (this.serverScope) files.push(`${filename}.${this.serverScope}_${this.serverEnv}`);
382
+
412
383
  return files;
413
384
  }
414
385
 
415
386
  resolveModule(filepath) {
416
- let fullPath;
387
+ let fullpath;
417
388
  try {
418
- fullPath = require.resolve(filepath);
389
+ fullpath = require.resolve(filepath);
419
390
  } catch (e) {
420
- let jscFile = filepath + '.jsc';
421
- if (fs.existsSync(jscFile)) {
422
- return jscFile;
423
- }
424
- return undefined;
425
- }
426
391
 
427
- if (process.env.Ee_TYPESCRIPT !== 'true' && fullPath.endsWith('.ts')) {
428
- return undefined;
392
+ // 特殊后缀处理
393
+ if (filepath && (filepath.endsWith('.defalut') || filepath.endsWith('.prod'))) {
394
+ fullpath = filepath + '.jsc';
395
+ } else if (filepath && filepath.endsWith('.js')) {
396
+ fullpath = filepath + 'c';
397
+ }
398
+
399
+ if (!fs.existsSync(filepath) && !fs.existsSync(fullpath)) {
400
+ this.options.logger.warn(`[ee-core] [core/lib/loader/ee_loader] resolveModule unknow filepath: ${filepath}`)
401
+ return undefined;
402
+ }
429
403
  }
430
404
 
431
- return fullPath;
405
+ return fullpath;
432
406
  }
433
407
 
434
408
  getPkg() {
@@ -24,14 +24,7 @@ module.exports = {
24
24
 
25
25
  // Load Application config first
26
26
  const appConfig = this._preloadAppConfig();
27
- //console.log('----------------------- appConfig:', appConfig);
28
-
29
- // plugin config.default
30
- // framework config.default
31
- // app config.default
32
- // plugin config.{env}
33
- // framework config.{env}
34
- // app config.{env}
27
+
35
28
  for (const filename of this.getTypeFiles('config')) {
36
29
  for (const unit of this.getLoadUnits()) {
37
30
  const isApp = unit.type === 'app';
@@ -64,6 +57,8 @@ module.exports = {
64
57
  'config.default',
65
58
  `config.${this.serverEnv}`,
66
59
  ];
60
+
61
+
67
62
  const target = {};
68
63
  for (const filename of names) {
69
64
  const config = this._loadConfig(this.options.baseDir, filename, undefined, 'app');
@@ -78,9 +73,9 @@ module.exports = {
78
73
  let filepath = this.resolveModule(path.join(dirpath, 'config', filename));
79
74
 
80
75
  // let config.js compatible
81
- if (filename === 'config.default' && !filepath) {
82
- filepath = this.resolveModule(path.join(dirpath, 'config/config'));
83
- }
76
+ // if (filename === 'config.default' && !filepath) {
77
+ // filepath = this.resolveModule(path.join(dirpath, 'config/config'));
78
+ // }
84
79
 
85
80
  const config = this.loadFile(filepath, this.appInfo, extraInject);
86
81
 
@@ -14,6 +14,12 @@ const Module = module.constructor.length > 1
14
14
  /* istanbul ignore next */
15
15
  : BuiltinModule;
16
16
 
17
+ // Module._extensions:
18
+ // '.js': [Function (anonymous)],
19
+ // '.json': [Function (anonymous)],
20
+ // '.node': [Function: func],
21
+ // '.jsc': [Function (anonymous)]
22
+
17
23
  module.exports = {
18
24
  extensions: Module._extensions,
19
25
 
@@ -20,7 +20,8 @@ class Appliaction extends EeApp {
20
20
  appUserData: app.getPath('userData'),
21
21
  appVersion: app.getVersion(),
22
22
  isPackaged: app.isPackaged,
23
- execDir: app.getAppPath()
23
+ execDir: app.getAppPath(),
24
+ isEncrypted: false
24
25
  }
25
26
 
26
27
  // argv
@@ -42,9 +43,9 @@ class Appliaction extends EeApp {
42
43
 
43
44
  // Use encryption, base directory is public/electron
44
45
  const encryptDir = path.join(app.getAppPath(), 'public', 'electron');
45
- let isEncrypted = fs.existsSync(encryptDir);
46
- if (options.env == 'prod' && isEncrypted) {
46
+ if (options.env == 'prod' && fs.existsSync(encryptDir)) {
47
47
  options.baseDir = encryptDir;
48
+ options.isEncrypted = true;
48
49
  }
49
50
 
50
51
  // normalize env
@@ -59,6 +60,7 @@ class Appliaction extends EeApp {
59
60
  env.HOT_RELOAD = hotReload;
60
61
  env.EE_EXEC_DIR = options.execDir;
61
62
  env.EE_IS_PACKAGED = options.isPackaged;
63
+ env.EE_IS_ENCRYPTED = options.isEncrypted;
62
64
  env.EE_DATABASE_DIR = null;
63
65
  env.EE_MAIN_PORT = null;
64
66
  env.EE_SOCKET_PORT = null;
@@ -0,0 +1,16 @@
1
+ const Log = require('../log');
2
+
3
+ /**
4
+ * 捕获异常
5
+ */
6
+ exports.start = function() {
7
+ process.on('uncaughtException', this.uncaughtExceptionHandler);
8
+ }
9
+
10
+ exports.uncaughtExceptionHandler = function(err) {
11
+ if (!(err instanceof Error)) {
12
+ err = new Error(String(err));
13
+ }
14
+
15
+ Log.coreLogger.error(err);
16
+ }
@@ -0,0 +1,23 @@
1
+ const Exception = require('ee-core/module/exception');
2
+ Exception.start();
3
+
4
+ const Loader = require('ee-core/module/loader');
5
+ const Log = require('ee-core/module/log');
6
+
7
+ class ChildApp {
8
+ constructor() {
9
+ const args = process.argv[2];
10
+ this.opt = args ? JSON.parse(args) : {};
11
+ }
12
+
13
+ run () {
14
+ Log.info('[child-process] run');
15
+
16
+ const jobFile = this.opt.jobPath;
17
+
18
+ Loader.loadJobFile(jobFile);
19
+ }
20
+ }
21
+
22
+ const app = new ChildApp()
23
+ app.run();
@@ -1,20 +1,25 @@
1
+ const path = require('path');
1
2
  const { fork } = require('child_process');
2
3
 
3
4
  class ForkProcess {
4
5
  constructor(host, modulePath, processArgs = [], processOptions = {}) {
5
6
  this.host = host;
6
7
  this.modulePath = modulePath;
7
- this.args = processArgs;
8
+ this.args;
8
9
  this.options = processOptions;
9
10
  this.sleeping = false;
10
11
  this.activitiesCount = 0;
11
12
  this.activitiesMap = new Map();
12
13
 
13
- this.child = fork(
14
- this.modulePath,
15
- this.args,
16
- this.options
17
- );
14
+ // 传递给子进程的参数
15
+ let scriptArgs = {
16
+ jobPath: modulePath
17
+ }
18
+ processArgs.push(JSON.stringify(scriptArgs));
19
+ this.args = processArgs;
20
+
21
+ const appPath = path.join(__dirname, 'app.js');
22
+ this.child = fork(appPath, this.args, this.options);
18
23
 
19
24
  this.pid = this.child.pid;
20
25
  this._init();
@@ -13,8 +13,10 @@ class ChildJob {
13
13
  * @param {Object} opt - child process options
14
14
  */
15
15
  constructor(name, filepath, opt = {}) {
16
+ // todo
17
+ //processArgs: Ps.isDev() ? [`--inspect=${Constants.jobs.inspectStartIndex}`] : [],
16
18
  let options = Object.assign({
17
- processArgs: Ps.isDev() ? [`--inspect=${Constants.jobs.inspectStartIndex}`] : [],
19
+ processArgs: Ps.isDev() ? [] : [],
18
20
  processOptions: {
19
21
  //cwd: path.dirname(filepath),
20
22
  env: Ps.allEnv(),
@@ -1,6 +1,5 @@
1
1
  const path = require('path');
2
2
  const fs = require('fs');
3
- const assert = require('assert');
4
3
  const RendererJob = require('./renderer');
5
4
  const ChildJob = require('./child');
6
5
  const Utils = require('../utils');
@@ -25,12 +24,14 @@ class Jobs {
25
24
 
26
25
  const isAbsolute = path.isAbsolute(this.path);
27
26
  if (!isAbsolute) {
28
- this.path = path.join(Utils.getBaseDir(), 'jobs', this.path);
27
+ this.path = path.join(Utils.getBaseDir(), this.path);
29
28
  }
30
29
  const filepath = Loader.resolveModule(this.path);
31
30
 
32
- assert(fs.existsSync(filepath), `file ${filepath} not exists`);
33
-
31
+ if (!fs.existsSync(filepath)) {
32
+ throw new Error(`[ee-core] [jobs-create] file ${this.path} not exists`);
33
+ }
34
+
34
35
  this.path = filepath;
35
36
  if (this.type == 'child') {
36
37
  this.instance = new ChildJob(name, filepath, this.childOptions);
@@ -3,6 +3,7 @@ const fs = require('fs');
3
3
  const path = require('path');
4
4
  const UtilsCore = require('../../core/lib/utils');
5
5
  const Ps = require('../utils/ps');
6
+ const Log = require('../log');
6
7
 
7
8
  module.exports = {
8
9
 
@@ -14,10 +15,17 @@ module.exports = {
14
15
  * @return {Object} exports
15
16
  * @since 1.0.0
16
17
  */
17
- loadFile (filepath, ...inject) {
18
+ loadOneFile (filepath, ...inject) {
19
+ const isAbsolute = path.isAbsolute(filepath);
20
+ if (!isAbsolute) {
21
+ filepath = path.join(Ps.getBaseDir(), filepath);
22
+ }
23
+
18
24
  filepath = filepath && this.resolveModule(filepath);
19
- if (!filepath) {
20
- return null;
25
+ if (!fs.existsSync(filepath)) {
26
+ let errorMsg = `[ee-core] [module/loader/index] loadOneFile ${filepath} does not exist`;
27
+ Log.coreLogger.error(errorMsg);
28
+ throw new Error(errorMsg);
21
29
  }
22
30
 
23
31
  const ret = UtilsCore.loadFile(filepath);
@@ -28,26 +36,51 @@ module.exports = {
28
36
  },
29
37
 
30
38
  /**
31
- * 模块的绝对路径
39
+ * 加载job文件
40
+ *
41
+ * @param {String} filepath - fullpath
42
+ * @param {Array} inject - pass rest arguments into the function when invoke
43
+ * @return {Object} exports
44
+ * @since 1.0.0
32
45
  */
33
- resolveModule(filepath) {
34
- const isAbsolute = path.isAbsolute(filepath);
35
- if (!isAbsolute) {
36
- filepath = path.join(Ps.getBaseDir(), 'jobs', filepath);
46
+ loadJobFile (filepath, ...inject) {
47
+ if (!fs.existsSync(filepath)) {
48
+ let warnMsg = `[ee-core] [module/loader/index] loadJobFile ${filepath} does not exist`;
49
+ Log.coreLogger.error(warnMsg);
50
+ }
51
+
52
+ const ret = UtilsCore.loadFile(filepath);
53
+ if (is.function(ret) && !is.class(ret) && !UtilsCore.isBytecodeClass(ret)) {
54
+ ret = ret(...inject);
37
55
  }
56
+ return ret;
57
+ },
38
58
 
39
- let fullPath;
59
+ /**
60
+ * 模块的绝对路径
61
+ * @param {String} filepath - fullpath
62
+ */
63
+ resolveModule(filepath) {
64
+ let fullpath;
40
65
  try {
41
- fullPath = require.resolve(filepath);
66
+ fullpath = require.resolve(filepath);
42
67
  } catch (e) {
43
- let jscFile = filepath + '.jsc';
44
- if (fs.existsSync(jscFile)) {
45
- return jscFile;
68
+
69
+ // 特殊后缀处理
70
+ if (filepath && (filepath.endsWith('.defalut') || filepath.endsWith('.prod'))) {
71
+ fullpath = filepath + '.jsc';
72
+ } else if (filepath && filepath.endsWith('.js')) {
73
+ fullpath = filepath + 'c';
74
+ }
75
+
76
+ if (!fs.existsSync(filepath) && !fs.existsSync(fullpath)) {
77
+ let files = { filepath, fullpath }
78
+ Log.coreLogger.warn(`[ee-core] [module-loader-resolveModule] unknow filepath: ${files}`)
79
+ return undefined;
46
80
  }
47
- return undefined;
48
81
  }
49
82
 
50
- return fullPath;
83
+ return fullpath;
51
84
  },
52
85
 
53
86
  /**
@@ -57,16 +90,35 @@ module.exports = {
57
90
  * @return {Object} exports
58
91
  * @since 1.0.0
59
92
  */
60
- requireModule (filepath) {
61
- filepath = filepath && this.resolveModule(filepath);
62
- if (!filepath) {
63
- return null;
93
+ requireModule (filepath, type = '') {
94
+ let fullpath;
95
+ const isAbsolute = path.isAbsolute(filepath);
96
+ if (!isAbsolute) {
97
+ filepath = path.join(Ps.getBaseDir(), type, filepath);
64
98
  }
65
- const ret = UtilsCore.loadFile(filepath);
99
+
100
+ fullpath = this.resolveModule(filepath);
101
+ if (!fs.existsSync(fullpath)) {
102
+ let errorMsg = `[ee-core] [module-loader-index] requireModule filepath: ${filepath} does not exist`;
103
+ Log.coreLogger.error(errorMsg);
104
+ }
105
+ const ret = UtilsCore.loadFile(fullpath);
66
106
 
67
107
  return ret;
68
108
  },
69
109
 
110
+ /**
111
+ * 加载jobs模块(子进程中使用)
112
+ *
113
+ * @param {String} filepath - fullpath
114
+ * @return {Object} exports
115
+ * @since 1.0.0
116
+ */
117
+ requireJobsModule (filepath) {
118
+ const ret = this.requireModule(filepath, 'jobs');
119
+
120
+ return ret;
121
+ },
70
122
  }
71
123
 
72
124
 
@@ -4,8 +4,12 @@ const DB = {};
4
4
 
5
5
  DB.connection = function (database, options = {}) {
6
6
  let driver = options.driver || 'jsondb';
7
+
8
+ // 兼容之前api
9
+ driver = driver == 'lowdb' ? 'jsondb' : driver;
10
+
7
11
  let opt = options.default || {};
8
- if (!_.includes(['jsondb', 'lowdb', 'sqlite'], driver)) {
12
+ if (!_.includes(['jsondb', 'sqlite'], driver)) {
9
13
  assert(database, `db driver ${driver} is not supported`);
10
14
  }
11
15
  if (_.isEmpty(database)) {
@@ -21,9 +25,7 @@ DB.connection = function (database, options = {}) {
21
25
  const SqliteStorage = require('./sqliteStorage');
22
26
  storage = new SqliteStorage(database, opt);
23
27
  break;
24
- default:
25
- const JsondbStorage = require('./jsondbStorage');
26
- storage = new JsondbStorage(database);
28
+ default:
27
29
  }
28
30
 
29
31
  return storage;
@@ -7,7 +7,7 @@ class FileSync extends Base {
7
7
  if (fs.existsSync(this.source)) {
8
8
  // Read database
9
9
  try {
10
- const data = fs.readFileSync(this.source, {encoding: 'utf8'})
10
+ const data = fs.readFileSync(this.source, {encoding: 'utf8'}).trim()
11
11
 
12
12
  // Handle blank file
13
13
  return data ? this.deserialize(data) : this.defaultValue
@@ -19,13 +19,13 @@ class FileSync extends Base {
19
19
  }
20
20
  } else {
21
21
  // Initialize
22
- fs.writeFileSync(this.source, this.serialize(this.defaultValue))
22
+ fs.writeFileSync(this.source, this.serialize(this.defaultValue), {flag:'w+'})
23
23
  return this.defaultValue
24
24
  }
25
25
  }
26
26
 
27
27
  write(data) {
28
- return fs.writeFileSync(this.source, this.serialize(data))
28
+ return fs.writeFileSync(this.source, this.serialize(data), {flag:'w+'})
29
29
  }
30
30
  }
31
31
 
@@ -20,7 +20,6 @@ class JsondbStorage {
20
20
  const storageDir = Ps.getStorageDir();
21
21
  if (!fs.existsSync(storageDir)) {
22
22
  Helper.mkdir(storageDir);
23
- Helper.chmodPath(storageDir, '777');
24
23
  }
25
24
 
26
25
  this.db = this.table(name);
@@ -49,8 +49,8 @@ exports.getRandomString = function() {
49
49
  /**
50
50
  * 创建文件夹
51
51
  */
52
- exports.mkdir = function(filepath) {
53
- mkdirp.sync(path.dirname(filepath));
52
+ exports.mkdir = function(filepath, opt = {}) {
53
+ mkdirp.sync(filepath, opt);
54
54
  return
55
55
  }
56
56
 
@@ -112,12 +112,19 @@ exports.getRootDir = function () {
112
112
  }
113
113
 
114
114
  /**
115
- * 获取 base目录
115
+ * 获取base目录
116
116
  */
117
117
  exports.getBaseDir = function() {
118
118
  return process.env.EE_BASE_DIR;
119
119
  }
120
120
 
121
+ /**
122
+ * 获取electron目录
123
+ */
124
+ exports.getElectronDir = function() {
125
+ return process.env.EE_BASE_DIR;
126
+ }
127
+
121
128
  /**
122
129
  * 获取 appUserData目录
123
130
  */
@@ -171,5 +178,19 @@ exports.getHttpPort = function () {
171
178
  * 是否打包
172
179
  */
173
180
  exports.isPackaged = function () {
174
- return process.env.EE_IS_PACKAGED;
181
+ return process.env.EE_IS_PACKAGED === 'true';
182
+ }
183
+
184
+ /**
185
+ * 是否加密
186
+ */
187
+ exports.isEncrypted = function () {
188
+ return process.env.EE_IS_ENCRYPTED === 'true';
189
+ }
190
+
191
+ /**
192
+ * 是否热重启
193
+ */
194
+ exports.isHotReload = function () {
195
+ return process.env.HOT_RELOAD === 'true';
175
196
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-beta.2",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {