fix: js/py dotenv unable to parse quoted value or duplicated (#114)

This commit is contained in:
sigoden
2024-10-18 20:19:10 +08:00
committed by GitHub
parent 615aa25266
commit e098f7f43a
4 changed files with 98 additions and 38 deletions
+27 -9
View File
@@ -54,20 +54,38 @@ async function setupEnv(rootDir, toolName) {
}
async function loadEnv(filePath) {
let lines = [];
try {
const data = await readFile(filePath, "utf-8");
const lines = data.split("\n");
lines = data.split("\n");
} catch {
return;
}
lines.forEach((line) => {
if (line.trim().startsWith("#") || line.trim() === "") return;
const envVars = new Map();
const [key, ...value] = line.split("=");
const envName = key.trim();
if (!process.env[envName]) {
process.env[envName] = value.join("=").trim();
for (const line of lines) {
if (line.trim().startsWith("#") || line.trim() === "") {
continue;
}
const [key, ...valueParts] = line.split("=");
const envName = key.trim();
if (!process.env[envName]) {
let envValue = valueParts.join("=").trim();
if (envValue.startsWith('"') && envValue.endsWith('"')) {
envValue = envValue.slice(1, -1);
} else if (envValue.startsWith("'") && envValue.endsWith("'")) {
envValue = envValue.slice(1, -1);
}
});
} catch { }
envVars.set(envName, envValue);
}
}
for (const [envName, envValue] of envVars.entries()) {
process.env[envName] = envValue;
}
}
async function run(toolPath, toolFunc, toolData) {