Skip to content

Commit ad4e366

Browse files
committed
Enable type generation in internal repo
1 parent 60115fd commit ad4e366

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

types/scripts/build-types.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import path from "node:path";
77
import prettier from "prettier";
88
import ts from "typescript";
99
import { SourcesMap, createMemoryProgram } from "../src/program.js";
10+
import { getFilePath } from "../src/utils";
1011

11-
const OUTPUT_PATH = "types/definitions";
12+
const OUTPUT_PATH = getFilePath("types/definitions");
1213
const ENTRYPOINTS = [
1314
{ compatDate: "2021-01-01", name: "oldest" },
1415
// https://developers.cloudflare.com/workers/platform/compatibility-dates/#formdata-parsing-supports-file
@@ -99,7 +100,7 @@ function spawnWorkerd(
99100
): Promise<{ url: URL; kill: () => Promise<void> }> {
100101
return new Promise((resolve) => {
101102
const workerdProcess = childProcess.spawn(
102-
"./src/workerd/server/workerd",
103+
getFilePath("src/workerd/server/workerd"),
103104
["serve", "--verbose", "--experimental", "--control-fd=3", configPath],
104105
{ stdio: ["inherit", "inherit", "inherit", "pipe"] }
105106
);
@@ -150,7 +151,7 @@ async function buildAllEntrypoints(workerUrl: URL) {
150151
await buildEntrypoint(entrypoint, workerUrl);
151152
}
152153
export async function main() {
153-
const worker = await spawnWorkerd("./types/scripts/config.capnp");
154+
const worker = await spawnWorkerd(getFilePath("types/scripts/config.capnp"));
154155
try {
155156
await buildAllEntrypoints(worker.url);
156157
} finally {

types/scripts/build-worker.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { build } from "esbuild";
55
import { CommentsData } from "src/transforms";
66
import cloudflareComments from "../src/cloudflare";
77
import { collateStandardComments } from "../src/standards";
8+
import { getFilePath } from "../src/utils";
89

910
async function readPath(rootPath: string): Promise<string> {
1011
try {
@@ -29,7 +30,7 @@ async function readParamNames() {
2930
["DurableObjectStorageOperations", "DurableObjectTransaction"],
3031
];
3132

32-
const data = await fs.readFile("src/workerd/tools/param-names.json", "utf8");
33+
const data = await fs.readFile(getFilePath("src/workerd/tools/param-names.json"), "utf8");
3334
const recordArray = JSON.parse(data) as {
3435
fully_qualified_parent_name: string[];
3536
function_like_name: string;
@@ -103,9 +104,9 @@ if (require.main === module)
103104
external: ["node:*", "workerd:*"],
104105
bundle: true,
105106
minify: true,
106-
outdir: "types/dist",
107+
outdir: getFilePath("types/dist"),
107108
outExtension: { ".js": ".mjs" },
108-
entryPoints: ["types/src/worker/index.ts"],
109+
entryPoints: [getFilePath("types/src/worker/index.ts")],
109110
plugins: [
110111
{
111112
name: "raw",

types/src/utils.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { existsSync } from "node:fs";
2+
import path from "node:path";
3+
4+
// When building types from the upstream repo all paths need to be prepended by external/workerd/
5+
export function getFilePath(f: string) {
6+
if (existsSync("external/workerd")) {
7+
return path.join("external", "workerd", f);
8+
} else {
9+
return f;
10+
}
11+
}

0 commit comments

Comments
 (0)