env-sentinel
Version:
Zero-dependency tool that auto-validates .env files against schema.env, with optional fallback and secure warnings.
19 lines (18 loc) • 743 B
JavaScript
import { existsSync, readFileSync } from 'node:fs';
import { inferType } from './infer-type.js';
import { parseEnvContent } from '../parsers/parse-env-content.js';
export function generateSchemaFromEnv(envPath) {
if (!existsSync(envPath)) {
throw new Error(`File not found: ${envPath}`);
}
const raw = readFileSync(envPath, 'utf-8');
const env = parseEnvContent(raw);
const schemaEntries = Object.entries(env).map(([key, value]) => {
const type = inferType(value);
const rule = type ? `required|${type}` : 'required';
return { key, rule };
});
return (`# Generated from ${envPath}\n` +
schemaEntries.map((entry) => `${entry.key}=${entry.rule}`).join('\n') +
'\n');
}