UNPKG

env-sentinel

Version:

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

22 lines (21 loc) 1.02 kB
import { log } from './../utils/log.js'; import { validate } from './../validate/index.js'; import { parseEnvContent } from './../parsers/index.js'; import { parseSchemaContent } from './../parsers/index.js'; import { readEnvFile, readSchemaFile, handleResult } from './../utils/handler-utils.js'; export function handleValidate(envFilePath, schemaFilePath) { const rawEnvContent = readEnvFile(envFilePath); const rawSchemaContent = readSchemaFile(schemaFilePath); const envVars = parseEnvContent(rawEnvContent); const schemaEntries = parseSchemaContent(rawSchemaContent); const schemaVars = Object.fromEntries(schemaEntries.map(entry => [entry.key, entry.rule])); const result = validate(envVars, schemaVars, rawEnvContent); handleResult(result, envFilePath, 'validate'); if (result.isValid) { log.success(`${envFilePath} is valid`); } else { log.error(`${envFilePath} has validation errors`); throw new Error(`Validation failed for ${envFilePath}`); } }