UNPKG

forge-deploy-cli

Version:

Professional CLI for local deployments with automatic subdomain routing, SSL certificates, and infrastructure management

117 lines 4.09 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigService = void 0; const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const os_1 = __importDefault(require("os")); const CONFIG_DIR = path_1.default.join(os_1.default.homedir(), '.forge'); const CONFIG_FILE = path_1.default.join(CONFIG_DIR, 'config.json'); const PROJECT_CONFIG_FILE = 'forge.config.json'; class ConfigService { async ensureConfigDir() { await fs_extra_1.default.ensureDir(CONFIG_DIR); } async saveGlobalConfig(config) { await this.ensureConfigDir(); const existingConfig = await this.loadGlobalConfig(); const updatedConfig = { ...existingConfig, ...config }; await fs_extra_1.default.writeJSON(CONFIG_FILE, updatedConfig, { spaces: 2 }); } async loadGlobalConfig() { try { await this.ensureConfigDir(); const config = await fs_extra_1.default.readJSON(CONFIG_FILE); return config; } catch (error) { return { apiUrl: 'https://api.forgecli.tech' }; } } async saveProjectConfig(config) { const existingConfig = await this.loadProjectConfig(); const updatedConfig = { ...existingConfig, ...config }; await fs_extra_1.default.writeJSON(PROJECT_CONFIG_FILE, updatedConfig, { spaces: 2 }); } async loadProjectConfig() { try { const config = await fs_extra_1.default.readJSON(PROJECT_CONFIG_FILE); return config; } catch (error) { return { apiUrl: 'https://api.forgecli.tech' }; } } async getConfig() { const globalConfig = await this.loadGlobalConfig(); const projectConfig = await this.loadProjectConfig(); return { ...globalConfig, ...projectConfig }; } async setConfigValue(key, value, global = false) { const config = global ? await this.loadGlobalConfig() : await this.loadProjectConfig(); // Handle nested keys like "env.NODE_ENV" const keys = key.split('.'); let current = config; for (let i = 0; i < keys.length - 1; i++) { if (!current[keys[i]]) { current[keys[i]] = {}; } current = current[keys[i]]; } current[keys[keys.length - 1]] = value; if (global) { await this.saveGlobalConfig(config); } else { await this.saveProjectConfig(config); } } async getConfigValue(key) { const config = await this.getConfig(); const keys = key.split('.'); let current = config; for (const k of keys) { if (current && typeof current === 'object' && k in current) { current = current[k]; } else { return undefined; } } return current; } async removeConfigValue(key, global = false) { const config = global ? await this.loadGlobalConfig() : await this.loadProjectConfig(); const keys = key.split('.'); let current = config; for (let i = 0; i < keys.length - 1; i++) { if (!current[keys[i]]) { return; // Key doesn't exist } current = current[keys[i]]; } delete current[keys[keys.length - 1]]; if (global) { await this.saveGlobalConfig(config); } else { await this.saveProjectConfig(config); } } async clearConfig(global = false) { if (global) { await fs_extra_1.default.remove(CONFIG_FILE); } else { await fs_extra_1.default.remove(PROJECT_CONFIG_FILE); } } } exports.ConfigService = ConfigService; //# sourceMappingURL=config.js.map