Skip to content

Commit 33f79f1

Browse files
committed
fix: better .env loading
1 parent 311a132 commit 33f79f1

2 files changed

Lines changed: 65 additions & 12 deletions

File tree

auth.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ export function createTrpcClient(debug: boolean, deployUrl: string) {
6262
errorLink,
6363
retryLink({
6464
retry({ error: err }) {
65-
if (
66-
!(err?.data?.code !== "NOT_AUTHENTICATED" &&
67-
err?.data?.code !== "TOKEN_EXPIRED")
68-
) {
65+
const code = err?.data?.code;
66+
if (!(code === "NOT_AUTHENTICATED" || code === "TOKEN_EXPIRED")) {
6967
return false;
7068
}
7169

env.ts

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface EnvVar {
99
id: string;
1010
key: string;
1111
value: string;
12+
is_secret: boolean;
1213
context_ids: string[];
1314
}
1415

@@ -346,17 +347,71 @@ export const envLoadCommand = new Command<EnvCommandContext>()
346347

347348
const variables = dotEnvParse(await Deno.readTextFile(file));
348349

350+
// deno-lint-ignore no-explicit-any
351+
const existingEnvVars: EnvVar[] = await (trpcClient.envVarsContexts as any)
352+
.list
353+
.query({
354+
org: orgAndApp.org,
355+
app: orgAndApp.app,
356+
});
357+
358+
const addEnvVars = [];
359+
let updateEnvVars = [];
360+
361+
for (const [key, value] of Object.entries(variables)) {
362+
const existing = existingEnvVars.find((envVar) => envVar.key === key);
363+
if (existing) {
364+
updateEnvVars.push({
365+
id: existing.id,
366+
key,
367+
value,
368+
is_secret: options.secrets?.includes(key) ?? existing.is_secret,
369+
context_ids: existing.context_ids,
370+
});
371+
} else {
372+
addEnvVars.push({
373+
app_id: fullApp.id,
374+
key,
375+
value,
376+
is_secret: options.secrets?.includes(key) ?? false,
377+
context_ids: null,
378+
});
379+
}
380+
}
381+
382+
if (updateEnvVars.length > 0) {
383+
console.log("The following env vars are already defined:");
384+
for (const updateEnvVar of updateEnvVars) {
385+
console.log(` - ${updateEnvVar.key}`);
386+
}
387+
console.log();
388+
outer: while (true) {
389+
const res = prompt("Would you like to replace these with your .env file? [y = Yes, n = No, s = Ignore/Skip]");
390+
if (res) {
391+
switch (res.toLowerCase()) {
392+
case "y": {
393+
break outer;
394+
}
395+
396+
// deno-lint-ignore no-fallthrough
397+
case "n": {
398+
error(options.debug, "Env vars are already defined, exiting");
399+
}
400+
case "s": {
401+
updateEnvVars = [];
402+
break outer;
403+
}
404+
}
405+
}
406+
}
407+
console.log();
408+
}
409+
349410
// deno-lint-ignore no-explicit-any
350411
await (trpcClient.envVarsContexts as any).updateEnvVars.mutate({
351412
org: orgAndApp.org,
352-
add: Object.entries(variables).map(([key, value]) => ({
353-
app_id: fullApp.id,
354-
key,
355-
value,
356-
is_secret: options.secrets?.includes(key) ?? false,
357-
context_ids: null,
358-
})),
359-
update: [],
413+
add: addEnvVars,
414+
update: updateEnvVars,
360415
remove: [],
361416
});
362417

0 commit comments

Comments
 (0)