UNPKG

@syntropysoft/praetorian

Version:

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

102 lines 3.49 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.parseTomlContent = exports.TomlFileAdapter = void 0; const toml = __importStar(require("toml")); const AbstractFileAdapter_1 = require("../base/AbstractFileAdapter"); /** * TOML File Adapter - Functional Programming * * Single Responsibility: Parse TOML configuration files only * Uses toml library for robust TOML parsing */ class TomlFileAdapter extends AbstractFileAdapter_1.AbstractFileAdapter { canHandle(filePath) { // Guard clause: no file path if (!filePath || typeof filePath !== 'string') { return false; } return isTomlFile(filePath); } async read(filePath) { // Guard clause: no file path if (!filePath || typeof filePath !== 'string') { throw new Error('File path is required'); } this.validateFileExists(filePath); try { const content = await this.readFileContent(filePath); return (0, exports.parseTomlContent)(content); } catch (error) { throw new Error(`Failed to parse TOML file ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`); } } getFormat() { return 'toml'; } getSupportedExtensions() { return ['.toml']; } } exports.TomlFileAdapter = TomlFileAdapter; /** * Pure function to check if file is TOML */ const isTomlFile = (filePath) => { // Guard clause: no file path if (!filePath) { return false; } return filePath.endsWith('.toml'); }; /** * Pure function to parse TOML content using toml library */ const parseTomlContent = (content) => { // Guard clause: no content if (!content || typeof content !== 'string') { return {}; } try { const result = toml.parse(content); return result || {}; } catch (error) { throw new Error(`TOML parsing failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } }; exports.parseTomlContent = parseTomlContent; //# sourceMappingURL=TomlFileAdapter.js.map