@syntropysoft/praetorian
Version:
Praetorian CLI – A universal multi-environment configuration validator for DevSecOps teams. Validate, compare, and secure YAML/ENV files with ease.
78 lines • 2.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TomlFileAdapter = void 0;
const AbstractFileAdapter_1 = require("../base/AbstractFileAdapter");
class TomlFileAdapter extends AbstractFileAdapter_1.AbstractFileAdapter {
canHandle(filePath) {
return filePath.endsWith('.toml');
}
async read(filePath) {
this.validateFileExists(filePath);
try {
const content = await this.readFileContent(filePath);
return this.parseTomlContent(content);
}
catch (error) {
throw new Error(`Failed to parse TOML file ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
parseTomlContent(content) {
const result = {};
const lines = content.split('\n');
let currentSection = null;
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
// Skip empty lines and comments
if (!line || line.startsWith('#')) {
continue;
}
// Handle section headers [section]
if (line.startsWith('[') && line.endsWith(']')) {
currentSection = line.slice(1, -1);
if (!result[currentSection]) {
result[currentSection] = {};
}
continue;
}
// Handle key-value pairs
const equalIndex = line.indexOf('=');
if (equalIndex > 0) {
const key = line.substring(0, equalIndex).trim();
const value = line.substring(equalIndex + 1).trim();
if (currentSection) {
result[currentSection][key] = this.parseValue(value);
}
else {
result[key] = this.parseValue(value);
}
}
}
return result;
}
parseValue(value) {
// Remove quotes
if ((value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))) {
return value.slice(1, -1);
}
// Parse booleans
if (value === 'true')
return true;
if (value === 'false')
return false;
// Parse numbers
if (!isNaN(Number(value))) {
return Number(value);
}
// Return as string
return value;
}
getFormat() {
return 'toml';
}
getSupportedExtensions() {
return ['.toml'];
}
}
exports.TomlFileAdapter = TomlFileAdapter;
//# sourceMappingURL=TomlFileAdapter.js.map