UNPKG

dotenv-constraint

Version:

A lightweight package to enforce constraints on environment variables in JavaScript, ensuring required variables are defined and match expected types.

145 lines (144 loc) 4.45 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { default: () => index_default, validateEnv: () => validateEnv }); module.exports = __toCommonJS(index_exports); var import_fs = __toESM(require("fs"), 1); var import_path = __toESM(require("path"), 1); var validateEnv = (config) => { var _a, _b; const appRoot = process.cwd(); let envFile; let envSchemaFile; try { envFile = import_fs.default.readFileSync( import_path.default.join(appRoot, (_a = config == null ? void 0 : config.dotenvPath) != null ? _a : ".env"), "utf8" ); envSchemaFile = import_fs.default.readFileSync( import_path.default.join(appRoot, (_b = config == null ? void 0 : config.schemaPath) != null ? _b : ".env.schema"), "utf8" ); } catch (error) { return { errors: [ { code: "file_not_found" } ], success: false }; } const envVariables = extractEnvVariables(envFile); const envSchema = extractEnvSchema(envSchemaFile); const result = checkConstraints(envVariables, envSchema); return { errors: result, success: result.length === 0 }; }; var extractEnvVariables = (dotenvFile) => { const envVariables = []; for (const line of dotenvFile.split("\n")) { if (line.includes("=")) { const [name, value] = line.split("="); envVariables.push({ name, value }); } } return envVariables; }; var extractEnvSchema = (schemaFile) => { const schema = []; for (const line of schemaFile.split("\n")) { if (line.includes("=")) { const name = line.split("=")[0]; const optional = line.includes("#optional"); const number = line.includes("#number"); schema.push({ name, constraints: { optional, number } }); } } return schema; }; var checkConstraints = (envVariables, envSchema) => { const errors = []; for (const { name, constraints } of envSchema) { const envVariable = envVariables.find((v) => v.name === name); if (!envVariable && !constraints.optional) { errors.push({ variable: name, code: "missing" }); } else if (envVariable) { if (constraints.number) { const result = isNumber(envVariable); if (result !== true) { errors.push(result); } } if (!constraints.optional) { const result = isRequired(envVariable); if (result !== true) { errors.push(result); } } } } return errors; }; var isRequired = (envVariable) => { if (!envVariable.value) { return { variable: envVariable.name, code: "empty" }; } return true; }; var isNumber = (envVariable) => { if (envVariable.value && isNaN(Number(envVariable.value))) { return { variable: envVariable.name, code: "invalid_type", expected: "number" }; } return true; }; var index_default = { validateEnv }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { validateEnv }); //# sourceMappingURL=index.cjs.map