refactor: move generateEnv script inside folder
This commit is contained in:
44
src/scripts/generateEnv.ts
Normal file
44
src/scripts/generateEnv.ts
Normal 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",
|
||||
);
|
||||
Reference in New Issue
Block a user