env-sentinel
Version:
Zero-dependency tool that auto-validates .env files against schema.env, with optional fallback and secure warnings.
34 lines (33 loc) • 1.32 kB
JavaScript
import { REFERENCE_REGEX, getReferencedKey } from '../utils/index.js';
export function parseEnvContent(rawEnvContent) {
const env = {};
const lines = rawEnvContent.split(/\r?\n/).filter((line) => line.trim() && !line.trim().startsWith('#'));
const linesWithReferences = [];
for (const line of lines) {
const [key, ...rest] = line.split('=');
const trimmedKey = key.trim();
if (!trimmedKey)
continue;
let value = rest.join('=').trim();
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
value = value.slice(1, -1);
}
if (REFERENCE_REGEX.test(value)) {
linesWithReferences.push({ key: trimmedKey, value });
continue;
}
env[trimmedKey] = value;
}
for (const line of linesWithReferences) {
const { key, value } = line;
env[key] = value.replace(REFERENCE_REGEX, (match) => {
const referencedKey = getReferencedKey(match);
const referencedValue = env[referencedKey];
if (referencedValue === undefined) {
throw new Error(`Referenced key "${referencedKey}" not found in the env.`);
}
return referencedValue;
});
}
return env;
}