UNPKG

env-sentinel

Version:

Zero-dependency tool that auto-validates .env files against schema.env, with optional fallback and secure warnings.

44 lines (43 loc) 1.52 kB
import { REFERENCE_REGEX, getReferencedKey } from '../utils/index.js'; export function parseSchemaContent(rawSchemaContent) { const lines = rawSchemaContent.split(/\r?\n/); const entries = []; const entriesWithReferences = []; for (const line of lines) { const trimmedLine = line.trim(); if (!trimmedLine || trimmedLine.startsWith('#')) { continue; } const equalIndex = trimmedLine.indexOf('='); if (equalIndex === -1) { continue; } const key = trimmedLine.substring(0, equalIndex).trim(); const ruleString = trimmedLine.substring(equalIndex + 1).trim(); if (!key) { continue; } const entry = { key, rule: ruleString }; if (REFERENCE_REGEX.test(ruleString)) { entriesWithReferences.push({ entry, originalRule: ruleString }); } else { entries.push(entry); } } for (const { entry, originalRule } of entriesWithReferences) { entry.rule = originalRule.replace(REFERENCE_REGEX, (match) => { const referencedKey = getReferencedKey(match); const referencedEntry = entries.find(e => e.key === referencedKey); if (!referencedEntry) { throw new Error(`Referenced key "${referencedKey}" not found in the schema.`); } return referencedEntry.rule; }); entries.push(entry); } return entries; }