34 lines
812 B
JavaScript
34 lines
812 B
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
let envLoaded = false;
|
|
|
|
function loadEnv(envPath = path.resolve(__dirname, "..", "..", ".env")) {
|
|
if (envLoaded) return;
|
|
envLoaded = true;
|
|
|
|
if (!fs.existsSync(envPath)) return;
|
|
|
|
const content = fs.readFileSync(envPath, "utf8");
|
|
const lines = content.split(/\r?\n/);
|
|
|
|
for (const line of lines) {
|
|
const trimmed = line.trim();
|
|
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
|
|
const separator = trimmed.indexOf("=");
|
|
if (separator === -1) continue;
|
|
|
|
const key = trimmed.slice(0, separator).trim();
|
|
const value = trimmed.slice(separator + 1);
|
|
|
|
if (!key || process.env[key] !== undefined) continue;
|
|
|
|
process.env[key] = value;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
loadEnv,
|
|
};
|