Presupuestador_web/e2e/scripts/env.mjs

29 lines
883 B
JavaScript

import { existsSync, readFileSync } from "node:fs";
import { resolve } from "node:path";
const root = resolve(import.meta.dirname, "../..");
const envFile = resolve(root, ".env.e2e");
if (existsSync(envFile)) {
for (const line of readFileSync(envFile, "utf8").split("\n")) {
const match = line.match(/^\s*([A-Z0-9_]+)=(.*)\s*$/);
if (match && process.env[match[1]] === undefined) process.env[match[1]] = match[2];
}
}
export const e2eEnv = (name) => {
const value = process.env[name];
if (!value) throw new Error(`Missing ${name}. Copy .env.e2e.example to .env.e2e and set it.`);
return value;
};
export const assertE2EDatabase = () => {
const database = e2eEnv("E2E_MYSQL_DATABASE");
if (!database.toLowerCase().includes("e2e")) {
throw new Error(`Refusing E2E operation for non-E2E database: ${database}`);
}
return database;
};
export { root };