feat: create script to generate Env type

This commit is contained in:
2024-06-03 08:29:00 -04:00
parent f56f844a88
commit 9c1559bce5
4 changed files with 36 additions and 2 deletions

32
src/generateEnv.ts Normal file
View File

@@ -0,0 +1,32 @@
import { $ } from "bun";
import { Project } from "ts-morph";
console.log('Re-generating "env.d.ts"');
console.time("Generated env.d.ts");
await $`bunx wrangler types src/types/env.d.ts`.quiet();
console.timeEnd("Generated env.d.ts");
console.time("Fetched secrets");
console.log("Fetching secrets from Cloudflare");
const { stdout } = await $`bunx wrangler secret list`.quiet();
const secretNames: string[] = JSON.parse(stdout.toString()).map(
(secret: { name: string; type: "secret_text" }) => secret.name,
);
console.timeEnd("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();

5
src/types/env.d.ts vendored
View File

@@ -1,9 +1,10 @@
// Generated by Wrangler on Mon Jun 03 2024 07:38:32 GMT-0400 (Eastern Daylight Time)
// Generated by Wrangler on Mon Jun 03 2024 08:28:23 GMT-0400 (Eastern Daylight Time)
// by running `wrangler types src/types/env.d.ts`
import type { Env as HonoEnv } from "hono";
interface Env extends HonoEnv {
interface Env {
ENABLE_ANIFY: string;
TURSO_URL: string;
QSTASH_URL: string;
TURSO_AUTH_TOKEN: string;
}