This commit is contained in:
Benjamin Toby
2025-05-22 10:04:18 +01:00
parent 0fc2c7fb3e
commit 2618d472dc
4 changed files with 65 additions and 28 deletions
+2 -2
View File
@@ -1,3 +1,3 @@
export default function parseEnv(/** Env File Path */ envPath: string): {
export default function parseEnv(envFile: string): {
[k: string]: string;
};
} | undefined;
+31 -12
View File
@@ -5,17 +5,36 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = parseEnv;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
function parseEnv(/** Env File Path */ envPath) {
let obj = {};
const finalEnvPath = path_1.default.resolve(process.cwd(), envPath);
const envFile = fs_1.default.readFileSync(finalEnvPath, "utf-8");
const envLinesArr = envFile
.split(/\r\n|\n|\r/)
.filter((ln) => ln.match(/\=/));
envLinesArr.forEach((ln) => {
const keyValArr = ln.split("=");
obj[keyValArr[0]] = keyValArr[1] || "";
function parseEnv(envFile) {
if (!fs_1.default.existsSync(envFile))
return undefined;
const envTextContent = fs_1.default.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;
});
return obj;
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;
}