Skip to content

Commit 78437b2

Browse files
authored
fix: better .env loading (#18)
1 parent eb9dfdb commit 78437b2

4 files changed

Lines changed: 79 additions & 83 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

deno.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
"imports": {
1818
"@cfa/gitignore-parser": "jsr:@cfa/gitignore-parser@^0.1.4",
1919
"@cliffy/command": "jsr:@cliffy/command@^1.0.0-rc.8",
20-
"@std/cli": "jsr:@std/cli@1.0.21",
20+
"@std/cli": "jsr:@std/cli@1.0.22",
2121
"@std/dotenv": "jsr:@std/dotenv@^0.225.5",
22-
"@std/encoding": "jsr:@std/encoding@^1.0.8",
22+
"@std/encoding": "jsr:@std/encoding@^1.0.10",
2323
"@std/fmt": "jsr:@std/fmt@^1.0.8",
24-
"@std/fs": "jsr:@std/fs@^1.0.14",
25-
"@std/jsonc": "jsr:@std/jsonc@^1.0.1",
26-
"@std/path": "jsr:@std/path@^1.0.8",
24+
"@std/fs": "jsr:@std/fs@^1.0.19",
25+
"@std/jsonc": "jsr:@std/jsonc@^1.0.2",
26+
"@std/path": "jsr:@std/path@^1.1.2",
2727
"@std/semver": "jsr:@std/semver@^1.0.5",
2828
"@std/tar": "jsr:@std/tar@^0.1.8",
2929
"@trpc/client": "npm:@trpc/client@^11.0.2",
@@ -35,7 +35,8 @@
3535
"prompts": "npm:prompts@2.4.2",
3636
"superjson": "npm:superjson@^2.2.2",
3737
"@deno/framework-detect": "jsr:@deno/framework-detect@^0",
38-
"temporal-polyfill": "npm:temporal-polyfill@^0.3.0"
38+
"temporal-polyfill": "npm:temporal-polyfill@^0.3.0",
39+
"prompts": "npm:prompts@2.4.2"
3940
},
4041
"exclude": [
4142
"astro-demo"

deno.lock

Lines changed: 5 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

env.ts

Lines changed: 65 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,73 @@ 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(
390+
"Would you like to replace these with your .env file? [y = Yes, n = No, s = Ignore/Skip]",
391+
);
392+
if (res) {
393+
switch (res.toLowerCase()) {
394+
case "y": {
395+
break outer;
396+
}
397+
398+
// deno-lint-ignore no-fallthrough
399+
case "n": {
400+
error(options.debug, "Env vars are already defined, exiting");
401+
}
402+
case "s": {
403+
updateEnvVars = [];
404+
break outer;
405+
}
406+
}
407+
}
408+
}
409+
console.log();
410+
}
411+
349412
// deno-lint-ignore no-explicit-any
350413
await (trpcClient.envVarsContexts as any).updateEnvVars.mutate({
351414
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: [],
415+
add: addEnvVars,
416+
update: updateEnvVars,
360417
remove: [],
361418
});
362419

0 commit comments

Comments
 (0)