UNPKG

@syntropysoft/praetorian

Version:

Praetorian CLI – A universal multi-environment configuration validator for DevSecOps teams. Validate, compare, and secure YAML/ENV files with ease.

256 lines 7.54 kB
"use strict"; /** * @file src/infrastructure/parsers/config-parsing/ConfigFileOperations.ts * @description Pure functions for configuration file operations */ 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.getFileExtension = exports.joinPath = exports.getDirectoryName = exports.resolvePath = exports.stringifyToYaml = exports.parseYamlContent = exports.createDirectorySync = exports.writeFileSync = exports.readFileSync = exports.fileExists = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const yaml = __importStar(require("yaml")); /** * Checks if a file exists * @param filePath - Path to check * @returns True if file exists, false otherwise */ const fileExists = (filePath) => { // Guard clause: empty path if (!filePath || filePath.trim().length === 0) { return false; } try { return fs.existsSync(filePath); } catch { return false; } }; exports.fileExists = fileExists; /** * Reads a file synchronously * @param filePath - Path to the file * @returns File operation result */ const readFileSync = (filePath) => { // Guard clause: empty path if (!filePath || filePath.trim().length === 0) { return { success: false, error: 'File path cannot be empty', }; } // Guard clause: file doesn't exist if (!(0, exports.fileExists)(filePath)) { return { success: false, error: `File not found: ${filePath}`, }; } try { const content = fs.readFileSync(filePath, 'utf8'); return { success: true, content, }; } catch (error) { return { success: false, error: `Failed to read file: ${error.message}`, }; } }; exports.readFileSync = readFileSync; /** * Writes content to a file synchronously * @param filePath - Path to the file * @param content - Content to write * @returns File operation result */ const writeFileSync = (filePath, content) => { // Guard clause: empty path if (!filePath || filePath.trim().length === 0) { return { success: false, error: 'File path cannot be empty', }; } // Guard clause: empty content if (content === undefined || content === null) { return { success: false, error: 'Content cannot be undefined or null', }; } try { fs.writeFileSync(filePath, content); return { success: true, }; } catch (error) { return { success: false, error: `Failed to write file: ${error.message}`, }; } }; exports.writeFileSync = writeFileSync; /** * Creates a directory if it doesn't exist * @param dirPath - Directory path * @returns File operation result */ const createDirectorySync = (dirPath) => { // Guard clause: empty path if (!dirPath || dirPath.trim().length === 0) { return { success: false, error: 'Directory path cannot be empty', }; } try { if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath, { recursive: true }); } return { success: true, }; } catch (error) { return { success: false, error: `Failed to create directory: ${error.message}`, }; } }; exports.createDirectorySync = createDirectorySync; /** * Parses YAML content * @param content - YAML content * @returns Parsed content */ const parseYamlContent = (content) => { // Guard clause: empty content if (!content || content.trim().length === 0) { return null; } try { return yaml.parse(content); } catch (error) { throw new Error(`Failed to parse YAML: ${error.message}`); } }; exports.parseYamlContent = parseYamlContent; /** * Stringifies content to YAML * @param content - Content to stringify * @returns YAML string */ const stringifyToYaml = (content) => { // Guard clause: null or undefined content if (content === null || content === undefined) { return ''; } try { return yaml.stringify(content, { indent: 2 }); } catch (error) { throw new Error(`Failed to stringify to YAML: ${error.message}`); } }; exports.stringifyToYaml = stringifyToYaml; /** * Resolves a path relative to a base directory * @param basePath - Base directory path * @param relativePath - Relative path * @returns Resolved absolute path */ const resolvePath = (basePath, relativePath) => { // Guard clause: empty base path if (!basePath || basePath.trim().length === 0) { return relativePath || ''; } // Guard clause: empty relative path if (!relativePath || relativePath.trim().length === 0) { return basePath; } return path.resolve(basePath, relativePath); }; exports.resolvePath = resolvePath; /** * Gets the directory name of a file path * @param filePath - File path * @returns Directory name */ const getDirectoryName = (filePath) => { // Guard clause: empty path if (!filePath || filePath.trim().length === 0) { return ''; } return path.dirname(filePath); }; exports.getDirectoryName = getDirectoryName; /** * Joins path segments * @param segments - Path segments * @returns Joined path */ const joinPath = (...segments) => { // Guard clause: no segments if (!segments || segments.length === 0) { return ''; } // Filter out empty segments const validSegments = segments.filter(segment => segment && segment.trim().length > 0); return path.join(...validSegments); }; exports.joinPath = joinPath; /** * Gets file extension * @param filePath - File path * @returns File extension (including the dot) */ const getFileExtension = (filePath) => { // Guard clause: empty path if (!filePath || filePath.trim().length === 0) { return ''; } return path.extname(filePath); }; exports.getFileExtension = getFileExtension; //# sourceMappingURL=ConfigFileOperations.js.map