envilder
Version:
A CLI and GitHub Action that securely centralizes your environment variables from AWS SSM or Azure Key Vault as a single source of truth
64 lines • 2.18 kB
JavaScript
import { SecretProviderType } from '../domain/secret-provider-type.js';
const CONFIG_KEY = '$config';
const PROVIDER_MAP = {
aws: SecretProviderType.Aws,
azure: SecretProviderType.Azure,
};
/**
* Parses a JSON map-file string into a {@link ParsedMapFile}.
*/
export class MapFileParser {
/**
* Parse a JSON map-file string, extracting `$config` and variable mappings.
*
* @param json - Raw JSON string of the map file.
* @returns Parsed config and variable mappings.
*/
parse(json) {
const raw = this.parseRawJson(json);
const mappings = new Map();
let config = {};
for (const [key, value] of Object.entries(raw)) {
if (key.startsWith('$')) {
if (key === CONFIG_KEY) {
config = this.parseConfig(value);
}
continue;
}
if (typeof value === 'string') {
mappings.set(key, value);
}
}
return { config, mappings };
}
parseRawJson(json) {
let raw;
try {
raw = JSON.parse(json);
}
catch (_a) {
throw new Error('Invalid map file: content is not valid JSON');
}
if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
throw new Error('Invalid map file: root must be a JSON object');
}
return raw;
}
parseConfig(value) {
if (typeof value !== 'object' || value === null) {
return {};
}
const obj = value;
const providerStr = typeof obj.provider === 'string' ? obj.provider.toLowerCase() : undefined;
const config = {
provider: providerStr ? PROVIDER_MAP[providerStr] : undefined,
vaultUrl: typeof obj.vaultUrl === 'string' ? obj.vaultUrl : undefined,
profile: typeof obj.profile === 'string' ? obj.profile : undefined,
};
if (providerStr && !config.provider) {
throw new Error(`Unknown provider: '${obj.provider}'. Supported: aws, azure`);
}
return config;
}
}
//# sourceMappingURL=map-file-parser.js.map