datasquirel/dist/package-shared/utils/parse-env.js
Benjamin Toby 7e8bb37c09 Updates
2025-07-05 14:59:30 +01:00

36 lines
1.1 KiB
JavaScript

import fs from "fs";
export default function parseEnv(
/** The file path to the env. Eg. /app/.env */ envFile) {
if (!fs.existsSync(envFile))
return undefined;
const envTextContent = fs.readFileSync(envFile, "utf-8");
const envLines = envTextContent
.split("\n")
.map((ln) => ln.trim())
.filter((ln) => {
const commentLine = ln.match(/^\#/);
const validEnv = ln.match(/.*\=/);
if (commentLine)
return false;
if (validEnv)
return true;
return false;
});
const newEnvObj = {};
for (let i = 0; i < envLines.length; i++) {
const emvLine = envLines[i];
const envLineArr = emvLine.split("=");
const envTitle = envLineArr[0];
const envValue = envLineArr[1];
if (!(envTitle === null || envTitle === void 0 ? void 0 : envTitle.match(/./)))
continue;
if (envValue === null || envValue === void 0 ? void 0 : envValue.match(/./)) {
newEnvObj[envTitle] = envValue;
}
else {
newEnvObj[envTitle] = "";
}
}
return newEnvObj;
}