@syntropysoft/praetorian
Version:
Praetorian CLI – A universal multi-environment configuration validator for DevSecOps teams. Validate, compare, and secure YAML/ENV files with ease.
97 lines • 3.57 kB
JavaScript
;
/**
* TODO: DECLARATIVE PROGRAMMING PATTERN
*
* This file demonstrates excellent declarative programming practices:
* - Pure functions with arrow functions and functional composition
* - Immutable data transformations with Object.keys() and filter()
* - Functional array methods (map, filter)
* - Promise.all() for concurrent operations
* - Null coalescing operators (??)
* - No imperative loops or state mutations
*
* Mutation Score: 85.25% - Functional patterns make testing straightforward!
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvironmentManager = void 0;
// Pure functions for file format detection
const isYamlFile = (filePath) => filePath.endsWith('.yaml') || filePath.endsWith('.yml');
const isJsonFile = (filePath) => filePath.endsWith('.json');
const getFileFormat = (filePath) => isYamlFile(filePath) ? 'yaml' : 'json';
// Pure function for parsing content
const parseContent = (content, format) => {
if (format === 'yaml') {
const yaml = require('yaml');
return yaml.parse(content);
}
return JSON.parse(content);
};
// Pure function for creating ConfigFile
const createConfigFile = (filePath, content) => ({
path: filePath,
content: parseContent(content, getFileFormat(filePath)),
format: getFileFormat(filePath)
});
// Pure function for creating validation result
const createValidationResult = (environment, configFile, result) => ({
environment,
configFile,
success: result.success ?? false,
errors: result.errors ?? [],
warnings: result.warnings ?? []
});
class EnvironmentManager {
constructor() {
this.fs = require('fs');
this.yaml = require('yaml');
}
/**
* Load environment configuration from file
*/
loadEnvironmentConfig(filePath) {
if (!this.fs.existsSync(filePath)) {
throw new Error(`Environment configuration file not found: ${filePath}`);
}
const content = this.fs.readFileSync(filePath, 'utf8');
return this.yaml.parse(content);
}
/**
* Validate specific environment
*/
async validateEnvironment(environment, environmentConfig, validationFunction) {
const configFile = environmentConfig[environment];
if (!configFile) {
throw new Error(`Environment '${environment}' not found in configuration`);
}
if (!this.fs.existsSync(configFile)) {
throw new Error(`Configuration file not found: ${configFile}`);
}
const content = this.fs.readFileSync(configFile, 'utf8');
const configFileObj = createConfigFile(configFile, content);
const result = await validationFunction([configFileObj]);
return createValidationResult(environment, configFile, result);
}
/**
* Validate all environments
*/
async validateAllEnvironments(environmentConfig, validationFunction) {
const environments = Object.keys(environmentConfig);
return Promise.all(environments.map(env => this.validateEnvironment(env, environmentConfig, validationFunction)));
}
/**
* Get environment summary
*/
getEnvironmentSummary(results) {
const total = results.length;
const passed = results.filter(r => r.success).length;
const failed = total - passed;
return {
total,
passed,
failed,
success: failed === 0
};
}
}
exports.EnvironmentManager = EnvironmentManager;
//# sourceMappingURL=EnvironmentManager.js.map