Updates
This commit is contained in:
@@ -1,18 +1,36 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
export default function parseEnv(/** Env File Path */ envPath: string) {
|
||||
let obj: { [k: string]: string } = {};
|
||||
const finalEnvPath = path.resolve(process.cwd(), envPath);
|
||||
const envFile = fs.readFileSync(finalEnvPath, "utf-8");
|
||||
const envLinesArr = envFile
|
||||
.split(/\r\n|\n|\r/)
|
||||
.filter((ln) => ln.match(/\=/));
|
||||
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(/.*\=/);
|
||||
|
||||
envLinesArr.forEach((ln) => {
|
||||
const keyValArr = ln.split("=");
|
||||
obj[keyValArr[0]] = keyValArr[1] || "";
|
||||
});
|
||||
if (commentLine) return false;
|
||||
if (validEnv) return true;
|
||||
return false;
|
||||
});
|
||||
|
||||
return obj;
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user