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

23
src/libs/logStep.ts Normal file
View File

@@ -0,0 +1,23 @@
export async function logStep<T = void>(
inProgressText: string,
step: () => Promise<T> | T,
): Promise<T>;
export async function logStep<T = void>(
inProgressText: string,
step: () => Promise<T> | T,
doneText: string,
): Promise<T>;
export async function logStep<T = void>(
inProgressText: string,
step: () => Promise<T> | T,
doneText: string = `Completed step "${inProgressText}"`,
) {
console.time(doneText);
console.log(`${inProgressText}...`);
return Promise.resolve(step()).then((value) => {
console.timeEnd(doneText);
return value;
});
}

View File

@@ -1,19 +1,23 @@
import { $ } from "bun"; import { $ } from "bun";
import { Project } from "ts-morph"; import { Project } from "ts-morph";
await logStep('Re-generating "env.d.ts"', "Generated env.d.ts", () => import { logStep } from "~/libs/logStep";
$`bunx wrangler types src/types/env.d.ts`.quiet(),
await logStep(
'Re-generating "env.d.ts"',
() => $`bunx wrangler types src/types/env.d.ts`.quiet(),
"Generated env.d.ts",
); );
const secretNames = await logStep( const secretNames = await logStep(
"Fetching secrets from Cloudflare", "Fetching secrets from Cloudflare",
"Fetched secrets",
async (): Promise<string[]> => { async (): Promise<string[]> => {
const { stdout } = await $`bunx wrangler secret list`.quiet(); const { stdout } = await $`bunx wrangler secret list`.quiet();
return JSON.parse(stdout.toString()).map( return JSON.parse(stdout.toString()).map(
(secret: { name: string; type: "secret_text" }) => secret.name, (secret: { name: string; type: "secret_text" }) => secret.name,
); );
}, },
"Fetched secrets",
); );
const project = new Project({}); const project = new Project({});
@@ -33,20 +37,8 @@ envFile.getInterfaceOrThrow("Env").addProperties(
await project.save(); await project.save();
await logStep("Formatting env.d.ts", "Formatted env.d.ts", () => await logStep(
$`bunx prettier --write src/types/env.d.ts`.quiet(), "Formatting env.d.ts",
() => $`bunx prettier --write src/types/env.d.ts`.quiet(),
"Formatted env.d.ts",
); );
async function logStep<T = void>(
inProgressText: string,
doneText: string,
step: () => Promise<T> | T,
) {
console.time(doneText);
console.log(`${inProgressText}...`);
return Promise.resolve(step()).then((value) => {
console.timeEnd(doneText);
return value;
});
}