@syntropysoft/praetorian
Version:
Praetorian CLI – A universal multi-environment configuration validator for DevSecOps teams. Validate, compare, and secure YAML/ENV files with ease.
106 lines • 3.95 kB
JavaScript
;
/**
* ConfigLoaderService - Single Responsibility: Load and parse configuration files
*
* This service handles all configuration loading operations:
* - Loading Praetorian configuration files
* - Parsing YAML/JSON content
* - File existence validation
* - Format detection
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigLoaderService = void 0;
const fs_1 = __importDefault(require("fs"));
const yaml_1 = __importDefault(require("yaml"));
class ConfigLoaderService {
/**
* Load Praetorian configuration with guard clauses
*/
loadPraetorianConfig(configPath) {
// Guard clause: validate file exists
if (!fs_1.default.existsSync(configPath)) {
throw new Error(`Configuration file not found: ${configPath}`);
}
// Guard clause: validate file is readable
try {
const configContent = fs_1.default.readFileSync(configPath, 'utf8');
return yaml_1.default.parse(configContent);
}
catch (error) {
throw new Error(`Failed to parse configuration file ${configPath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
/**
* Load single configuration file with guard clauses
*/
loadConfigFile(filePath) {
// Guard clause: validate file exists
if (!fs_1.default.existsSync(filePath)) {
throw new Error(`Configuration file not found: ${filePath}`);
}
try {
const content = fs_1.default.readFileSync(filePath, 'utf8');
const format = this.determineFileFormat(filePath);
return {
path: filePath,
content: this.parseContent(content, format),
format
};
}
catch (error) {
throw new Error(`Failed to load configuration file ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
/**
* Load multiple configuration files (pure function)
*/
loadConfigFiles(filePaths) {
// Guard clause: validate input
if (!Array.isArray(filePaths) || filePaths.length === 0) {
return [];
}
return filePaths.map(filePath => this.loadConfigFile(filePath));
}
/**
* Separate existing and missing files (pure function)
*/
separateExistingAndMissingFiles(files) {
// Guard clause: validate input
if (!Array.isArray(files)) {
return { existingFiles: [], missingFiles: [] };
}
const existingFiles = files.filter(file => fs_1.default.existsSync(file));
const missingFiles = files.filter(file => !fs_1.default.existsSync(file));
return { existingFiles, missingFiles };
}
/**
* Determine file format (pure function)
*/
determineFileFormat(filePath) {
// Guard clause: validate input
if (typeof filePath !== 'string' || filePath.length === 0) {
throw new Error('Invalid file path provided');
}
return filePath.endsWith('.yaml') || filePath.endsWith('.yml') ? 'yaml' : 'json';
}
/**
* Parse content based on format (pure function)
*/
parseContent(content, format) {
// Guard clause: validate content
if (typeof content !== 'string') {
throw new Error('Content must be a string');
}
try {
return format === 'yaml' ? yaml_1.default.parse(content) : JSON.parse(content);
}
catch (error) {
throw new Error(`Failed to parse ${format} content: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
}
exports.ConfigLoaderService = ConfigLoaderService;
//# sourceMappingURL=ConfigLoaderService.js.map