befly 3.13.3 → 3.13.5

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.
@@ -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,24 @@ const defaultOptions = {
70
71
  // ========== Addon 配置 ==========
71
72
  addons: {}
72
73
  };
73
- const beflyConfigCache = new Map();
74
74
  export async function loadBeflyConfig() {
75
- const nodeEnv = process.env.NODE_ENV || defaultOptions.nodeEnv || "development";
75
+ const nodeEnv = process.env.NODE_ENV || "development";
76
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
- }
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
+ // 配置校验:redis.prefix 作为 key 前缀,由 RedisHelper 统一拼接 ":"。
84
+ // 因此 prefix 本身不允许包含 ":",否则会导致 key 结构出现空段或多段分隔(例如 "prefix::key"),
85
+ // RedisInsight 等工具里可能显示 [NO NAME] 空分组,且容易造成 key 管理混乱。
86
+ const redisPrefix = config?.redis?.prefix;
87
+ if (typeof redisPrefix === "string") {
88
+ const trimmedPrefix = redisPrefix.trim();
89
+ if (trimmedPrefix.includes(":")) {
90
+ throw new Error(`配置错误:redis.prefix 不允许包含 ':'(RedisHelper 会自动拼接分隔符 ':'),请改为不带冒号的前缀,例如 'befly_demo',当前值=${redisPrefix}`);
101
91
  }
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
- }
112
- catch (error) {
113
- beflyConfigCache.delete(cacheKey);
114
- throw error;
115
92
  }
93
+ return config;
116
94
  }