37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
import fs from "fs";
|
|
|
|
export default function parseEnv(envFile: string) {
|
|
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: { [k: string]: string } = {};
|
|
|
|
for (let i = 0; i < envLines.length; i++) {
|
|
const emvLine = envLines[i];
|
|
const envLineArr = emvLine.split("=");
|
|
const envTitle = envLineArr[0];
|
|
const envValue = envLineArr[1] as string | undefined;
|
|
|
|
if (!envTitle?.match(/./)) continue;
|
|
|
|
if (envValue?.match(/./)) {
|
|
newEnvObj[envTitle] = envValue;
|
|
} else {
|
|
newEnvObj[envTitle] = "";
|
|
}
|
|
}
|
|
|
|
return newEnvObj;
|
|
}
|