UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

122 lines 4.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseHandler = void 0; const cli_table3_1 = __importDefault(require("cli-table3")); const errors_1 = require("../utils/errors"); class BaseHandler { async executeWithErrorHandling(operation, operationName) { try { return await operation(); } catch (error) { if (process.env['DEBUG']) { console.error(`[${operationName}] Error details:`, error); } if (error instanceof errors_1.PolyVError) { throw error; } (0, errors_1.logError)(error instanceof Error ? error : new Error(String(error))); throw error; } } displaySuccess(message, data, format = 'table') { console.log(`✅ ${message}`); if (data) { this.displayData(data, format); } } displayData(data, format = 'table') { if (format === 'json') { console.log(JSON.stringify(data, null, 2)); } else { this.displayAsTable(data); } } displayAsTable(data) { if (!data || typeof data !== 'object') { console.log(data); return; } if (Array.isArray(data)) { this.displayArrayAsTable(data); } else { this.displayObjectAsTable(data); } } displayArrayAsTable(data) { if (data.length === 0) { console.log('No data to display'); return; } const allKeys = new Set(); data.forEach(item => { if (typeof item === 'object' && item !== null) { Object.keys(item).forEach(key => allKeys.add(key)); } }); const headers = Array.from(allKeys); const table = new cli_table3_1.default({ head: headers, style: { head: ['cyan'] } }); data.forEach(item => { const row = headers.map(header => { const value = item?.[header]; return this.formatTableValue(value); }); table.push(row); }); console.log(table.toString()); } displayObjectAsTable(data) { const table = new cli_table3_1.default({ style: { head: ['cyan'] } }); Object.entries(data).forEach(([key, value]) => { const formattedKey = key.charAt(0).toUpperCase() + key.slice(1); const formattedValue = this.formatTableValue(value); table.push({ [formattedKey]: formattedValue }); }); console.log(table.toString()); } formatTableValue(value) { if (value === null || value === undefined) { return '-'; } if (typeof value === 'boolean') { return value ? 'Yes' : 'No'; } if (value instanceof Date) { return value.toLocaleString(); } if (typeof value === 'object') { return JSON.stringify(value); } return String(value); } displayError(message, details) { console.error(`❌ ${message}`); if (details && process.env['DEBUG']) { console.error('Error details:', details); } } displayWarning(message) { console.warn(`⚠️ ${message}`); } displayInfo(message) { console.log(`ℹ️ ${message}`); } validateRequiredParams(params, requiredFields) { const missing = requiredFields.filter(field => params[field] === undefined || params[field] === null || params[field] === ''); if (missing.length > 0) { throw new errors_1.PolyVError(`Missing required parameters: ${missing.join(', ')}`, 'MISSING_REQUIRED_PARAMS', 1, { missing, provided: Object.keys(params) }); } } } exports.BaseHandler = BaseHandler; //# sourceMappingURL=base.handler.js.map