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

View File

@ -1,3 +1,3 @@
export default function parseEnv(/** Env File Path */ envPath: string): { export default function parseEnv(envFile: string): {
[k: string]: string; [k: string]: string;
}; } | undefined;

View File

@ -5,17 +5,36 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.default = parseEnv; exports.default = parseEnv;
const fs_1 = __importDefault(require("fs")); const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path")); function parseEnv(envFile) {
function parseEnv(/** Env File Path */ envPath) { if (!fs_1.default.existsSync(envFile))
let obj = {}; return undefined;
const finalEnvPath = path_1.default.resolve(process.cwd(), envPath); const envTextContent = fs_1.default.readFileSync(envFile, "utf-8");
const envFile = fs_1.default.readFileSync(finalEnvPath, "utf-8"); const envLines = envTextContent
const envLinesArr = envFile .split("\n")
.split(/\r\n|\n|\r/) .map((ln) => ln.trim())
.filter((ln) => ln.match(/\=/)); .filter((ln) => {
envLinesArr.forEach((ln) => { const commentLine = ln.match(/^\#/);
const keyValArr = ln.split("="); const validEnv = ln.match(/.*\=/);
obj[keyValArr[0]] = keyValArr[1] || ""; 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;
} }

View File

@ -1,18 +1,36 @@
import fs from "fs"; import fs from "fs";
import path from "path";
export default function parseEnv(/** Env File Path */ envPath: string) { export default function parseEnv(envFile: string) {
let obj: { [k: string]: string } = {}; if (!fs.existsSync(envFile)) return undefined;
const finalEnvPath = path.resolve(process.cwd(), envPath); const envTextContent = fs.readFileSync(envFile, "utf-8");
const envFile = fs.readFileSync(finalEnvPath, "utf-8"); const envLines = envTextContent
const envLinesArr = envFile .split("\n")
.split(/\r\n|\n|\r/) .map((ln) => ln.trim())
.filter((ln) => ln.match(/\=/)); .filter((ln) => {
const commentLine = ln.match(/^\#/);
const validEnv = ln.match(/.*\=/);
envLinesArr.forEach((ln) => { if (commentLine) return false;
const keyValArr = ln.split("="); if (validEnv) return true;
obj[keyValArr[0]] = keyValArr[1] || ""; 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;
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/datasquirel", "name": "@moduletrace/datasquirel",
"version": "4.6.2", "version": "4.6.3",
"description": "Cloud-based SQL data management tool", "description": "Cloud-based SQL data management tool",
"main": "dist/index.js", "main": "dist/index.js",
"bin": { "bin": {