Skip to content

Commit

Permalink
Merge pull request #3220 from cloudflare/dlapid/type_generation
Browse files Browse the repository at this point in the history
Enable type generation in internal repo
  • Loading branch information
danlapid authored Dec 8, 2024
2 parents f979ca7 + ad4e366 commit c876e52
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
7 changes: 4 additions & 3 deletions types/scripts/build-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -99,7 +100,7 @@ function spawnWorkerd(
): Promise<{ url: URL; kill: () => Promise<void> }> {
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"] }
);
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions types/scripts/build-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
try {
Expand All @@ -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;
Expand Down Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions types/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit c876e52

Please sign in to comment.