Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cli/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,18 @@ export function readEnvFile(path: string): Map<string, string> {
const out = new Map<string, string>();
if (!existsSync(path)) return out;
for (const raw of readFileSync(path, "utf8").split("\n")) {
const line = raw.trim();
let line = raw.trim();
if (!line || line.startsWith("#")) continue;
if (line.startsWith("export ")) line = line.slice(7).trimStart();
const eq = line.indexOf("=");
Comment on lines +220 to 223
if (eq <= 0) continue;
const key = line.slice(0, eq).trim();
if (isEnvVarName(key)) out.set(key, line.slice(eq + 1));
if (!isEnvVarName(key)) continue;
let val = line.slice(eq + 1).trim();
if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
val = val.slice(1, -1);
}
out.set(key, val);
Comment on lines +227 to +231
}
return out;
}
Expand Down
8 changes: 6 additions & 2 deletions cli/test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,20 @@ test("canonicalJson sorts keys and matches JSON.stringify's undefined semantics"
}
});

test("readEnvFile preserves hashes in unquoted values", (t) => {
test("readEnvFile preserves hashes in unquoted values and strips export/quotes", (t) => {
const dir = mkdtempSync(join(tmpdir(), "qm-env-"));
t.after(() => rmSync(dir, { recursive: true, force: true }));
const file = join(dir, ".env");
writeFileSync(file, "# ignored\nTOKEN=abc#def\nURL=https://host/path#fragment\n");
writeFileSync(
file,
'# ignored\nTOKEN=abc#def\nURL="https://host/path#fragment"\nexport SECRET=\'mysecret\'\n',
);
assert.deepEqual(
[...readEnvFile(file)],
[
["TOKEN", "abc#def"],
["URL", "https://host/path#fragment"],
["SECRET", "mysecret"],
],
);
});
Expand Down