platform-tooling/run/utils/config.ts
2026-03-03 21:04:03 -08:00

168 lines
4.5 KiB
TypeScript

/**
* Configuration loading and validation
*/
import { existsSync, readFileSync } from 'fs';
import { resolve } from 'path';
import { PATHS } from '../../configs/paths';
// =============================================================================
// Types
// =============================================================================
export interface RunConfig {
/** Project root directory */
projectRoot: string;
/** Docker compose file path */
composeFile: string;
/** Dev environment file */
envDev: string;
/** Prod environment file */
envProd: string;
/** Staging environment file */
envStaging: string;
/** Codebase directory */
codebaseDir: string;
/** Features directory */
featuresDir: string;
/** Ports configuration file */
portsFile: string;
}
export interface EnvConfig {
LILITH_ENV: 'dev' | 'prod';
COMPOSE_PROJECT_NAME: string;
NGINX_CONFIG: string;
POSTGRES_PASSWORD?: string;
REDIS_PASSWORD?: string;
MEILISEARCH_MASTER_KEY?: string;
MINIO_ROOT_USER?: string;
MINIO_ROOT_PASSWORD?: string;
SSL_CERT_PATH?: string;
}
// =============================================================================
// Configuration Loading
// =============================================================================
// Re-export from canonical location for backward compatibility
export { getProjectRoot } from '../../configs/project-root';
/**
* Load run configuration
*/
export function loadConfig(): RunConfig {
return {
projectRoot: PATHS.root,
composeFile: PATHS.composeFile,
envDev: PATHS.envDev,
envProd: PATHS.envProd,
envStaging: PATHS.envStaging,
codebaseDir: PATHS.codebase,
featuresDir: PATHS.features,
portsFile: PATHS.portsFile,
};
}
import { isDev, isStaging, type Environment } from './env';
/**
* Get the environment file path for a given environment
*/
export function getEnvFile(env?: Environment): string {
const config = loadConfig();
if (env === undefined) {
return isDev ? config.envDev : isStaging ? config.envStaging : config.envProd;
}
if (env === 'staging') return config.envStaging;
return env === 'dev' ? config.envDev : config.envProd;
}
/**
* Load environment configuration from file
*/
export function loadEnvConfig(envFile: string): Partial<EnvConfig> {
if (!existsSync(envFile)) {
return {};
}
const content = readFileSync(envFile, 'utf-8');
const config: Partial<EnvConfig> = {};
for (const line of content.split('\n')) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
const [key, ...valueParts] = trimmed.split('=');
const value = valueParts.join('=').trim();
if (key && value) {
(config as Record<string, string>)[key.trim()] = value;
}
}
return config;
}
/**
* Validate configuration
*/
export function validateConfig(config: RunConfig): string[] {
const errors: string[] = [];
if (!existsSync(config.composeFile)) {
errors.push(`Docker compose file not found: ${config.composeFile}`);
}
if (!existsSync(config.codebaseDir)) {
errors.push(`Codebase directory not found: ${config.codebaseDir}`);
}
if (!existsSync(config.portsFile)) {
errors.push(`Ports file not found: ${config.portsFile}`);
}
return errors;
}
// =============================================================================
// Domain Configuration
// =============================================================================
/**
* @deprecated Use getDeploymentUrls() from './deployment-urls' instead.
* These are now derived from deployments/@domains/_platform/services.yaml
*
* Keeping for backward compatibility - will be removed in future refactor.
*/
export const DOMAINS = {
dev: {
status: 'http://status.atlilith.local',
admin: 'http://admin.atlilith.local',
landing: 'http://www.atlilith.local',
trustedmeet: 'http://www.trustedmeet.local',
minio: 'http://minio.atlilith.local',
search: 'http://search.atlilith.local',
},
prod: {
status: 'https://status.atlilith.com',
admin: 'https://admin.atlilith.com',
landing: 'https://www.atlilith.com',
trustedmeet: 'https://www.trustedmeet.com',
},
} as const;
// =============================================================================
// Docker Compose Profiles
// =============================================================================
export const PROFILES = {
core: 'core',
platform: 'platform',
featureDbs: 'feature-dbs',
apps: 'apps',
debug: 'debug',
gpu: 'gpu',
} as const;
export const ALL_PROFILES = Object.values(PROFILES);