refactor(env): centralize app environment detection

This commit is contained in:
2026-06-17 15:31:15 +08:00
parent 977c0905bd
commit a6ee0d5e24
4 changed files with 40 additions and 20 deletions
+28 -7
View File
@@ -3,13 +3,34 @@
*
* 原始 Dart: lib/utils/app_env_util.dart
*
* 替代 `AppEnvUtil.isDevelopment`:基于 Next.js 注入的 `process.env.NODE_ENV`
* 基于 `.env*` 中的 `NEXT_PUBLIC_APP_ENV` 判断当前业务环境
*
* 注意:该变量带 `NEXT_PUBLIC_` 前缀,可同时用于服务端和客户端代码。
*/
export const isDevelopment = (): boolean =>
process.env.NODE_ENV !== "production";
export type AppEnv = "development" | "test" | "production";
export const isProduction = (): boolean =>
process.env.NODE_ENV === "production";
export class AppEnvUtil {
private constructor() {}
export const isTest = (): boolean =>
process.env.NODE_ENV === "test";
static get current(): AppEnv {
const env = process.env.NEXT_PUBLIC_APP_ENV;
if (env === "production" || env === "test") return env;
return "development";
}
static isDevelopment(): boolean {
return AppEnvUtil.current === "development";
}
static isProduction(): boolean {
return AppEnvUtil.current === "production";
}
static isTest(): boolean {
return AppEnvUtil.current === "test";
}
static canOutputLogs(): boolean {
return !AppEnvUtil.isProduction();
}
}