From e098f7f43a7e8e4e85149bc5d24aa854a98cec66 Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 18 Oct 2024 20:19:10 +0800 Subject: fix: js/py dotenv unable to parse quoted value or duplicated (#114) --- scripts/run-tool.js | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'scripts/run-tool.js') diff --git a/scripts/run-tool.js b/scripts/run-tool.js index d610772..e4ac434 100755 --- a/scripts/run-tool.js +++ b/scripts/run-tool.js @@ -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; + } + + const envVars = new Map(); + + for (const line of lines) { + if (line.trim().startsWith("#") || line.trim() === "") { + continue; + } - lines.forEach((line) => { - if (line.trim().startsWith("#") || line.trim() === "") return; + const [key, ...valueParts] = line.split("="); + const envName = key.trim(); - const [key, ...value] = line.split("="); - const envName = key.trim(); - if (!process.env[envName]) { - process.env[envName] = value.join("=").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) { -- cgit v1.2.3