refactor: Enhance environment configuration and loading mechanism
- Implement flexible environment variable loading strategy - Add support for environment-specific and local override configuration files - Create new `loadEnv.ts` module for dynamic environment configuration - Update configuration loading in multiple config files - Remove deprecated `.env.development.template` - Add setup script for environment validation - Improve WebSocket error handling and client configuration
This commit is contained in:
@@ -1,23 +1,5 @@
|
||||
import { config } from "dotenv";
|
||||
import { resolve } from "path";
|
||||
import { z } from "zod";
|
||||
|
||||
/**
|
||||
* Load environment variables based on NODE_ENV
|
||||
* Development: .env.development
|
||||
* Test: .env.test
|
||||
* Production: .env
|
||||
*/
|
||||
const envFile =
|
||||
process.env.NODE_ENV === "production"
|
||||
? ".env"
|
||||
: process.env.NODE_ENV === "test"
|
||||
? ".env.test"
|
||||
: ".env.development";
|
||||
|
||||
console.log(`Loading environment from ${envFile}`);
|
||||
config({ path: resolve(process.cwd(), envFile) });
|
||||
|
||||
/**
|
||||
* Application configuration object
|
||||
* Contains all configuration settings for the application
|
||||
|
||||
@@ -11,6 +11,7 @@ const envFile =
|
||||
|
||||
config({ path: resolve(process.cwd(), envFile) });
|
||||
|
||||
// Base configuration for Home Assistant
|
||||
export const HASS_CONFIG = {
|
||||
// Base configuration
|
||||
BASE_URL: process.env.HASS_HOST || "http://localhost:8123",
|
||||
|
||||
@@ -1,16 +1,7 @@
|
||||
import { config } from "dotenv";
|
||||
import { resolve } from "path";
|
||||
import { loadEnvironmentVariables } from "./loadEnv";
|
||||
|
||||
// Load environment variables based on NODE_ENV
|
||||
const envFile =
|
||||
process.env.NODE_ENV === "production"
|
||||
? ".env"
|
||||
: process.env.NODE_ENV === "test"
|
||||
? ".env.test"
|
||||
: ".env.development";
|
||||
|
||||
console.log(`Loading environment from ${envFile}`);
|
||||
config({ path: resolve(process.cwd(), envFile) });
|
||||
// Load environment variables from the appropriate files
|
||||
loadEnvironmentVariables();
|
||||
|
||||
// Home Assistant Configuration
|
||||
export const HASS_CONFIG = {
|
||||
|
||||
49
src/config/loadEnv.ts
Normal file
49
src/config/loadEnv.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { config as dotenvConfig } from "dotenv";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
/**
|
||||
* Maps NODE_ENV values to their corresponding environment file names
|
||||
*/
|
||||
const ENV_FILE_MAPPING: Record<string, string> = {
|
||||
production: ".env.prod",
|
||||
development: ".env.dev",
|
||||
test: ".env.test",
|
||||
};
|
||||
|
||||
/**
|
||||
* Loads environment variables from the appropriate files based on NODE_ENV.
|
||||
* First loads environment-specific file, then overrides with generic .env if it exists.
|
||||
*/
|
||||
export function loadEnvironmentVariables() {
|
||||
// Determine the current environment (default to 'development')
|
||||
const nodeEnv = (process.env.NODE_ENV || "development").toLowerCase();
|
||||
|
||||
// Get the environment-specific file name
|
||||
const envSpecificFile = ENV_FILE_MAPPING[nodeEnv];
|
||||
if (!envSpecificFile) {
|
||||
console.warn(`Unknown NODE_ENV value: ${nodeEnv}. Using .env.dev as fallback.`);
|
||||
}
|
||||
|
||||
const envFile = envSpecificFile || ".env.dev";
|
||||
const envPath = path.resolve(process.cwd(), envFile);
|
||||
|
||||
// Load the environment-specific file if it exists
|
||||
if (fs.existsSync(envPath)) {
|
||||
dotenvConfig({ path: envPath });
|
||||
console.log(`Loaded environment variables from ${envFile}`);
|
||||
} else {
|
||||
console.warn(`Environment-specific file ${envFile} not found.`);
|
||||
}
|
||||
|
||||
// Finally, check if there is a generic .env file present
|
||||
// If so, load it with the override option, so its values take precedence
|
||||
const genericEnvPath = path.resolve(process.cwd(), ".env");
|
||||
if (fs.existsSync(genericEnvPath)) {
|
||||
dotenvConfig({ path: genericEnvPath, override: true });
|
||||
console.log("Loaded and overrode with generic .env file");
|
||||
}
|
||||
}
|
||||
|
||||
// Export the environment file mapping for reference
|
||||
export const ENV_FILES = ENV_FILE_MAPPING;
|
||||
Reference in New Issue
Block a user