UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

352 lines 14 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigVersionManager = exports.ConfigExporter = exports.ConfigImporter = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const config_validator_1 = require("./config-validator"); const errors_1 = require("../utils/errors"); class ConfigImporter { static async importFromFile(filePath, options = {}) { try { if (!fs.existsSync(filePath)) { throw new errors_1.ConfigurationError(`Import file '${filePath}' not found`); } const fileContent = await fs.promises.readFile(filePath, 'utf8'); const format = options.format || this.detectFormat(filePath, fileContent); const parsedData = this.parseContent(fileContent, format); return await this.importFromData(parsedData, options); } catch (error) { return { success: false, warnings: [], errors: [error instanceof Error ? error.message : String(error)], }; } } static async importFromData(data, _options = {}) { const result = { success: false, warnings: [], errors: [], }; try { if (!data || typeof data !== 'object') { throw new errors_1.ConfigurationError('Invalid configuration data: must be an object'); } if (data.manifest) { result.manifest = data.manifest; this.validateManifest(result.manifest); } if (data.config) { const configResult = this.importConfig(data.config); if (configResult.success) { result.config = configResult.config; } else { result.errors.push(...configResult.errors); result.warnings.push(...configResult.warnings); } } if (data.themes && Array.isArray(data.themes)) { const themesResult = this.importThemes(data.themes); result.themes = themesResult.themes; result.warnings.push(...themesResult.warnings); result.errors.push(...themesResult.errors); } if (data.layouts && Array.isArray(data.layouts)) { const layoutsResult = this.importLayouts(data.layouts); result.layouts = layoutsResult.layouts; result.warnings.push(...layoutsResult.warnings); result.errors.push(...layoutsResult.errors); } result.success = result.errors.length === 0 && !!(result.config !== undefined || (result.themes && result.themes.length > 0) || (result.layouts && result.layouts.length > 0)); return result; } catch (error) { result.errors.push(error instanceof Error ? error.message : String(error)); return result; } } static detectFormat(filePath, content) { const ext = path.extname(filePath).toLowerCase(); if (ext === '.yaml' || ext === '.yml') { return 'yaml'; } if (ext === '.json') { return 'json'; } try { JSON.parse(content); return 'json'; } catch { return 'yaml'; } } static parseContent(content, format) { try { if (format === 'json') { return JSON.parse(content); } else { throw new errors_1.ConfigurationError('YAML format not yet implemented. Please use JSON format.'); } } catch (error) { throw new errors_1.ConfigurationError(`Failed to parse ${format.toUpperCase()} content: ${error instanceof Error ? error.message : String(error)}`); } } static validateManifest(manifest) { if (!manifest.version) { throw new errors_1.ConfigurationError('Invalid manifest: version is required'); } if (!manifest.format) { throw new errors_1.ConfigurationError('Invalid manifest: format is required'); } if (!manifest.exportedAt) { throw new errors_1.ConfigurationError('Invalid manifest: exportedAt is required'); } } static importConfig(configData) { const result = { success: false, errors: [], warnings: [], }; try { const validation = config_validator_1.ConfigValidator.validateMonitoringConfig(configData); if (!validation.valid) { result.errors.push(...validation.errors); result.errors.push(...validation.critical); result.warnings.push(...validation.warnings); return result; } result.success = true; return { ...result, config: configData, }; } catch (error) { result.errors.push(error instanceof Error ? error.message : String(error)); return result; } } static importThemes(themesData) { const result = { themes: [], errors: [], warnings: [], }; themesData.forEach((themeData, index) => { try { const validation = config_validator_1.ConfigValidator.validateThemeConfig(themeData, `themes[${index}]`); if (validation.valid) { result.themes.push(themeData); } else { result.errors.push(...validation.errors); result.errors.push(...validation.critical); result.warnings.push(...validation.warnings); } } catch (error) { result.errors.push(`Theme ${index}: ${error instanceof Error ? error.message : String(error)}`); } }); return result; } static importLayouts(layoutsData) { const result = { layouts: [], errors: [], warnings: [], }; layoutsData.forEach((layoutData, index) => { try { const validation = config_validator_1.ConfigValidator.validateLayoutConfig(layoutData, `layouts[${index}]`); if (validation.valid) { result.layouts.push(layoutData); } else { result.errors.push(...validation.errors); result.errors.push(...validation.critical); result.warnings.push(...validation.warnings); } } catch (error) { result.errors.push(`Layout ${index}: ${error instanceof Error ? error.message : String(error)}`); } }); return result; } } exports.ConfigImporter = ConfigImporter; class ConfigExporter { static async exportToFile(config, filePath, options = { format: 'json' }) { try { const exportData = this.createExportData(config, options); const serializedData = this.serializeData(exportData, options); const dir = path.dirname(filePath); if (!fs.existsSync(dir)) { await fs.promises.mkdir(dir, { recursive: true }); } await fs.promises.writeFile(filePath, serializedData, 'utf8'); } catch (error) { throw new errors_1.ConfigurationError(`Failed to export configuration to '${filePath}': ${error instanceof Error ? error.message : String(error)}`); } } static async exportComplete(config, themes, layouts, filePath, options = { format: 'json' }) { try { const exportData = { manifest: this.createManifest(config, themes, layouts, options), config, themes: options.includeCustomThemes ? themes.filter(t => !t.isBuiltIn) : [], layouts: options.includeCustomLayouts ? layouts.filter(l => !l.isBuiltIn) : [], }; if (options.includeMetadata) { exportData.manifest.metadata = { exportedBy: 'polyv-cli', exportedFrom: process.platform, nodeVersion: process.version, timestamp: new Date().toISOString(), }; } const serializedData = this.serializeData(exportData, options); const dir = path.dirname(filePath); if (!fs.existsSync(dir)) { await fs.promises.mkdir(dir, { recursive: true }); } await fs.promises.writeFile(filePath, serializedData, 'utf8'); } catch (error) { throw new errors_1.ConfigurationError(`Failed to export complete configuration to '${filePath}': ${error instanceof Error ? error.message : String(error)}`); } } static exportToString(config, options = { format: 'json' }) { const exportData = this.createExportData(config, options); return this.serializeData(exportData, options); } static createExportData(config, options) { const exportData = { manifest: { version: config.version, format: options.format, exportedAt: new Date().toISOString(), source: 'polyv-cli-config-manager', components: { config: true, themes: options.includeCustomThemes ? config.customThemes.length : 0, layouts: options.includeCustomLayouts ? config.customLayouts.length : 0, }, }, config, }; if (options.includeCustomThemes && config.customThemes.length > 0) { exportData.themes = config.customThemes; } if (options.includeCustomLayouts && config.customLayouts.length > 0) { exportData.layouts = config.customLayouts; } if (options.includeMetadata) { exportData.manifest.metadata = { exportedBy: 'polyv-cli', exportedFrom: process.platform, nodeVersion: process.version, }; } return exportData; } static createManifest(config, themes, layouts, options) { return { version: config.version, format: options.format, exportedAt: new Date().toISOString(), source: 'polyv-cli-config-manager', components: { config: true, themes: themes.length, layouts: layouts.length, }, }; } static serializeData(data, options) { if (options.format === 'json') { const indent = options.minify ? 0 : 2; return JSON.stringify(data, null, indent); } else { throw new errors_1.ConfigurationError('YAML format not yet implemented. Please use JSON format.'); } } } exports.ConfigExporter = ConfigExporter; class ConfigVersionManager { static isVersionSupported(version) { return this.SUPPORTED_VERSIONS.includes(version); } static getCurrentVersion() { return this.CURRENT_VERSION; } static migrateConfig(config, fromVersion) { if (!this.isVersionSupported(fromVersion)) { throw new errors_1.ConfigurationError(`Unsupported configuration version: ${fromVersion}. Supported versions: ${this.SUPPORTED_VERSIONS.join(', ')}`); } if (fromVersion === this.CURRENT_VERSION) { return config; } const migratedConfig = { ...config }; migratedConfig.version = this.CURRENT_VERSION; return migratedConfig; } static getMigrationPath(fromVersion, toVersion) { if (fromVersion === toVersion) { return []; } if (!this.isVersionSupported(fromVersion) || !this.isVersionSupported(toVersion)) { throw new errors_1.ConfigurationError(`Migration path not available from ${fromVersion} to ${toVersion}`); } return [toVersion]; } } exports.ConfigVersionManager = ConfigVersionManager; ConfigVersionManager.SUPPORTED_VERSIONS = ['1.0.0']; ConfigVersionManager.CURRENT_VERSION = '1.0.0'; //# sourceMappingURL=config-io.js.map