import { readFileSync, writeFileSync, existsSync } from 'fs'; import { join, dirname } from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); // Default configuration values const DEFAULT_CONFIG = { version: '1.0', project: { name: '', type: '', template: '' }, build: { platform: 'linux/amd64', nodeVersion: '20', pythonVersion: '3.11', dotnetVersion: '9.0', entryPoint: '', buildCommand: null }, runtime: { port: 3000, envFile: true, volumes: [], extraHosts: false }, deployment: { sshHost: '', sshUser: '', sshKeyPath: '', targetPath: '', autoLoad: true, autoStart: true } }; // Global defaults (can be overridden per project) const GLOBAL_DEFAULTS = { deployment: { sshHost: '192.168.8.178', sshUser: 'deployer', targetRoot: '~/containers' }, build: { platform: 'linux/amd64', nodeVersion: '20', pythonVersion: '3.11', dotnetVersion: '9.0' } }; /** * Load project configuration from docker-deployment.json */ export function loadProjectConfig(projectPath) { const configPath = join(projectPath, 'docker-deployment.json'); if (!existsSync(configPath)) { return null; } try { const content = readFileSync(configPath, 'utf-8'); return JSON.parse(content); } catch (error) { throw new Error(`Failed to parse config at ${configPath}: ${error.message}`); } } /** * Save project configuration to docker-deployment.json */ export function saveProjectConfig(projectPath, config) { const configPath = join(projectPath, 'docker-deployment.json'); const content = JSON.stringify(config, null, 2); writeFileSync(configPath, content, 'utf-8'); return configPath; } /** * Load global configuration from the deployment tool directory */ export function loadGlobalConfig() { const globalConfigPath = join(__dirname, '..', '..', 'global-deployment-config.json'); if (!existsSync(globalConfigPath)) { return GLOBAL_DEFAULTS; } try { const content = readFileSync(globalConfigPath, 'utf-8'); return { ...GLOBAL_DEFAULTS, ...JSON.parse(content) }; } catch (error) { return GLOBAL_DEFAULTS; } } /** * Save global configuration */ export function saveGlobalConfig(config) { const globalConfigPath = join(__dirname, '..', '..', 'global-deployment-config.json'); const content = JSON.stringify(config, null, 2); writeFileSync(globalConfigPath, content, 'utf-8'); return globalConfigPath; } /** * Create a new project configuration with defaults */ export function createProjectConfig(projectName, detection, overrides = {}) { const globalConfig = loadGlobalConfig(); const config = { ...DEFAULT_CONFIG, project: { name: projectName, type: detection.type, template: detection.template }, build: { ...DEFAULT_CONFIG.build, ...globalConfig.build, entryPoint: detection.entryPoint || '', buildCommand: detection.buildCommand || null }, runtime: { ...DEFAULT_CONFIG.runtime, port: detection.port || 3000 }, deployment: { ...DEFAULT_CONFIG.deployment, sshHost: globalConfig.deployment?.sshHost || '', sshUser: globalConfig.deployment?.sshUser || '', targetPath: `${globalConfig.deployment?.targetRoot || '~/containers'}/${projectName}/files` } }; // Apply overrides if (overrides.port) config.runtime.port = overrides.port; if (overrides.name) config.project.name = overrides.name; if (overrides.sshHost) config.deployment.sshHost = overrides.sshHost; if (overrides.sshUser) config.deployment.sshUser = overrides.sshUser; if (overrides.volumes) config.runtime.volumes = overrides.volumes; return config; } /** * Get the target path for deployment */ export function getTargetPath(config) { return config.deployment?.targetPath || `~/containers/${config.project.name}/files`; } /** * Merge configs with priority: overrides > project > global > defaults */ export function mergeConfigs(projectConfig, overrides = {}) { const globalConfig = loadGlobalConfig(); return { ...DEFAULT_CONFIG, ...globalConfig, ...projectConfig, ...overrides }; } export { DEFAULT_CONFIG, GLOBAL_DEFAULTS };