UNPKG

kira-crud

Version:

Intelligent CRUD Generator for Laravel and Angular

244 lines (212 loc) 7.76 kB
/** * Config Converter for KIRA CRUD Generator * Provides utilities to convert between YAML and JSON configuration formats */ const fs = require('fs').promises; const path = require('path'); const yaml = require('js-yaml'); const chalk = require('chalk'); /** * Convert a configuration file from one format to another * @param {string} inputPath - Path to the input configuration file * @param {string} outputFormat - Output format (yaml, json) * @param {string} [outputPath] - Optional output path (defaults to changing extension) * @returns {Promise<string>} Path to the output file */ async function convertConfigFormat(inputPath, outputFormat, outputPath) { try { // Validate parameters if (!inputPath) { throw new Error('Input file path is required'); } if (!outputFormat || !['yaml', 'yml', 'json'].includes(outputFormat.toLowerCase())) { throw new Error('Output format must be "yaml", "yml", or "json"'); } // Normalize output format outputFormat = outputFormat.toLowerCase(); if (outputFormat === 'yml') { outputFormat = 'yaml'; } // Read the input file const inputContent = await fs.readFile(inputPath, 'utf8'); // Determine input format from file extension const inputExtension = path.extname(inputPath).toLowerCase().substring(1); let configObject; // Parse input file if (['yaml', 'yml'].includes(inputExtension)) { configObject = yaml.load(inputContent); } else if (inputExtension === 'json') { configObject = JSON.parse(inputContent); } else { throw new Error(`Unsupported input format: ${inputExtension}. Must be yaml, yml, or json.`); } // Generate output path if not provided if (!outputPath) { const inputDir = path.dirname(inputPath); const inputBaseName = path.basename(inputPath, path.extname(inputPath)); outputPath = path.join(inputDir, `${inputBaseName}.${outputFormat}`); } // Write to the output file let outputContent; if (outputFormat === 'yaml') { outputContent = yaml.dump(configObject, { indent: 2, lineWidth: 120, noRefs: true }); } else { // json outputContent = JSON.stringify(configObject, null, 2); } await fs.writeFile(outputPath, outputContent); return outputPath; } catch (error) { throw new Error(`Error converting configuration: ${error.message}`); } } /** * Load a configuration file regardless of format (YAML or JSON) * @param {string} configPath - Path to the configuration file * @returns {Promise<Object>} Parsed configuration object */ async function loadConfig(configPath) { try { // Read the input file const content = await fs.readFile(configPath, 'utf8'); // Determine format from file extension const extension = path.extname(configPath).toLowerCase().substring(1); // Parse based on format if (['yaml', 'yml'].includes(extension)) { return yaml.load(content); } else if (extension === 'json') { return JSON.parse(content); } else { throw new Error(`Unsupported file format: ${extension}. Must be yaml, yml, or json.`); } } catch (error) { throw new Error(`Error loading configuration: ${error.message}`); } } /** * Save a configuration object to a file * @param {Object} config - Configuration object * @param {string} outputPath - Path to save the configuration * @param {string} [format] - Format to save as (defaults to using file extension) * @returns {Promise<string>} Path to the saved file */ async function saveConfig(config, outputPath, format) { try { // Determine format from file extension if not provided if (!format) { const extension = path.extname(outputPath).toLowerCase().substring(1); if (['yaml', 'yml'].includes(extension)) { format = 'yaml'; } else if (extension === 'json') { format = 'json'; } else { throw new Error(`Unsupported file format: ${extension}. Must be yaml, yml, or json.`); } } // Normalize format format = format.toLowerCase(); if (format === 'yml') { format = 'yaml'; } // Generate content based on format let content; if (format === 'yaml') { content = yaml.dump(config, { indent: 2, lineWidth: 120, noRefs: true }); } else if (format === 'json') { content = JSON.stringify(config, null, 2); } else { throw new Error(`Unsupported output format: ${format}. Must be yaml or json.`); } // Ensure directory exists const outputDir = path.dirname(outputPath); await fs.mkdir(outputDir, { recursive: true }); // Write to file await fs.writeFile(outputPath, content); return outputPath; } catch (error) { throw new Error(`Error saving configuration: ${error.message}`); } } /** * Standardize configuration files by converting them all to a specific format * @param {string} directory - Directory containing configuration files * @param {string} targetFormat - Format to standardize to (yaml or json) * @param {boolean} [recursive=false] - Whether to recursively process subdirectories * @returns {Promise<Object>} Summary of conversion operations */ async function standardizeConfigFiles(directory, targetFormat, recursive = false) { try { // Validate parameters if (!targetFormat || !['yaml', 'yml', 'json'].includes(targetFormat.toLowerCase())) { throw new Error('Target format must be "yaml", "yml", or "json"'); } // Normalize target format targetFormat = targetFormat.toLowerCase(); if (targetFormat === 'yml') { targetFormat = 'yaml'; } const stats = { processed: 0, converted: 0, skipped: 0, errors: [] }; // Read directory contents const items = await fs.readdir(directory, { withFileTypes: true }); // Process each item for (const item of items) { const itemPath = path.join(directory, item.name); if (item.isDirectory() && recursive) { // Recursively process subdirectory const subStats = await standardizeConfigFiles(itemPath, targetFormat, recursive); // Merge stats stats.processed += subStats.processed; stats.converted += subStats.converted; stats.skipped += subStats.skipped; stats.errors = [...stats.errors, ...subStats.errors]; } else if (item.isFile()) { // Check if it's a configuration file const extension = path.extname(item.name).toLowerCase().substring(1); if (['yaml', 'yml', 'json'].includes(extension)) { stats.processed++; // Skip if already in target format if ((extension === targetFormat) || (extension === 'yml' && targetFormat === 'yaml')) { stats.skipped++; continue; } try { // Convert to target format const outputPath = path.join( directory, `${path.basename(item.name, path.extname(item.name))}.${targetFormat}` ); await convertConfigFormat(itemPath, targetFormat, outputPath); stats.converted++; } catch (error) { stats.errors.push({ file: itemPath, error: error.message }); } } } } return stats; } catch (error) { throw new Error(`Error standardizing configuration files: ${error.message}`); } } module.exports = { convertConfigFormat, loadConfig, saveConfig, standardizeConfigFiles };