From ad4e366ee7ccd5e5900153ad20b79901de94c9d4 Mon Sep 17 00:00:00 2001 From: Dan Lapid Date: Sat, 7 Dec 2024 00:16:22 +0000 Subject: [PATCH] Enable type generation in internal repo --- types/scripts/build-types.ts | 7 ++++--- types/scripts/build-worker.ts | 7 ++++--- types/src/utils.ts | 11 +++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 types/src/utils.ts diff --git a/types/scripts/build-types.ts b/types/scripts/build-types.ts index db747aba2fe..0741d83978f 100644 --- a/types/scripts/build-types.ts +++ b/types/scripts/build-types.ts @@ -7,8 +7,9 @@ import path from "node:path"; import prettier from "prettier"; import ts from "typescript"; import { SourcesMap, createMemoryProgram } from "../src/program.js"; +import { getFilePath } from "../src/utils"; -const OUTPUT_PATH = "types/definitions"; +const OUTPUT_PATH = getFilePath("types/definitions"); const ENTRYPOINTS = [ { compatDate: "2021-01-01", name: "oldest" }, // https://developers.cloudflare.com/workers/platform/compatibility-dates/#formdata-parsing-supports-file @@ -99,7 +100,7 @@ function spawnWorkerd( ): Promise<{ url: URL; kill: () => Promise }> { return new Promise((resolve) => { const workerdProcess = childProcess.spawn( - "./src/workerd/server/workerd", + getFilePath("src/workerd/server/workerd"), ["serve", "--verbose", "--experimental", "--control-fd=3", configPath], { stdio: ["inherit", "inherit", "inherit", "pipe"] } ); @@ -150,7 +151,7 @@ async function buildAllEntrypoints(workerUrl: URL) { await buildEntrypoint(entrypoint, workerUrl); } export async function main() { - const worker = await spawnWorkerd("./types/scripts/config.capnp"); + const worker = await spawnWorkerd(getFilePath("types/scripts/config.capnp")); try { await buildAllEntrypoints(worker.url); } finally { diff --git a/types/scripts/build-worker.ts b/types/scripts/build-worker.ts index 8ec8a9cd7e3..5f58b021060 100644 --- a/types/scripts/build-worker.ts +++ b/types/scripts/build-worker.ts @@ -5,6 +5,7 @@ import { build } from "esbuild"; import { CommentsData } from "src/transforms"; import cloudflareComments from "../src/cloudflare"; import { collateStandardComments } from "../src/standards"; +import { getFilePath } from "../src/utils"; async function readPath(rootPath: string): Promise { try { @@ -29,7 +30,7 @@ async function readParamNames() { ["DurableObjectStorageOperations", "DurableObjectTransaction"], ]; - const data = await fs.readFile("src/workerd/tools/param-names.json", "utf8"); + const data = await fs.readFile(getFilePath("src/workerd/tools/param-names.json"), "utf8"); const recordArray = JSON.parse(data) as { fully_qualified_parent_name: string[]; function_like_name: string; @@ -103,9 +104,9 @@ if (require.main === module) external: ["node:*", "workerd:*"], bundle: true, minify: true, - outdir: "types/dist", + outdir: getFilePath("types/dist"), outExtension: { ".js": ".mjs" }, - entryPoints: ["types/src/worker/index.ts"], + entryPoints: [getFilePath("types/src/worker/index.ts")], plugins: [ { name: "raw", diff --git a/types/src/utils.ts b/types/src/utils.ts new file mode 100644 index 00000000000..31af32899d1 --- /dev/null +++ b/types/src/utils.ts @@ -0,0 +1,11 @@ +import { existsSync } from "node:fs"; +import path from "node:path"; + +// When building types from the upstream repo all paths need to be prepended by external/workerd/ +export function getFilePath(f: string) { + if (existsSync("external/workerd")) { + return path.join("external", "workerd", f); + } else { + return f; + } +}