From c2eb371c51e643a771fda9c4a1cb79e2ba181872 Mon Sep 17 00:00:00 2001 From: typedarray <90073088+0xOlias@users.noreply.github.com> Date: Wed, 8 Nov 2023 19:28:05 -0500 Subject: [PATCH] update codegen --- packages/core/src/Ponder.ts | 5 +++ packages/core/src/codegen/event.ts | 44 ----------------------- packages/core/src/codegen/prettier.ts | 25 ------------- packages/core/src/codegen/service.test.ts | 33 ----------------- packages/core/src/codegen/service.ts | 33 +++++++++++++++++ packages/create-ponder/src/index.ts | 27 +++++++++++--- 6 files changed, 60 insertions(+), 107 deletions(-) delete mode 100644 packages/core/src/codegen/event.ts delete mode 100644 packages/core/src/codegen/prettier.ts delete mode 100644 packages/core/src/codegen/service.test.ts diff --git a/packages/core/src/Ponder.ts b/packages/core/src/Ponder.ts index 39fde7359..f88d25c7c 100644 --- a/packages/core/src/Ponder.ts +++ b/packages/core/src/Ponder.ts @@ -183,6 +183,9 @@ export class Ponder { // using the initial config, register service dependencies. this.registerServiceDependencies(); + // Regenerate the `ponder-env.d.ts` file in case it was deleted. + this.codegenService.generateDeclarationFile(); + // One-time setup for some services. await this.syncStore.migrateUp(); await this.serverService.start(); @@ -236,6 +239,8 @@ export class Ponder { } async codegen() { + this.codegenService.generateDeclarationFile(); + const result = await this.buildService.loadSchema(); if (result) { const { graphqlSchema } = result; diff --git a/packages/core/src/codegen/event.ts b/packages/core/src/codegen/event.ts deleted file mode 100644 index f4180eede..000000000 --- a/packages/core/src/codegen/event.ts +++ /dev/null @@ -1,44 +0,0 @@ -import type { LogEventMetadata } from "@/config/abi"; -import { Source } from "@/config/sources"; - -export const buildEventTypes = ({ sources }: { sources: Source[] }) => { - const allIndexingFunctions = sources.map((source) => - Object.values(source.events) - .filter((val): val is LogEventMetadata => !!val) - .map(({ safeName, abiItem }) => { - const paramsType = `{${abiItem.inputs - .map((input, index) => { - const inputName = input.name ? input.name : `param_${index}`; - return `${inputName}: - AbiParameterToPrimitiveType<${JSON.stringify(input)}>`; - }) - .join(";")}}`; - - return `["${source.name}:${safeName}"]: ({ - event, context - }: { - event: { - name: "${abiItem.name}"; - params: ${paramsType}; - log: Log; - block: Block; - transaction: Transaction; - }; - context: Context; - }) => Promise | any;`; - }) - .join("") - ); - - allIndexingFunctions.unshift( - `["setup"]: ({ context }: { context: Context; }) => Promise | any;` - ); - - const final = ` - export type AppType = { - ${allIndexingFunctions.join("")} - } - `; - - return final; -}; diff --git a/packages/core/src/codegen/prettier.ts b/packages/core/src/codegen/prettier.ts deleted file mode 100644 index 1f41b887f..000000000 --- a/packages/core/src/codegen/prettier.ts +++ /dev/null @@ -1,25 +0,0 @@ -import prettier from "prettier"; - -let prettierConfig: prettier.Options = { parser: "typescript" }; - -const loadPrettierConfig = async () => { - if (prettierConfig) return; - - const configFile = await prettier.resolveConfigFile(); - if (configFile) { - const foundConfig = await prettier.resolveConfig(configFile); - if (foundConfig) { - prettierConfig = foundConfig; - } - } -}; - -// Just call this once on process start -loadPrettierConfig(); - -export const formatPrettier = ( - source: string, - configOverrides?: Partial -) => { - return prettier.format(source, { ...prettierConfig, ...configOverrides }); -}; diff --git a/packages/core/src/codegen/service.test.ts b/packages/core/src/codegen/service.test.ts deleted file mode 100644 index 0556c82ab..000000000 --- a/packages/core/src/codegen/service.test.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { expect, test } from "vitest"; - -import { buildEntityTypes } from "@/codegen/entity"; -import * as p from "@/schema"; - -test("entity type codegen succeeds", () => { - const output = buildEntityTypes( - p.createSchema({ - name: p.createTable({ - id: p.bigint(), - age: p.int(), - }), - }) - ); - expect(output).toStrictEqual(`export type name = { - id: bigint;age: number; - };`); -}); - -test("enum type codegen succeeds", () => { - const output = buildEntityTypes( - p.createSchema({ - e: p.createEnum(["ONE", "TWO"]), - name: p.createTable({ - id: p.bigint(), - age: p.enum("e"), - }), - }) - ); - expect(output).toStrictEqual(`export type name = { - id: bigint;age: "ONE" | "TWO"; - };`); -}); diff --git a/packages/core/src/codegen/service.ts b/packages/core/src/codegen/service.ts index 2b1931023..5f00ba16e 100644 --- a/packages/core/src/codegen/service.ts +++ b/packages/core/src/codegen/service.ts @@ -14,6 +14,39 @@ export class CodegenService extends Emittery { this.common = common; } + generateDeclarationFile() { + const getImportPath = (file: string) => { + let relative = path.relative(this.common.options.rootDir, file); + + // If the file is in the same directory, prepend with "./" + if (!relative.startsWith("..") && !path.isAbsolute(relative)) + relative = `./${relative}`; + + return relative; + }; + + const configPath = getImportPath(this.common.options.configFile); + const schemaPath = getImportPath(this.common.options.schemaFile); + + const contents = `declare module "@/generated" { + import type { PonderApp } from "@ponder/core"; + + export const ponder: PonderApp< + typeof import("${configPath}").config, + typeof import("${schemaPath}").schema + >; +} +`; + + const filePath = path.join(this.common.options.rootDir, "ponder-env.d.ts"); + writeFileSync(filePath, contents, "utf8"); + + this.common.logger.debug({ + service: "codegen", + msg: `Generated ponder-env.d.ts`, + }); + } + generateGraphqlSchemaFile({ graphqlSchema, }: { diff --git a/packages/create-ponder/src/index.ts b/packages/create-ponder/src/index.ts index b1c02972a..885cb15dd 100644 --- a/packages/create-ponder/src/index.ts +++ b/packages/create-ponder/src/index.ts @@ -211,10 +211,7 @@ export const run = async ( "resolveJsonModule": true, "esModuleInterop": true, "strict": true, - "rootDir": ".", - "paths": { - "@/generated": ["./generated/index.ts"] - } + "rootDir": "." }, "include": ["./**/*.ts"], "exclude": ["node_modules"] @@ -241,7 +238,27 @@ export const run = async ( // Write the .gitignore file. writeFileSync( path.join(rootDir, ".gitignore"), - `node_modules/\n.DS_Store\n\n.env.local\n.ponder/\ngenerated/` + `# Dependencies +/node_modules + +# Debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* +.pnpm-debug.log* + +# Misc +.DS_Store + +# Env files +.env*.local + +# Ponder +/.next/ +/generated/ + +# TypeScript +ponder-env.d.ts` ); const packageManager = await getPackageManager();