Skip to content

Commit 4610be7

Browse files
authored
feat: manage the tsconfig file (#215)
## Summary by Sourcery Manage the tsconfig.json file by adding or updating the @0no-co/graphqlsp plugin with specified schemas, and enhance the gqltadaCodegen function to support turbo output generation for various schema types. New Features: - Introduce management of the tsconfig.json file by adding or updating the @0no-co/graphqlsp plugin with specified schemas. Enhancements: - Enhance the gqltadaCodegen function to include turbo output generation for different schema types.
1 parent 8245c11 commit 4610be7

File tree

2 files changed

+99
-7
lines changed

2 files changed

+99
-7
lines changed

packages/cli/src/commands/codegen.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { gqltadaSpinner } from "@/commands/codegen/gqltada.spinner";
22
import { Command } from "@commander-js/extra-typings";
33
import { loadEnv } from "@settlemint/sdk-utils/environment";
4-
import { intro, outro } from "@settlemint/sdk-utils/terminal";
4+
import { cancel, intro, outro } from "@settlemint/sdk-utils/terminal";
55
import type { DotEnv } from "@settlemint/sdk-utils/validation";
66
import { bold, italic, underline } from "yoctocolors";
77

@@ -28,7 +28,11 @@ export function codegenCommand(): Command {
2828

2929
const env: DotEnv = await loadEnv();
3030

31-
await gqltadaSpinner(env);
31+
try {
32+
await gqltadaSpinner(env);
33+
} catch (error) {
34+
cancel((error as Error).message);
35+
}
3236

3337
outro("Codegen complete");
3438
})

packages/cli/src/commands/codegen/gqltada.spinner.ts

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { generateSchema } from "@gql.tada/cli-utils";
1+
import { readFileSync, writeFileSync } from "node:fs";
2+
import { join } from "node:path";
3+
import { generateSchema, generateTurbo } from "@gql.tada/cli-utils";
4+
import { projectRoot } from "@settlemint/sdk-utils/filesystem";
25
import type { DotEnv } from "@settlemint/sdk-utils/validation";
36

47
/**
@@ -16,6 +19,79 @@ import type { DotEnv } from "@settlemint/sdk-utils/validation";
1619
* );
1720
*/
1821
export async function gqltadaSpinner(env: DotEnv) {
22+
const tadaConfig = {
23+
name: "@0no-co/graphqlsp",
24+
schemas: [
25+
{
26+
name: "hasura",
27+
schema: "hasura-schema.graphql",
28+
tadaOutputLocation: "hasura-env.d.ts",
29+
tadaTurboLocation: "hasura-cache.d.ts",
30+
},
31+
{
32+
name: "thegraph",
33+
schema: "thegraph-schema.graphql",
34+
tadaOutputLocation: "thegraph-env.d.ts",
35+
tadaTurboLocation: "thegraph-cache.d.ts",
36+
},
37+
{
38+
name: "thegraph-fallback",
39+
schema: "thegraph-fallback-schema.graphql",
40+
tadaOutputLocation: "thegraph-fallback-env.d.ts",
41+
tadaTurboLocation: "thegraph-fallback-cache.d.ts",
42+
},
43+
{
44+
name: "portal",
45+
schema: "portal-schema.graphql",
46+
tadaOutputLocation: "portal-env.d.ts",
47+
tadaTurboLocation: "portal-cache.d.ts",
48+
},
49+
],
50+
};
51+
52+
const projectDir = await projectRoot();
53+
const tsconfigFile = join(projectDir, "tsconfig.json");
54+
const tsconfigContent = readFileSync(tsconfigFile, "utf8");
55+
const tsconfig: {
56+
compilerOptions: {
57+
plugins?: {
58+
name: string;
59+
schemas: {
60+
name: string;
61+
schema: string;
62+
tadaOutputLocation: string;
63+
tadaTurboLocation: string;
64+
}[];
65+
}[];
66+
};
67+
} = JSON.parse(tsconfigContent);
68+
// Find the existing @0no-co/graphqlsp plugin or create a new one
69+
if (!tsconfig.compilerOptions.plugins) {
70+
tsconfig.compilerOptions.plugins = [];
71+
}
72+
let graphqlspPlugin = (tsconfig.compilerOptions.plugins ?? []).find((plugin) => plugin.name === "@0no-co/graphqlsp");
73+
74+
if (!graphqlspPlugin) {
75+
// If the plugin doesn't exist, create it and add it to the plugins array
76+
graphqlspPlugin = { name: "@0no-co/graphqlsp", schemas: [] };
77+
tsconfig.compilerOptions.plugins.push(graphqlspPlugin);
78+
}
79+
80+
// Update or add the schemas defined in tadaConfig
81+
for (const schema of tadaConfig.schemas) {
82+
const existingSchemaIndex = graphqlspPlugin.schemas.findIndex((s) => s.name === schema.name);
83+
if (existingSchemaIndex !== -1) {
84+
// Update existing schema
85+
graphqlspPlugin.schemas[existingSchemaIndex] = schema;
86+
} else {
87+
// Add new schema
88+
graphqlspPlugin.schemas.push(schema);
89+
}
90+
}
91+
92+
// Write the updated tsconfig back to the file
93+
writeFileSync(tsconfigFile, JSON.stringify(tsconfig, null, 2), "utf8");
94+
1995
await gqltadaCodegen({
2096
type: "HASURA",
2197
env,
@@ -42,26 +118,32 @@ async function gqltadaCodegen(options: {
42118
}) {
43119
let gqlEndpoint: string | undefined = undefined;
44120
let output: string;
121+
let turboOutput: string;
45122
let adminSecret: string | undefined = undefined;
46123
const accessToken = options.env.SETTLEMINT_ACCESS_TOKEN;
47124

48125
switch (options.type) {
49126
case "HASURA":
50127
gqlEndpoint = options.env.SETTLEMINT_HASURA_ENDPOINT;
51-
output = "hasura.schema.graphql";
128+
output = "hasura-schema.graphql";
129+
turboOutput = "hasura-cache.d.ts";
52130
adminSecret = options.env.SETTLEMINT_HASURA_ADMIN_SECRET;
53131
break;
54132
case "PORTAL":
55133
gqlEndpoint = options.env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT;
56-
output = "portal.schema.graphql";
134+
output = "portal-schema.graphql";
135+
turboOutput = "portal-cache.d.ts";
57136
break;
58137
case "THEGRAPH":
59138
gqlEndpoint = options.env.SETTLEMINT_THEGRAPH_SUBGRAPH_ENDPOINT;
60-
output = "thegraph.schema.graphql";
139+
output = "thegraph-schema.graphql";
140+
turboOutput = "thegraph-cache.d.ts";
61141
break;
62142
case "THEGRAPH_FALLBACK":
63143
gqlEndpoint = options.env.SETTLEMINT_THEGRAPH_SUBGRAPH_ENDPOINT_FALLBACK;
64-
output = "thegraph-fallback.schema.graphql";
144+
output = "thegraph-fallback-schema.graphql";
145+
turboOutput = "thegraph-fallback-cache.d.ts";
146+
break;
65147
}
66148

67149
if (!gqlEndpoint) {
@@ -107,6 +189,12 @@ async function gqltadaCodegen(options: {
107189
tsconfig: undefined,
108190
headers,
109191
});
192+
193+
await generateTurbo({
194+
failOnWarn: false,
195+
output: turboOutput,
196+
tsconfig: undefined,
197+
});
110198
} catch (error) {
111199
if (options.allowToFail) {
112200
// ignore

0 commit comments

Comments
 (0)