From bd0eb4e7914a540bfb00860df7179f02fc3832ca Mon Sep 17 00:00:00 2001 From: abearxiong Date: Tue, 13 Jan 2026 01:31:24 +0800 Subject: [PATCH] update --- src/env.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/env.ts b/src/env.ts index 642f6b4..3afabc1 100644 --- a/src/env.ts +++ b/src/env.ts @@ -5,7 +5,7 @@ import { fileURLToPath } from 'url'; // 配置类型 export type Config = { - PORT: string; + PORT?: string; // redis REDIS_HOST?: string; REDIS_PORT?: string; @@ -70,6 +70,10 @@ export type Config = { POCKETBASE_URL?: string; POCKETBASE_TOKEN?: string; + CNB_TOKEN?: string; + CNB_COOKIE?: string; + + KUBECONFIG_DATA?: string; // 其他自定义配置 [key: string]: any; } & T; @@ -132,7 +136,7 @@ export const getConfigFile = (opts?: ConfigOpts) => { if (lastLast) return lastLastPath; return ''; }; -type GetConfigOpts = ConfigOpts & { dotenvOpts?: dotenv.DotenvConfigOptions, quite?: boolean }; +type GetConfigOpts = ConfigOpts & { dotenvOpts?: dotenv.DotenvConfigOptions, quite?: boolean, showError?: boolean }; /** * 初始化配置, 不会把配置内容挂载到全局 * @param opts 配置选项 @@ -144,21 +148,34 @@ type GetConfigOpts = ConfigOpts & { dotenvOpts?: dotenv.DotenvConfigOptions, qui */ export const getConfig = (opts?: GetConfigOpts): Config => { let quiet = opts?.quite ?? true; + const showError = opts?.showError ?? false; if (opts?.dotenvOpts) { const prased = dotenv.config({ quiet, ...opts.dotenvOpts }).parsed as Config; if (prased) { return prased; } else { - throw new Error('未找到配置文件'); + if (showError) { + if(!quiet) { + console.warn('config 读取失败'); + } + throw new Error('未找到配置文件'); + } + return {} as Config; } } // 配置读取路径,3级判断 const filePath = getConfigFile(opts); - if (!quiet) { + if (!quiet && filePath) { console.log('config pathname:', filePath); } if (!filePath) { - throw new Error('未找到配置文件'); + if(showError) { + if(!quiet) { + console.warn('config 路径未找到'); + } + throw new Error('未找到配置文件'); + } + return {} as Config; } const value = dotenv.config({ quiet: true, path: filePath }).parsed as Config; return value;