diff --git a/packages/cli/src/commands/codegen.ts b/packages/cli/src/commands/codegen.ts index ab1610a73..9c04da60d 100644 --- a/packages/cli/src/commands/codegen.ts +++ b/packages/cli/src/commands/codegen.ts @@ -1,7 +1,7 @@ import { gqltadaSpinner } from "@/commands/codegen/gqltada.spinner"; import { Command } from "@commander-js/extra-typings"; import { loadEnv } from "@settlemint/sdk-utils/environment"; -import { intro, outro } from "@settlemint/sdk-utils/terminal"; +import { cancel, intro, outro } from "@settlemint/sdk-utils/terminal"; import type { DotEnv } from "@settlemint/sdk-utils/validation"; import { bold, italic, underline } from "yoctocolors"; @@ -28,7 +28,11 @@ export function codegenCommand(): Command { const env: DotEnv = await loadEnv(); - await gqltadaSpinner(env); + try { + await gqltadaSpinner(env); + } catch (error) { + cancel((error as Error).message); + } outro("Codegen complete"); }) diff --git a/packages/cli/src/commands/codegen/gqltada.spinner.ts b/packages/cli/src/commands/codegen/gqltada.spinner.ts index 63b0df5c3..8269e51a9 100644 --- a/packages/cli/src/commands/codegen/gqltada.spinner.ts +++ b/packages/cli/src/commands/codegen/gqltada.spinner.ts @@ -1,4 +1,7 @@ -import { generateSchema } from "@gql.tada/cli-utils"; +import { readFileSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { generateSchema, generateTurbo } from "@gql.tada/cli-utils"; +import { projectRoot } from "@settlemint/sdk-utils/filesystem"; import type { DotEnv } from "@settlemint/sdk-utils/validation"; /** @@ -16,6 +19,79 @@ import type { DotEnv } from "@settlemint/sdk-utils/validation"; * ); */ export async function gqltadaSpinner(env: DotEnv) { + const tadaConfig = { + name: "@0no-co/graphqlsp", + schemas: [ + { + name: "hasura", + schema: "hasura-schema.graphql", + tadaOutputLocation: "hasura-env.d.ts", + tadaTurboLocation: "hasura-cache.d.ts", + }, + { + name: "thegraph", + schema: "thegraph-schema.graphql", + tadaOutputLocation: "thegraph-env.d.ts", + tadaTurboLocation: "thegraph-cache.d.ts", + }, + { + name: "thegraph-fallback", + schema: "thegraph-fallback-schema.graphql", + tadaOutputLocation: "thegraph-fallback-env.d.ts", + tadaTurboLocation: "thegraph-fallback-cache.d.ts", + }, + { + name: "portal", + schema: "portal-schema.graphql", + tadaOutputLocation: "portal-env.d.ts", + tadaTurboLocation: "portal-cache.d.ts", + }, + ], + }; + + const projectDir = await projectRoot(); + const tsconfigFile = join(projectDir, "tsconfig.json"); + const tsconfigContent = readFileSync(tsconfigFile, "utf8"); + const tsconfig: { + compilerOptions: { + plugins?: { + name: string; + schemas: { + name: string; + schema: string; + tadaOutputLocation: string; + tadaTurboLocation: string; + }[]; + }[]; + }; + } = JSON.parse(tsconfigContent); + // Find the existing @0no-co/graphqlsp plugin or create a new one + if (!tsconfig.compilerOptions.plugins) { + tsconfig.compilerOptions.plugins = []; + } + let graphqlspPlugin = (tsconfig.compilerOptions.plugins ?? []).find((plugin) => plugin.name === "@0no-co/graphqlsp"); + + if (!graphqlspPlugin) { + // If the plugin doesn't exist, create it and add it to the plugins array + graphqlspPlugin = { name: "@0no-co/graphqlsp", schemas: [] }; + tsconfig.compilerOptions.plugins.push(graphqlspPlugin); + } + + // Update or add the schemas defined in tadaConfig + for (const schema of tadaConfig.schemas) { + const existingSchemaIndex = graphqlspPlugin.schemas.findIndex((s) => s.name === schema.name); + if (existingSchemaIndex !== -1) { + // Update existing schema + graphqlspPlugin.schemas[existingSchemaIndex] = schema; + } else { + // Add new schema + graphqlspPlugin.schemas.push(schema); + } + } + + // Write the updated tsconfig back to the file + writeFileSync(tsconfigFile, JSON.stringify(tsconfig, null, 2), "utf8"); + await gqltadaCodegen({ type: "HASURA", env, @@ -42,26 +118,32 @@ async function gqltadaCodegen(options: { }) { let gqlEndpoint: string | undefined = undefined; let output: string; + let turboOutput: string; let adminSecret: string | undefined = undefined; const accessToken = options.env.SETTLEMINT_ACCESS_TOKEN; switch (options.type) { case "HASURA": gqlEndpoint = options.env.SETTLEMINT_HASURA_ENDPOINT; - output = "hasura.schema.graphql"; + output = "hasura-schema.graphql"; + turboOutput = "hasura-cache.d.ts"; adminSecret = options.env.SETTLEMINT_HASURA_ADMIN_SECRET; break; case "PORTAL": gqlEndpoint = options.env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT; - output = "portal.schema.graphql"; + output = "portal-schema.graphql"; + turboOutput = "portal-cache.d.ts"; break; case "THEGRAPH": gqlEndpoint = options.env.SETTLEMINT_THEGRAPH_SUBGRAPH_ENDPOINT; - output = "thegraph.schema.graphql"; + output = "thegraph-schema.graphql"; + turboOutput = "thegraph-cache.d.ts"; break; case "THEGRAPH_FALLBACK": gqlEndpoint = options.env.SETTLEMINT_THEGRAPH_SUBGRAPH_ENDPOINT_FALLBACK; - output = "thegraph-fallback.schema.graphql"; + output = "thegraph-fallback-schema.graphql"; + turboOutput = "thegraph-fallback-cache.d.ts"; + break; } if (!gqlEndpoint) { @@ -107,6 +189,12 @@ async function gqltadaCodegen(options: { tsconfig: undefined, headers, }); + + await generateTurbo({ + failOnWarn: false, + output: turboOutput, + tsconfig: undefined, + }); } catch (error) { if (options.allowToFail) { // ignore