From 2dc76b6b86e06399c8371e74e4bb66d87d329385 Mon Sep 17 00:00:00 2001 From: Andy Jessop Date: Mon, 30 Sep 2024 11:09:14 +0200 Subject: [PATCH] feat: add programmatic API for types --- packages/wrangler/src/api/types/index.ts | 71 ++++++++++++++++++++++++ packages/wrangler/src/cli.ts | 1 + pnpm-lock.yaml | 6 ++ 3 files changed, 78 insertions(+) create mode 100644 packages/wrangler/src/api/types/index.ts diff --git a/packages/wrangler/src/api/types/index.ts b/packages/wrangler/src/api/types/index.ts new file mode 100644 index 000000000000..58ddbad1fba7 --- /dev/null +++ b/packages/wrangler/src/api/types/index.ts @@ -0,0 +1,71 @@ +import { writeFile } from "fs/promises"; +import path from "path"; +import { readConfig } from "../../config"; +import type { Config } from "../../config"; + +export async function generateProjectTypes(options: { + config?: Config; + configFile?: string; + outFile?: string; + persistToFilesystem?: boolean; +}): Promise { + return generateTypes(getProjectTypes, options); +} + +export async function generateRuntimeTypes(options: { + config?: Config; + configFile?: string; + outFile?: string; + persistToFilesystem?: boolean; +}): Promise { + return generateTypes( + async (config) => + getRuntimeTypes({ + compatibilityDate: config.compatibility_date, + compatibilityFlags: config.compatibility_flags.filter( + (flag) => !flag.includes("nodejs_compat") + ), + }), + options + ); +} + +async function generateTypes( + getTypesFunction: (config: Config) => Promise, + { + config, + configFile, + outFile, + persistToFilesystem = false, + }: { + config?: Config; + configFile?: string; + outFile?: string; + persistToFilesystem?: boolean; + } +): Promise { + if (!config && !configFile) { + throw new Error("Either config or configFile must be provided"); + } + if (config && configFile) { + throw new Error("Only one of config or configFile should be provided"); + } + if (outFile && persistToFilesystem === undefined) { + throw new Error( + "persistToFilesystem must be specified when outFile is provided" + ); + } + + const resolvedConfig = config ?? readConfig(configFile, {}); + + const types = await getTypesFunction(resolvedConfig); + + if (persistToFilesystem) { + const resolvedOutFile = + outFile ?? path.join(process.cwd(), "runtime-configuration.d.ts"); + + await writeFile(resolvedOutFile, types, "utf8"); + } + + return types; +} diff --git a/packages/wrangler/src/cli.ts b/packages/wrangler/src/cli.ts index eea6469827db..880793f449aa 100644 --- a/packages/wrangler/src/cli.ts +++ b/packages/wrangler/src/cli.ts @@ -35,6 +35,7 @@ export { unstable_dev, unstable_pages, unstable_DevEnv, unstable_startWorker }; export type { UnstableDevWorker, UnstableDevOptions }; export * from "./api/integrations"; +export * from "./api/types"; // Export internal APIs required by the Vitest integration as `unstable_` export { default as unstable_splitSqlQuery } from "./d1/splitter"; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3ef18db71228..c13c98ea5834 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6,6 +6,12 @@ settings: catalogs: default: + '@vitest/runner': + specifier: ~2.1.1 + version: 2.1.1 + '@vitest/snapshot': + specifier: ~2.1.1 + version: 2.1.1 vitest: specifier: ~2.1.1 version: 2.1.1