refactor: move generateEnv script inside folder

This commit is contained in:
2024-06-03 09:15:02 -04:00
parent f2b9d10a16
commit 5e0de6387d
2 changed files with 34 additions and 19 deletions

View File

@@ -0,0 +1,44 @@
import { $ } from "bun";
import { Project } from "ts-morph";
import { logStep } from "~/libs/logStep";
await logStep(
'Re-generating "env.d.ts"',
() => $`bunx wrangler types src/types/env.d.ts`.quiet(),
"Generated env.d.ts",
);
const secretNames = await logStep(
"Fetching secrets from Cloudflare",
async (): Promise<string[]> => {
const { stdout } = await $`bunx wrangler secret list`.quiet();
return JSON.parse(stdout.toString()).map(
(secret: { name: string; type: "secret_text" }) => secret.name,
);
},
"Fetched secrets",
);
const project = new Project({});
const envFile = project.addSourceFileAtPath("src/types/env.d.ts");
envFile.insertImportDeclaration(2, {
isTypeOnly: true,
moduleSpecifier: "hono",
namedImports: ["Env as HonoEnv"],
});
envFile.getInterfaceOrThrow("Env").addProperties(
secretNames.map((name) => ({
name,
type: `string`,
})),
);
await project.save();
await logStep(
"Formatting env.d.ts",
() => $`bunx prettier --write src/types/env.d.ts`.quiet(),
"Formatted env.d.ts",
);