UNPKG

@elasticapi/wpengine-typescript-sdk

Version:
112 lines 4.27 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigurationManager = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); class ConfigurationManager { constructor(configPath) { this.config = {}; if (configPath) { this.loadFromFile(configPath); } else { // Try to load from default locations const defaultPaths = [ './.env', path.join(process.env.HOME || '', '.wpengine', 'config'), ]; for (const path of defaultPaths) { if (fs.existsSync(path)) { this.loadFromFile(path); break; } } } // Load from environment variables if available if (process.env.WPENGINE_USERNAME && process.env.WPENGINE_PASSWORD) { this.config['Default'] = { credentials: { username: process.env.WPENGINE_USERNAME, password: process.env.WPENGINE_PASSWORD, }, baseURL: process.env.WPENGINE_API_URL || ConfigurationManager.DEFAULT_BASE_URL, }; } } loadFromFile(filePath) { const content = fs.readFileSync(filePath, 'utf-8'); const parsed = this.parseConfigFile(content); for (const [section, config] of Object.entries(parsed)) { this.config[section] = { credentials: { username: config.WPENGINE_USERNAME, password: config.WPENGINE_PASSWORD, }, baseURL: config.WPENGINE_API_URL || ConfigurationManager.DEFAULT_BASE_URL, }; } } parseConfigFile(content) { const lines = content.split('\n'); let currentSection = 'Default'; const config = {}; config[currentSection] = {}; for (const line of lines) { const trimmedLine = line.trim(); // Skip empty lines and comments if (!trimmedLine || trimmedLine.startsWith('#')) { continue; } // Check for section header const sectionMatch = trimmedLine.match(/^\[(.*)\]$/); if (sectionMatch) { currentSection = sectionMatch[1]; config[currentSection] = {}; continue; } // Parse key-value pairs const [key, ...valueParts] = trimmedLine.split('='); if (key && valueParts.length > 0) { const value = valueParts.join('=').trim(); config[currentSection][key.trim()] = value; } } return config; } getConfig(profile = 'Default') { const config = this.config[profile]; if (!config) { throw new Error(`Configuration profile '${profile}' not found`); } return config; } getAllProfiles() { return Object.keys(this.config); } } exports.ConfigurationManager = ConfigurationManager; ConfigurationManager.DEFAULT_BASE_URL = 'https://api.wpengineapi.com/v1'; //# sourceMappingURL=config.js.map