import { initializeLicense } from './license';
/**
* Global configuration and initialization
*/
export interface Config {
env: 'development' | 'production';
debug: boolean;
port: number;
wsPort: number;
license: any; // LicenseConfig from license.ts
}
let config: Config | null = null;
/**
* Initialize configuration
*/
export async function initializeConfig(forceNewLicense: boolean = false): Promise<Config> {
if (config) {
return config;
}
const env = (process.env.NODE_ENV || 'development') as 'development' | 'production';
const debug = env === 'development' || process.env.DEBUG === 'true';
// Load or create license
const license = await initializeLicense(forceNewLicense);
// If not in development and license is invalid, fail startup
if (env === 'production' && !license.isValid) {
throw new Error(
'Invalid or missing license. Please run: npm run setup-license'
);
}
// Warn in development if license invalid
if (debug && !license.isValid) {
console.warn('⚠️ No valid license found. Created demo license for development.');
}
config = {
env,
debug,
port: parseInt(process.env.PORT || '3000', 10),
wsPort: parseInt(process.env.WS_PORT || '4000', 10),
license,
};
return config;
}
/**
* Get current configuration (must call initializeConfig first)
*/
export function getConfig(): Config {
if (!config) {
throw new Error('Configuration not initialized. Call initializeConfig first.');
}
return config;
}