Skip to content

Commit 0412084

Browse files
committed
fix: parsing env variables in variable-editor
1 parent cd88d4e commit 0412084

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

src/components/VariableEditor.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import InputGroupText from "react-bootstrap/InputGroupText";
1717
import OverlayTrigger from "react-bootstrap/OverlayTrigger";
1818
import Tooltip from "react-bootstrap/Tooltip";
1919
import { v4 as uuidv4 } from "uuid";
20+
import { parseEnvVariables } from "~/utils/parseEnvVariables";
2021
import type { IconProp } from "@fortawesome/fontawesome-svg-core";
2122

2223
type VariableEditorProps = {
@@ -81,8 +82,8 @@ const VariableEditor = ({
8182
})();
8283
if (text) {
8384
if (text.includes("=")) {
84-
const newItems = text.replace(/\r\n/g, "\n").split("\n").map(line => {
85-
const [key, value] = line.split("=");
85+
const newItems = Object.entries(parseEnvVariables(text)).map(line => {
86+
const [key, value] = line;
8687
return {
8788
id: crypto.randomUUID(),
8889
key,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, expect, it } from "@jest/globals";
2+
import { parseEnvVariables } from "./parseEnvVariables";
3+
4+
/* eslint-disable @stylistic/max-len */
5+
6+
describe("parseEnvVariables", () => {
7+
it("should parse environment variables correctly from a text", () => {
8+
const input = `
9+
FOO=bar
10+
BAZ="qux"
11+
`;
12+
const output = parseEnvVariables(input);
13+
expect(output).toEqual({
14+
FOO: "bar",
15+
BAZ: '"qux"',
16+
});
17+
});
18+
19+
it("should parse multi-line environment variables correctly from a text", () => {
20+
const input = `
21+
FOO=bar
22+
MULTI_LINE_KEY="-----BEGIN PRIVATE KEY-----\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAA\n-----END PRIVATE KEY-----"
23+
`;
24+
const output = parseEnvVariables(input);
25+
expect(output).toEqual({
26+
FOO: "bar",
27+
MULTI_LINE_KEY: "-----BEGIN PRIVATE KEY-----\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAA\n-----END PRIVATE KEY-----",
28+
});
29+
});
30+
31+
it("should ignore commented lines", () => {
32+
const input = `
33+
FOO=bar
34+
# BAZ=qux
35+
`;
36+
const output = parseEnvVariables(input);
37+
expect(output).toEqual({
38+
FOO: "bar",
39+
});
40+
});
41+
});

src/utils/parseEnvVariables.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export function parseEnvVariables(text: string): Record<string, string> {
2+
const lines = text.split("\n").map(line => line.trim());
3+
const envVariables: Record<string, string> = {};
4+
5+
let currentKey: string | null = null;
6+
let currentValue: string[] = [];
7+
8+
for (const line of lines) {
9+
if (line.startsWith("#")) {
10+
continue; // Skip commented lines
11+
}
12+
if (currentKey) {
13+
currentValue.push(line);
14+
if (line.endsWith('"')) {
15+
envVariables[currentKey] = currentValue.join("\n").slice(1, -1); // Remove surrounding quotes
16+
currentKey = null;
17+
currentValue = [];
18+
}
19+
} else {
20+
const [key, ...valueParts] = line.split("=");
21+
const value = valueParts.join("=");
22+
if (key && value.startsWith('"') && !value.endsWith('"')) {
23+
currentKey = key.trim();
24+
currentValue.push(value);
25+
} else if (key) {
26+
envVariables[key.trim()] = value.trim();
27+
}
28+
}
29+
}
30+
31+
return envVariables;
32+
}

0 commit comments

Comments
 (0)