UNPKG

@cere/rob-cli

Version:

CLI tool for deploying and managing rafts and data sources

95 lines 3.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.processEnvVars = processEnvVars; exports.loadYamlFile = loadYamlFile; exports.saveYamlFile = saveYamlFile; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const js_yaml_1 = __importDefault(require("js-yaml")); const logger_1 = require("./logger"); const dotenv_1 = __importDefault(require("dotenv")); // Load .env file dotenv_1.default.config(); const logger = new logger_1.Logger('YamlLoader'); /** * Process environment variables in strings * Replaces ${ENV_VAR} with the value of process.env.ENV_VAR */ function processEnvVars(obj) { if (typeof obj === 'string') { return obj.replace(/\${([^}]+)}/g, (_, envVar) => { const value = process.env[envVar]; if (value === undefined) { logger.warn(`Environment variable ${envVar} not found`); return ''; } logger.debug(`Replaced environment variable ${envVar} with its value`); return value; }); } else if (Array.isArray(obj)) { return obj.map(item => processEnvVars(item)); } else if (obj !== null && typeof obj === 'object') { const result = {}; for (const key in obj) { result[key] = processEnvVars(obj[key]); } return result; } return obj; } /** * Load a YAML file and parse its contents */ function loadYamlFile(filePath) { try { const absolutePath = path_1.default.resolve(process.cwd(), filePath); logger.debug(`Loading YAML file: ${absolutePath}`); if (!fs_1.default.existsSync(absolutePath)) { throw new Error(`File not found: ${absolutePath}`); } const fileContents = fs_1.default.readFileSync(absolutePath, 'utf8'); const parsedData = js_yaml_1.default.load(fileContents); // Process environment variables return processEnvVars(parsedData); } catch (error) { if (error instanceof Error) { logger.error(`Error loading YAML file: ${error.message}`); } else { logger.error(`Unknown error loading YAML file`); } throw error; } } /** * Save data to a YAML file */ function saveYamlFile(filePath, data) { try { const absolutePath = path_1.default.resolve(process.cwd(), filePath); logger.debug(`Saving YAML file: ${absolutePath}`); const yamlStr = js_yaml_1.default.dump(data, { indent: 2, lineWidth: 120, noRefs: true, }); fs_1.default.writeFileSync(absolutePath, yamlStr, 'utf8'); logger.debug(`File saved successfully: ${absolutePath}`); } catch (error) { if (error instanceof Error) { logger.error(`Error saving YAML file: ${error.message}`); } else { logger.error(`Unknown error saving YAML file`); } throw error; } } //# sourceMappingURL=yaml-loader.js.map