UNPKG

@interopio/desktop-cli

Version:

io.Connect Desktop Seed Repository CLI Tools

180 lines 7.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IocdConfigManager = void 0; const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const utils_1 = require("../../../utils"); // Configuration constants const CONFIG_FILENAMES = ['.iocdrc', '.iocdrc.json', 'iocd.config.json']; const DEFAULT_HTTP_BASE_URL = 'http://localhost:8080'; const DEFAULT_TIMEOUT = 300000; // 5 minutes const DEFAULT_MAX_RETRIES = 3; const DEFAULT_CACHE_DIR = '.iocd-cache'; class IocdConfigManager { /** * Load configuration from .iocdrc file(s) * Searches in current directory, then parent directories */ static async loadConfig(startDir) { if (this.cachedConfig) { return this.cachedConfig; } const configPath = await this.findConfigFile(startDir); if (configPath) { try { const config = await this.loadConfigFile(configPath); this.cachedConfig = this.mergeWithDefaults(config); utils_1.Logger.debug(`Loaded configuration from: ${configPath}`); return this.cachedConfig; } catch (error) { utils_1.Logger.warning(`Failed to load config from ${configPath}: ${error instanceof Error ? error.message : String(error)}`); } } // No config file found, return defaults utils_1.Logger.debug('No .iocdrc file found, using default configuration'); this.cachedConfig = this.getDefaultConfig(); return this.cachedConfig; } /** * Find configuration file by searching up the directory tree */ static async findConfigFile(startDir) { const searchDir = startDir || process.cwd(); let currentDir = path_1.default.resolve(searchDir); // Search up the directory tree until we reach the root while (currentDir) { for (const filename of CONFIG_FILENAMES) { const configPath = path_1.default.join(currentDir, filename); if (await fs_extra_1.default.pathExists(configPath)) { return configPath; } } const parentDir = path_1.default.dirname(currentDir); // Stop when we reach the root (parentDir === currentDir) if (parentDir === currentDir) { break; } currentDir = parentDir; } return null; } /** * Load and parse a specific config file */ static async loadConfigFile(configPath) { const content = await fs_extra_1.default.readFile(configPath, 'utf-8'); try { return JSON.parse(content); } catch (error) { throw new Error(`Invalid JSON in config file ${configPath}: ${error instanceof Error ? error.message : String(error)}`); } } /** * Merge loaded config with environment variables and defaults */ static mergeWithDefaults(config) { const defaultConfig = this.getDefaultConfig(); // Environment variables override config file const envOverrides = { http: { baseUrl: process.env.IOCD_HTTP_BASE_URL, urlPattern: process.env.IOCD_HTTP_URL_PATTERN, timeout: process.env.IOCD_HTTP_TIMEOUT ? parseInt(process.env.IOCD_HTTP_TIMEOUT) : undefined, maxRetries: process.env.IOCD_HTTP_MAX_RETRIES ? parseInt(process.env.IOCD_HTTP_MAX_RETRIES) : undefined, }, settings: { defaultStorage: process.env.IOCD_DEFAULT_STORAGE, debug: process.env.IOCD_DEBUG === 'true', useCache: process.env.IOCD_USE_CACHE === 'true', cacheDir: process.env.IOCD_CACHE_DIR, } }; // Deep merge: defaults < config file < environment variables return this.deepMerge(defaultConfig, config, envOverrides); } /** * Get default configuration */ static getDefaultConfig() { return { http: { baseUrl: DEFAULT_HTTP_BASE_URL, urlPattern: '', timeout: DEFAULT_TIMEOUT, maxRetries: DEFAULT_MAX_RETRIES, headers: {}, }, settings: { defaultStorage: 'http', debug: false, useCache: true, cacheDir: DEFAULT_CACHE_DIR, } }; } /** * Deep merge multiple configuration objects */ static deepMerge(...objects) { const result = {}; for (const obj of objects) { if (!obj) continue; for (const [key, value] of Object.entries(obj)) { if (value === null || value === undefined) continue; if (typeof value === 'object' && !Array.isArray(value)) { result[key] = this.deepMerge(result[key] || {}, value); } else { result[key] = value; } } } return result; } /** * Create a sample .iocdrc file */ static async createSampleConfig(outputPath = '.iocdrc') { const sampleConfig = { $schema: "https://github.com/InteropIO/iocd-cli/schema/iocdrc.schema.json", http: { baseUrl: "https://releases.example.com", urlPattern: "https://cdn.example.com/{component}/{version}/{fileName}-{version}-{platform}-{arch}.{ext}", timeout: DEFAULT_TIMEOUT, maxRetries: DEFAULT_MAX_RETRIES, headers: { "Authorization": "Bearer YOUR_TOKEN_HERE", "User-Agent": "iocd-cli" } }, // Components are now built-in - no manual configuration needed! // Available: iocd, bbg-v2, teams-adapter, excel-adapter settings: { defaultStorage: "http", debug: false, useCache: true, cacheDir: DEFAULT_CACHE_DIR } }; await fs_extra_1.default.writeFile(outputPath, JSON.stringify(sampleConfig, null, 2)); utils_1.Logger.info(`Sample configuration created: ${outputPath}`); utils_1.Logger.info('Components are now built-in - no manual component configuration required!'); utils_1.Logger.info('Available components: iocd, bbg-v2, teams-adapter, excel-adapter'); } /** * Clear cached configuration (for testing) */ static clearCache() { this.cachedConfig = null; } } exports.IocdConfigManager = IocdConfigManager; IocdConfigManager.cachedConfig = null; //# sourceMappingURL=iocd-config.js.map