ee-core 2.11.1 → 2.12.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.
package/config/cache.js CHANGED
@@ -1,27 +1,47 @@
1
1
  const EE = require('../ee');
2
+ const { ConfigLoader } = require('./config_loader');
3
+
4
+ const Instance = {
5
+ config: null,
6
+ };
2
7
 
3
8
  const conf = {
4
9
 
10
+ loadConfig() {
11
+ const configLoader = new ConfigLoader();
12
+ Instance["config"] = configLoader.load();
13
+ return Instance["config"];
14
+ },
15
+
5
16
  /**
6
17
  * 获取 内存中的config
7
18
  */
8
- _getConfig(withError = true) {
19
+ _getConfig() {
9
20
  const { CoreApp } = EE;
10
- if (!CoreApp && withError) {
11
- throw new Error(`[ee-core] [config] Frame initialization is not complete !`);
21
+
22
+ if (CoreApp && CoreApp.config) {
23
+ return CoreApp.config;
12
24
  }
13
- if (!CoreApp) {
14
- return null;
25
+
26
+ if (Instance["config"]) {
27
+ return Instance["config"];
28
+ }
29
+
30
+ // 重新加载 config
31
+ this.loadConfig();
32
+
33
+ if (!Instance["config"]) {
34
+ throw new Error('[ee-core] [config] config is not loaded!');
15
35
  }
16
36
 
17
- return CoreApp.config;
37
+ return Instance["config"];
18
38
  },
19
39
 
20
40
  /**
21
41
  * all
22
42
  */
23
- all(withError = true) {
24
- return this._getConfig(withError);
43
+ all() {
44
+ return this._getConfig();
25
45
  },
26
46
 
27
47
  /**
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ const debug = require('debug')('ee-core:config:config_loader');
4
+ const path = require('path');
5
+ const Ps = require('../ps');
6
+ const extend = require('../utils/extend');
7
+ const Timing = require('../core/lib/utils/timing');
8
+ const Loader= require('../loader');
9
+
10
+ class ConfigLoader {
11
+ constructor() {
12
+ this.timing = new Timing();
13
+ this.config = {};
14
+ }
15
+
16
+ /**
17
+ * Load config/config.xxx.js
18
+ */
19
+ load() {
20
+ this.timing.start('Load Config');
21
+
22
+ // Load Application config
23
+ const appConfig = this._AppConfig();
24
+ debug("[load] appConfig: %o", appConfig);
25
+ this.config = appConfig;
26
+
27
+ this.timing.end('Load Config');
28
+ return this.config;
29
+ }
30
+
31
+ _AppConfig() {
32
+ const names = [
33
+ 'config.default',
34
+ `config.${Ps.env()}`,
35
+ ];
36
+ const target = {};
37
+ for (const filename of names) {
38
+ const config = this._loadConfig(Ps.getElectronDir(), filename);
39
+ extend(true, target, config);
40
+ }
41
+ return target;
42
+ }
43
+
44
+ _loadConfig(dirpath, filename) {
45
+ const appInfo = {
46
+ name: Ps.appName(),
47
+ baseDir: Ps.getElectronDir(),
48
+ electronDir: Ps.getElectronDir(),
49
+ env: Ps.env(),
50
+ home: Ps.getHomeDir(),
51
+ root: Ps.getRootDir(),
52
+ appUserDataDir: Ps.getAppUserDataDir(),
53
+ userHome: Ps.getUserHomeDir(),
54
+ appVersion: Ps.appVersion(),
55
+ isPackaged: Ps.isPackaged(),
56
+ isEncrypted: Ps.isEncrypted(),
57
+ execDir: Ps.getExecDir(),
58
+ }
59
+ const filepath = path.join(dirpath, 'config', filename);
60
+ const config = Loader.loadOneFile(filepath, appInfo);
61
+ debug("[_loadConfig] filepath: %s", filepath);
62
+ if (!config) return null;
63
+
64
+ return config;
65
+ }
66
+ }
67
+
68
+ module.exports = {
69
+ ConfigLoader
70
+ };
package/config/index.js CHANGED
@@ -1,66 +1,21 @@
1
- const Storage = require('../storage');
2
1
  const ConfigCache = require('./cache');
3
- var SystemDb = undefined;
4
2
 
5
3
  const Cfg = {
6
4
 
7
- /**
8
- * 获取 coredb
9
- */
10
- _getCoreDB() {
11
- // [todo] 要么每次new对象,要么所有地方都用同一个实例,否则会出现数据无法刷新的情况
12
- SystemDb = Storage.connection('system');
13
- return SystemDb;
14
- },
15
-
16
5
  /**
17
6
  * all
18
7
  */
19
- all(fromCache = true) {
20
- if (fromCache === true) {
21
- // 如果子进程
22
- const cacheValue = ConfigCache.all();
23
- return cacheValue;
24
- }
25
- const cdb = this._getCoreDB();
26
- const config = cdb.getItem('config');
27
-
28
- return config;
29
- },
30
-
31
- /**
32
- * setAll
33
- */
34
- setAll(value) {
35
- const cdb = this._getCoreDB();
36
- cdb.setItem('config', value);
37
-
38
- return;
39
- },
40
-
41
- /**
42
- * setValue
43
- */
44
- setValue(key, value) {
45
- const cdb = this._getCoreDB();
46
- cdb.setConfigItem(key, value);
47
-
48
- return;
8
+ all() {
9
+ const cacheValue = ConfigCache.all();
10
+ return cacheValue;
49
11
  },
50
12
 
51
13
  /**
52
14
  * getValue
53
15
  */
54
- getValue(key, fromCache = true) {
55
- if (fromCache === true) {
56
- const cacheValue = ConfigCache.getValue(key);
57
- return cacheValue;
58
- }
59
-
60
- const cdb = this._getCoreDB();
61
- let v = cdb.getConfigItem(key);
62
-
63
- return v;
16
+ getValue(key) {
17
+ const cacheValue = ConfigCache.getValue(key);
18
+ return cacheValue;
64
19
  },
65
20
 
66
21
  /**
package/ee/application.js CHANGED
@@ -58,6 +58,7 @@ class Appliaction extends EeApp {
58
58
 
59
59
  // normalize env
60
60
  env.EE_APP_NAME = options.appName;
61
+ env.EE_APP_VERSION = options.appVersion;
61
62
  env.EE_HOME = options.homeDir;
62
63
  env.EE_BASE_DIR = options.baseDir;
63
64
  env.EE_SERVER_ENV = options.env;
package/ee/baseApp.js CHANGED
@@ -7,7 +7,6 @@ const HttpClient = require('../httpclient');
7
7
  const HTTPCLIENT = Symbol('EeApplication#httpclient');
8
8
  const LOGGERS = Symbol('EeApplication#loggers');
9
9
  const Log = require('../log');
10
- const Conf = require('../config');
11
10
 
12
11
  class BaseApp extends EeAppCore {
13
12
  constructor (options = {}) {
@@ -15,10 +14,6 @@ class BaseApp extends EeAppCore {
15
14
  super(options);
16
15
 
17
16
  this.loader.loadConfig();
18
-
19
-
20
- // [todo] 缓存配置
21
- Conf.setAll(this.config);
22
17
 
23
18
  this.loader.load();
24
19
 
package/ee/eeApp.js CHANGED
@@ -49,9 +49,6 @@ class EeApp extends BaseApp {
49
49
  process.env.EE_HTTP_PORT = httpPort;
50
50
  this.config.httpServer.port = httpPort;
51
51
  }
52
-
53
- // [todo] 更新db配置 (system.json 不再主进程中使用了,后续可能在子进程中使用)
54
- Conf.setAll(this.config);
55
52
  }
56
53
 
57
54
  /**
package/jobs/child/app.js CHANGED
@@ -58,7 +58,7 @@ class ChildApp {
58
58
 
59
59
  } else if (is.function(mod)) {
60
60
  mod(jobParams);
61
- }
61
+ }
62
62
  }
63
63
  }
64
64
 
@@ -7,6 +7,7 @@ const Ps = require('../../ps');
7
7
  const Channel = require('../../const/channel');
8
8
  const Helper = require('../../utils/helper');
9
9
  const Loader = require('../../loader');
10
+ const extend = require('../../utils/extend');
10
11
 
11
12
  class ForkProcess {
12
13
  constructor(host, opt = {}) {
@@ -18,8 +19,7 @@ class ForkProcess {
18
19
  cwd = path.join(Ps.getHomeDir(), '..');
19
20
  }
20
21
 
21
- // TODO Object.assign 只能单层对象结构,多层的对象会直接覆盖
22
- let options = Object.assign({
22
+ let options = extend(true,{
23
23
  processArgs: {},
24
24
  processOptions: {
25
25
  cwd: cwd,
package/loader/index.js CHANGED
@@ -3,7 +3,6 @@ const fs = require('fs');
3
3
  const path = require('path');
4
4
  const UtilsCore = require('../core/lib/utils');
5
5
  const Ps = require('../ps');
6
- const Log = require('../log');
7
6
 
8
7
  module.exports = {
9
8
 
@@ -27,7 +26,7 @@ module.exports = {
27
26
  throw new Error(errorMsg);
28
27
  }
29
28
 
30
- const ret = UtilsCore.loadFile(filepath);
29
+ let ret = UtilsCore.loadFile(filepath);
31
30
  if (is.function(ret) && !is.class(ret) && !UtilsCore.isBytecodeClass(ret)) {
32
31
  ret = ret(...inject);
33
32
  }
package/log/logger.js CHANGED
@@ -3,8 +3,9 @@ const assert = require('assert');
3
3
  const dayjs = require('dayjs');
4
4
  const path = require('path');
5
5
  const Ps = require('../ps');
6
- const Conf = require('../config');
7
6
  const ConfigCache = require('../config/cache');
7
+ const extend = require('../utils/extend');
8
+
8
9
  let LogDate = 0;
9
10
  const TmpFileName = {
10
11
  appLogName: '',
@@ -39,17 +40,13 @@ module.exports = {
39
40
  coreLogger: {},
40
41
  allowDebugAtProd: false,
41
42
  enablePerformanceTimer: false,
42
- rotator: 'none',
43
+ rotator: 'day',
43
44
  },
44
45
  customLogger: {}
45
46
  }
46
47
 
47
- // 先从 cache 中读配置且不能抛出错误,如果没有从文件中读(子进程无法获取 cache)
48
- let sysConfig = ConfigCache.all(false);
49
- if (!sysConfig) {
50
- sysConfig = Conf.all(false);
51
- }
52
- opt = Object.assign(defaultConfig, {
48
+ const sysConfig = ConfigCache.all();
49
+ opt = extend(true, defaultConfig, {
53
50
  logger: sysConfig.logger,
54
51
  customLogger: sysConfig.customLogger || {}
55
52
  });
@@ -40,7 +40,6 @@ class ChildMessage {
40
40
  */
41
41
  sendErrorToTerminal(err) {
42
42
  let errTips = (err && typeof err == 'object') ? err.toString() : '';
43
- errTips += ' Error !!! Please See file ee-core.log or ee-error-xxx.log for details !'
44
43
  let message = {
45
44
  channel: Channel.process.showException,
46
45
  data: errTips
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "2.11.1",
3
+ "version": "2.12.0",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/ps/index.js CHANGED
@@ -134,6 +134,13 @@ exports.appName = function() {
134
134
  return process.env.EE_APP_NAME;
135
135
  }
136
136
 
137
+ /**
138
+ * app version
139
+ */
140
+ exports.appVersion = function() {
141
+ return process.env.EE_APP_VERSION;
142
+ }
143
+
137
144
  /**
138
145
  * 获取home路径
139
146
  */
@@ -19,7 +19,7 @@ class FileSync extends Base {
19
19
  let canDeserialized = this._canDeserialized(data);
20
20
  if (!canDeserialized) {
21
21
  let errMessage = `[ee-core] [storage/jsondb] malformed json in file: ${this.source}\n${data}`;
22
- Log.coreLogger.error(errMessage);
22
+ console.error(errMessage);
23
23
 
24
24
  // 是否文件结尾多一个括号,尝试处理
25
25
  data = data.trim().slice(0, -1);
@@ -34,7 +34,7 @@ class FileSync extends Base {
34
34
  this._fsWrite(this.defaultValue);
35
35
  }
36
36
  errMessage = '[ee-core] [storage/jsondb] malformed json that cannot be handled!';
37
- Log.coreLogger.error(errMessage);
37
+ console.error(errMessage);
38
38
  }
39
39
  }
40
40
  const value = canDeserialized ? this.deserialize(data) : this.defaultValue;
@@ -53,7 +53,7 @@ class FileSync extends Base {
53
53
  _fsWrite(data) {
54
54
  const isObject = Object.prototype.toString.call(data) === '[object Object]';
55
55
  if (!isObject) {
56
- Log.coreLogger.error('[ee-core] [storage/jsondb] Variable is not an object :', data);
56
+ console.error('[ee-core] [storage/jsondb] Variable is not an object :', data);
57
57
  return
58
58
  }
59
59