@@ -26,49 +26,56 @@ This file is part of Network Pro.
2626
2727/**
2828 * @typedef {object } EnvironmentInfo
29- * @property {string } mode - The detected environment mode (`dev`, `prod`, `audit`, etc.).
30- * @property {boolean } isDev - True when running in a development or local environment.
31- * @property {boolean } isProd - True when running in production.
32- * @property {boolean } isAudit - True when running in audit / staging environments.
33- * @property {boolean } isCI - True when running in continuous integration (CI) pipelines.
34- * @property {boolean } isTest - True when running under test or mock environments.
29+ * @property {string } mode - The build-time environment mode.
30+ * @property {string } effective - The environment actually being used (after host fallback).
31+ * @property {boolean } isDev
32+ * @property {boolean } isProd
33+ * @property {boolean } isAudit
34+ * @property {boolean } isCI
35+ * @property {boolean } isTest
3536 */
3637
3738/**
38- * Normalizes environment detection across client, SSR, and build contexts.
39- * Uses `import.meta.env` for Vite build-time vars and `process.env` for runtime vars.
39+ * Build-time mode injected by Vite.
40+ * Always baked into the client bundle.
41+ * Falls back to 'production' if nothing else is defined.
42+ */
43+ export const BUILD_ENV_MODE =
44+ import . meta. env . PUBLIC_ENV_MODE || import . meta. env . MODE || 'production' ;
45+
46+ /**
47+ * Detects the current environment, combining build-time
48+ * and runtime (hostname-based) checks.
49+ * Works safely in both Node and browser contexts.
4050 *
41- * @returns {EnvironmentInfo } Normalized environment context flags.
51+ * @returns {EnvironmentInfo }
4252 */
4353export function detectEnvironment ( ) {
44- /** @type {string | undefined } */
45- const viteMode = import . meta. env ?. MODE ;
46- /** @type {string | undefined } */
47- const publicEnvMode = import . meta. env ?. PUBLIC_ENV_MODE ;
54+ const mode = BUILD_ENV_MODE ;
55+
56+ // Client-side fallback for audit/netwk environments
57+ const host = typeof window !== 'undefined' ? window . location . hostname : '' ;
58+
59+ const hostIsAudit = / ( ^ | \. ) a u d i t \. n e t w k \. p r o $ / i. test ( host ) ;
4860
49- /** @type { string | undefined } */
50- const nodeEnv =
51- typeof process !== 'undefined' && process ?. env ?. NODE_ENV
52- ? process . env . NODE_ENV
53- : undefined ;
61+ const isDev = [ 'development' , 'dev' ] . includes ( mode ) ;
62+ const isProd = [ 'production' , 'prod' ] . includes ( mode ) ;
63+ const isAudit = mode === 'audit' || hostIsAudit ;
64+ const isCI = mode === 'ci' ;
65+ const isTest = mode === 'test' ;
5466
55- /** @type {string | undefined } */
56- const envMode =
57- typeof process !== 'undefined' && process ?. env ?. ENV_MODE
58- ? process . env . ENV_MODE
59- : undefined ;
67+ // Prefer host-based detection if it disagrees with build-time
68+ const effective = hostIsAudit && ! isAudit ? 'audit(host)' : mode ;
6069
61- // Fallback order — guarantees a mode string even if nothing is set
62- /** @type {string } */
63- const mode = envMode || publicEnvMode || viteMode || nodeEnv || 'unknown' ;
70+ if ( typeof window === 'undefined' ) {
71+ // Only log on server / build to avoid client noise
72+ console . log ( '[detectEnvironment] Build mode:' , mode ) ;
73+ if ( hostIsAudit && mode !== 'audit' ) {
74+ console . log (
75+ '[detectEnvironment] Host suggests audit, overriding build-time mode.' ,
76+ ) ;
77+ }
78+ }
6479
65- // Return a normalized, typed object
66- return {
67- mode,
68- isDev : [ 'development' , 'dev' ] . includes ( mode ) ,
69- isProd : [ 'production' , 'prod' ] . includes ( mode ) ,
70- isAudit : mode === 'audit' ,
71- isCI : mode === 'ci' ,
72- isTest : mode === 'test' ,
73- } ;
80+ return { mode, effective, isDev, isProd, isAudit, isCI, isTest } ;
7481}
0 commit comments