@x-9lab/xlab 1.0.5 → 1.0.8

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.
@@ -0,0 +1,3 @@
1
+ /**处理根目录下可能存在的配置文件 */
2
+ declare function processDotFile(env: string, logger: any): void;
3
+ export { processDotFile };
@@ -47,6 +47,8 @@ declare global {
47
47
  * @param conf 配置项
48
48
  */
49
49
  function setSysConfig(conf: XLab.IConfig): void;
50
+ /**全局注入挂在对象 */
51
+ var XLAB: Record<string, XLab.JsonValue>;
50
52
  namespace XLab {
51
53
  /**JSON 基本数据类型 */
52
54
  type JsonValue = boolean | string | number | null | undefined | JsonArray | JsonObject;
@@ -142,8 +144,6 @@ declare global {
142
144
  pathReplaceRegExp?: string;
143
145
  /**移动端入口文件地址(旧版逻辑) */
144
146
  routeMobile?: string;
145
- /**不同端入口地址设置 */
146
- launchRouter?: Record<string, string>;
147
147
  /**定时任务设置 */
148
148
  cron?: {
149
149
  def?: number;
package/README.md CHANGED
@@ -18,6 +18,11 @@ X-9lab 通用服务端
18
18
  模块会自动检测执行根目录中是否存在 `xlab.config.js` 文件。如存在该文件则会使用该文件为模块的启动配置文件。支持的配置项:
19
19
  1. `watch` 是否开启 watch 模式
20
20
  1. `business` 业务目录名称。注意当前只支持根目录下的文件夹
21
+ - 配置文件 `.xlab`
22
+ 该文件用于定义系统中的全局变量,模块会自动检测执行根目录中是否存在 `.xlab` 文件。该类型文件一般用于定义一些不方便在代码中声明的敏感数据,因此 `.xlab` 及附属的环境文件 **不应该** 被提交到仓库中。
23
+ - 使用 `<key>=<value>` 的方式定义数据
24
+ - 文件中的数据会被加载到 `global.XLAB` 对象中
25
+ - `.xlab` 为生产环境定义,`.xlab.development` 为开发环境定义,`.xlab.sandbox` 为沙箱环境定义
21
26
  - `watch` 模式
22
27
  `xlab` 支持自动重启业务,开发中使用该功能可以减少大量手动重启业务的操作。
23
28
  启用方式:
@@ -179,7 +184,7 @@ X-9lab 通用服务端
179
184
  |clearLocalStorage|`boolean`||是否每次都强制清除 LocalStorage|
180
185
  |pathReplaceRegExp|`string`||处理代理过来多余的地址层级路径替换判断正则|
181
186
  |routeMobile|`string`||移动端入口文件地址(旧版逻辑)|
182
- |launchRouter|`Record<string, string>`||不同端入口地址设置|
187
+ |launchRouter|`Record<string, string>`||不同端入口地址设置, 由 `@x-drive/launch-detect` 提供支持|
183
188
  |cron|`{def?: number;}`|{"def": 60}|定时任务设置|
184
189
  |injection|`string[]`|[]|注入参数列表|
185
190
  |indexPageCacheTime|`number`||首页缓存时间|
@@ -19,7 +19,6 @@ const CONFIG = {
19
19
  "enableCron": false,
20
20
  "middleware": [],
21
21
  "strictSSL": false,
22
- "launchRouter": {},
23
22
  "root": "public"
24
23
  };
25
24
  const _default = CONFIG;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "processDotFile", {
6
+ enumerable: true,
7
+ get: ()=>processDotFile
8
+ });
9
+ const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
10
+ const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
11
+ const _utils = require("@x-drive/utils");
12
+ function _interopRequireDefault(obj) {
13
+ return obj && obj.__esModule ? obj : {
14
+ default: obj
15
+ };
16
+ }
17
+ /**行分割正则 */ const LINE_SPLIT_REGEXP = /[\s]?=[\s]?/;
18
+ /**处理根目录下可能存在的配置文件 */ function processDotFile(env, logger) {
19
+ env = env.toLowerCase();
20
+ if (env === "production") {
21
+ env = "";
22
+ } else {
23
+ env = `.${env}`;
24
+ }
25
+ const dotFile = _path.default.resolve(process.cwd(), `.xlab${env}`);
26
+ try {
27
+ const fileStr = _fs.default.readFileSync(dotFile, "utf8");
28
+ const lines = fileStr.split("\n");
29
+ for(let i = 0; i < lines.length; i++){
30
+ const line = lines[i];
31
+ const linePart = line.split(LINE_SPLIT_REGEXP);
32
+ if ((0, _utils.isArray)(linePart) && linePart.length === 2) {
33
+ global.XLAB[linePart[0]] = JSON.parse(linePart[1]);
34
+ }
35
+ }
36
+ } catch (e) {
37
+ if (e.code !== "ENOENT") {
38
+ logger.error(e);
39
+ }
40
+ }
41
+ }
package/dist/global.js CHANGED
@@ -66,3 +66,4 @@ global.getSysConfig = getSysConfig;
66
66
  (0, _config.update)(conf);
67
67
  }
68
68
  global.setSysConfig = setSysConfig;
69
+ global.XLAB = {};
package/dist/server.js CHANGED
@@ -17,6 +17,7 @@ require("./global");
17
17
  const _log = require("./components/log");
18
18
  const _cron = require("./components/cron");
19
19
  const _defaultXConfig = /*#__PURE__*/ _interopRequireDefault(require("./default-x-config"));
20
+ const _dotFile = require("./dot-file");
20
21
  const _koaStatic = /*#__PURE__*/ _interopRequireDefault(require("koa-static"));
21
22
  const _koaRouter = /*#__PURE__*/ _interopRequireDefault(require("koa-router"));
22
23
  const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
@@ -72,6 +73,7 @@ function _interopRequireWildcard(obj, nodeInterop) {
72
73
  return newObj;
73
74
  }
74
75
  const logger = _log.globalLog.getLogger("system");
76
+ (0, _dotFile.processDotFile)(_config.default.env, logger);
75
77
  // 只在 master 上输出的日志
76
78
  global.masterLog = _config.log;
77
79
  // 服务实例
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@x-9lab/xlab",
3
- "version": "1.0.5",
3
+ "version": "1.0.8",
4
4
  "description": "9lab 服务端模块",
5
- "versionDesc": "修改配置文件名称, 修改服务端主要业务目录",
5
+ "versionDesc": "支持根目录下的点配置文件",
6
6
  "scripts": {
7
7
  "dev": "swc src -D ./src/bin --config-file .swcrc -d dist -w",
8
8
  "compile": "swc src -D ./src/bin --config-file .swcrc -d dist",
@@ -33,7 +33,7 @@
33
33
  "koa-router": "11.0.1",
34
34
  "http-proxy": "1.18.1",
35
35
  "koa-compress": "5.1.0",
36
- "@x-drive/utils": "1.1.22"
36
+ "@x-drive/utils": "1.1.23"
37
37
  },
38
38
  "devDependencies": {
39
39
  "co": "4.6.0",