befly 3.13.4 → 3.13.6

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/README.md CHANGED
@@ -81,7 +81,7 @@ my-api/
81
81
  import { Befly } from "befly";
82
82
 
83
83
  const app = new Befly();
84
- await app.start();
84
+ await app.start(Bun.env);
85
85
  ```
86
86
 
87
87
  ## 编写第一个 API
@@ -4,4 +4,4 @@
4
4
  * 支持环境分离:befly.common.json + befly.development/production.json
5
5
  */
6
6
  import type { BeflyOptions } from "./types/befly";
7
- export declare function loadBeflyConfig(): Promise<BeflyOptions>;
7
+ export declare function loadBeflyConfig(nodeEnv?: string): Promise<BeflyOptions>;
@@ -1,5 +1,6 @@
1
- import { compileDisableMenuGlobRules } from "./utils/disableMenusGlob";
2
- import { scanConfig } from "./utils/scanConfig";
1
+ import { join } from "node:path";
2
+ import { importDefault } from "./utils/importDefault";
3
+ import { mergeAndConcat } from "./utils/mergeAndConcat";
3
4
  /** 默认配置 */
4
5
  const defaultOptions = {
5
6
  // ========== 核心参数 ==========
@@ -70,47 +71,33 @@ const defaultOptions = {
70
71
  // ========== Addon 配置 ==========
71
72
  addons: {}
72
73
  };
73
- const beflyConfigCache = new Map();
74
- export async function loadBeflyConfig() {
75
- const nodeEnv = process.env.NODE_ENV || defaultOptions.nodeEnv || "development";
76
- const envSuffix = nodeEnv === "production" ? "production" : "development";
77
- const cacheKey = nodeEnv;
78
- const cached = beflyConfigCache.get(cacheKey);
79
- if (cached) {
80
- return await cached;
81
- }
82
- const promise = (async () => {
83
- // 使用 scanConfig 一次性加载并合并所有配置文件
84
- // 合并顺序:defaultOptions ← befly.common.json befly.development/production.json
85
- const config = await scanConfig({
86
- dirs: ["configs"],
87
- files: ["befly.common", `befly.${envSuffix}`],
88
- extensions: [".json"],
89
- mode: "merge",
90
- defaults: defaultOptions
91
- });
92
- // 配置校验:redis.prefix 作为 key 前缀,由 RedisHelper 统一拼接 ":"。
93
- // 因此 prefix 本身不允许包含 ":",否则会导致 key 结构出现空段或多段分隔(例如 "prefix::key"),
94
- // 在 RedisInsight 等工具里可能显示 [NO NAME] 空分组,且容易造成 key 管理混乱。
95
- const redisPrefix = config?.redis?.prefix;
96
- if (typeof redisPrefix === "string") {
97
- const trimmedPrefix = redisPrefix.trim();
98
- if (trimmedPrefix.includes(":")) {
99
- throw new Error(`配置错误:redis.prefix 不允许包含 ':'(RedisHelper 会自动拼接分隔符 ':'),请改为不带冒号的前缀,例如 'befly_demo',当前值=${redisPrefix}`);
100
- }
74
+ export async function loadBeflyConfig(nodeEnv) {
75
+ const normalizedNodeEnv = normalizeNodeEnv(nodeEnv);
76
+ const envSuffix = normalizedNodeEnv === "production" ? "production" : "development";
77
+ // 使用 importDefault 加载 configs 目录下的配置文件。
78
+ // 合并顺序:defaultOptions befly.common.json ← befly.development/production.json
79
+ const configsDir = join(process.cwd(), "configs");
80
+ const commonConfig = await importDefault(join(configsDir, "befly.common.json"), {});
81
+ const envConfig = await importDefault(join(configsDir, `befly.${envSuffix}.json`), {});
82
+ const config = mergeAndConcat(defaultOptions, commonConfig, envConfig);
83
+ // 重要:nodeEnv 的来源只从 Befly.start(env) 的入参 env.NODE_ENV 传入,避免 process.env 在 bundle 阶段被常量折叠。
84
+ // 同时确保运行时行为(例如 Bun.serve development 标记)与实际选择的配置文件一致。
85
+ config.nodeEnv = normalizedNodeEnv;
86
+ // 配置校验:redis.prefix 作为 key 前缀,由 RedisHelper 统一拼接 ":"。
87
+ // 因此 prefix 本身不允许包含 ":",否则会导致 key 结构出现空段或多段分隔(例如 "prefix::key"),
88
+ // 在 RedisInsight 等工具里可能显示 [NO NAME] 空分组,且容易造成 key 管理混乱。
89
+ const redisPrefix = config?.redis?.prefix;
90
+ if (typeof redisPrefix === "string") {
91
+ const trimmedPrefix = redisPrefix.trim();
92
+ if (trimmedPrefix.includes(":")) {
93
+ throw new Error(`配置错误:redis.prefix 不允许包含 ':'(RedisHelper 会自动拼接分隔符 ':'),请改为不带冒号的前缀,例如 'befly_demo',当前值=${redisPrefix}`);
101
94
  }
102
- // 预编译 disableMenus 的 Bun.Glob 规则:
103
- // - 提前暴露配置错误(fail-fast)
104
- // - 后续 checkMenu 会复用同一进程级缓存
105
- compileDisableMenuGlobRules(config?.disableMenus);
106
- return config;
107
- })();
108
- beflyConfigCache.set(cacheKey, promise);
109
- try {
110
- return await promise;
111
95
  }
112
- catch (error) {
113
- beflyConfigCache.delete(cacheKey);
114
- throw error;
96
+ return config;
97
+ }
98
+ function normalizeNodeEnv(nodeEnv) {
99
+ if (typeof nodeEnv === "string" && nodeEnv.trim()) {
100
+ return nodeEnv.trim();
115
101
  }
102
+ return "development";
116
103
  }