Allow configuring environmental variables in panel (#626)

- [ ] Add test cases
This commit is contained in:
Will Chen
2025-07-11 10:52:52 -07:00
committed by GitHub
parent 4b84b12fe3
commit 2c284d0f20
18 changed files with 1212 additions and 7 deletions

View File

@@ -0,0 +1,71 @@
/**
* DO NOT USE LOGGER HERE.
* Environment variables are sensitive and should not be logged.
*/
import { EnvVar } from "../ipc_types";
// Helper function to parse .env.local file content
export function parseEnvFile(content: string): EnvVar[] {
const envVars: EnvVar[] = [];
const lines = content.split("\n");
for (const line of lines) {
const trimmedLine = line.trim();
// Skip empty lines and comments
if (!trimmedLine || trimmedLine.startsWith("#")) {
continue;
}
// Parse key=value pairs
const equalIndex = trimmedLine.indexOf("=");
if (equalIndex > 0) {
const key = trimmedLine.substring(0, equalIndex).trim();
const value = trimmedLine.substring(equalIndex + 1).trim();
// Handle quoted values with potential inline comments
let cleanValue = value;
if (value.startsWith('"')) {
// Find the closing quote, handling escaped quotes
let endQuoteIndex = -1;
for (let i = 1; i < value.length; i++) {
if (value[i] === '"' && value[i - 1] !== "\\") {
endQuoteIndex = i;
break;
}
}
if (endQuoteIndex !== -1) {
cleanValue = value.slice(1, endQuoteIndex);
// Unescape escaped quotes
cleanValue = cleanValue.replace(/\\"/g, '"');
}
} else if (value.startsWith("'")) {
// Find the closing quote for single quotes
const endQuoteIndex = value.indexOf("'", 1);
if (endQuoteIndex !== -1) {
cleanValue = value.slice(1, endQuoteIndex);
}
}
// For unquoted values, keep everything as-is (including potential # symbols)
envVars.push({ key, value: cleanValue });
}
}
return envVars;
}
// Helper function to serialize environment variables to .env.local format
export function serializeEnvFile(envVars: EnvVar[]): string {
return envVars
.map(({ key, value }) => {
// Add quotes if value contains spaces or special characters
const needsQuotes = /[\s#"'=&?]/.test(value);
const quotedValue = needsQuotes
? `"${value.replace(/"/g, '\\"')}"`
: value;
return `${key}=${quotedValue}`;
})
.join("\n");
}