import * as fs from 'fs'; import * as plugins from './tsdocker.plugins.js'; import { logger } from './tsdocker.logging.js'; import type { IGlobalConfig, IRemoteBuilder } from './interfaces/index.js'; const CONFIG_DIR = plugins.path.join( process.env.HOME || process.env.USERPROFILE || '~', '.git.zone', 'tsdocker', ); const CONFIG_PATH = plugins.path.join(CONFIG_DIR, 'config.json'); const DEFAULT_CONFIG: IGlobalConfig = { remoteBuilders: [], }; export class GlobalConfig { static getConfigPath(): string { return CONFIG_PATH; } static load(): IGlobalConfig { try { const raw = fs.readFileSync(CONFIG_PATH, 'utf-8'); const parsed = JSON.parse(raw); return { ...DEFAULT_CONFIG, ...parsed, }; } catch { return { ...DEFAULT_CONFIG }; } } static save(config: IGlobalConfig): void { fs.mkdirSync(CONFIG_DIR, { recursive: true }); fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2) + '\n', 'utf-8'); } static addBuilder(builder: IRemoteBuilder): void { const config = GlobalConfig.load(); const existing = config.remoteBuilders.findIndex((b) => b.name === builder.name); if (existing >= 0) { config.remoteBuilders[existing] = builder; logger.log('info', `Updated remote builder: ${builder.name}`); } else { config.remoteBuilders.push(builder); logger.log('info', `Added remote builder: ${builder.name}`); } GlobalConfig.save(config); } static removeBuilder(name: string): void { const config = GlobalConfig.load(); const before = config.remoteBuilders.length; config.remoteBuilders = config.remoteBuilders.filter((b) => b.name !== name); if (config.remoteBuilders.length < before) { logger.log('info', `Removed remote builder: ${name}`); } else { logger.log('warn', `Remote builder not found: ${name}`); } GlobalConfig.save(config); } static getBuilders(): IRemoteBuilder[] { return GlobalConfig.load().remoteBuilders; } /** * Returns remote builders that match any of the requested platforms */ static getBuildersForPlatforms(platforms: string[]): IRemoteBuilder[] { const builders = GlobalConfig.getBuilders(); return builders.filter((b) => platforms.includes(b.platform)); } }