From 38d58134e77181fd17f124150821d9ef8e61f1e7 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Wed, 21 Feb 2024 10:35:53 +0100 Subject: [PATCH 1/2] add warnings --- packages/cli-utils/src/index.ts | 24 +++++++++++++----------- packages/cli-utils/src/tada.ts | 18 +++++++----------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/packages/cli-utils/src/index.ts b/packages/cli-utils/src/index.ts index f1203909..9c01b5e3 100644 --- a/packages/cli-utils/src/index.ts +++ b/packages/cli-utils/src/index.ts @@ -7,7 +7,7 @@ import { parse } from 'comment-json'; import type { TsConfigJson } from 'type-fest'; import { ensureTadaIntrospection } from './tada'; -const prog = sade('fuse'); +const prog = sade('gql.tada'); prog.version(process.env.npm_package_version || '0.0.0'); @@ -19,12 +19,12 @@ type GraphQLSPConfig = { function hasGraphQLSP(tsconfig: TsConfigJson): boolean { if (!tsconfig.compilerOptions) { - // Warn + console.warn('Missing compilerOptions object in tsconfig.json.'); return false; } if (!tsconfig.compilerOptions.plugins) { - // Warn + console.warn('Missing plugins array in tsconfig.json.'); return false; } @@ -32,17 +32,19 @@ function hasGraphQLSP(tsconfig: TsConfigJson): boolean { (plugin) => plugin.name === '@0no-co/graphqlsp' ) as GraphQLSPConfig | undefined; if (!foundPlugin) { - // Warn + console.warn('Missing @0no-co/graphqlsp plugin in tsconfig.json.'); return false; } if (!foundPlugin.schema) { - // Warn + console.warn('Missing schema property in @0no-co/graphqlsp plugin in tsconfig.json.'); return false; } if (!foundPlugin.tadaOutputLocation) { - // Warn + console.warn( + 'Missing tadaOutputLocation property in @0no-co/graphqlsp plugin in tsconfig.json.' + ); return false; } @@ -60,7 +62,8 @@ async function main() { const tsconfigpath = path.resolve(cwd, 'tsconfig.json'); const hasTsConfig = existsSync(tsconfigpath); if (!hasTsConfig) { - // Error + console.error('Missing tsconfig.json'); + return; } const tsconfigContents = await fs.readFile(tsconfigpath, 'utf-8'); @@ -68,20 +71,19 @@ async function main() { try { tsConfig = parse(tsconfigContents) as TsConfigJson; } catch (err) { - // report error and bail + console.error(err); return; } if (!hasGraphQLSP(tsConfig)) { - // Error + return; } const foundPlugin = tsConfig.compilerOptions!.plugins!.find( (plugin) => plugin.name === '@0no-co/graphqlsp' ) as GraphQLSPConfig; - await ensureTadaIntrospection(foundPlugin.schema, false); - // Generate the file + await ensureTadaIntrospection(foundPlugin.schema, foundPlugin.tadaOutputLocation!); }); } diff --git a/packages/cli-utils/src/tada.ts b/packages/cli-utils/src/tada.ts index 0ad63755..ccd0043a 100644 --- a/packages/cli-utils/src/tada.ts +++ b/packages/cli-utils/src/tada.ts @@ -1,4 +1,4 @@ -import { promises as fs, watch, existsSync } from 'node:fs'; +import { promises as fs, existsSync } from 'node:fs'; import path from 'node:path'; import { buildSchema, introspectionFromSchema } from 'graphql'; import { minifyIntrospectionQuery } from '@urql/introspection'; @@ -23,7 +23,7 @@ export { readFragment as useFragment } from 'gql.tada'; * we are not able to leverage the workspace TS version we will rely on * this function. */ -export async function ensureTadaIntrospection(location: string, shouldWatch: boolean) { +export async function ensureTadaIntrospection(location: string, outputLocation: string) { const schemaLocation = path.resolve(location, 'schema.graphql'); const writeTada = async () => { @@ -44,7 +44,7 @@ export async function ensureTadaIntrospection(location: string, shouldWatch: boo const hasSrcDir = existsSync(path.resolve(location, 'src')); const base = hasSrcDir ? path.resolve(location, 'src') : location; - const outputLocation = path.resolve(base, 'fuse', 'introspection.ts'); + const resolvedOutputLocation = path.resolve(base, outputLocation, 'introspection.ts'); const contents = [ preambleComments, tsAnnotationComment, @@ -52,17 +52,13 @@ export async function ensureTadaIntrospection(location: string, shouldWatch: boo 'export { introspection };', ].join('\n'); - await fs.writeFile(outputLocation, contents); - } catch (e) {} + await fs.writeFile(resolvedOutputLocation, contents); + } catch (e) { + console.error('Something went wrong while writing the introspection file', e); + } }; await writeTada(); - - if (shouldWatch) { - watch(schemaLocation, async () => { - await writeTada(); - }); - } } const preambleComments = ['/* eslint-disable */', '/* prettier-ignore */'].join('\n') + '\n'; From 6f35f3b787ebbcb1bc0dbd1fa7c869f6c027cb8f Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Wed, 21 Feb 2024 10:40:04 +0100 Subject: [PATCH 2/2] read schema correctly --- packages/cli-utils/src/tada.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/cli-utils/src/tada.ts b/packages/cli-utils/src/tada.ts index ccd0043a..d4fc390b 100644 --- a/packages/cli-utils/src/tada.ts +++ b/packages/cli-utils/src/tada.ts @@ -1,4 +1,4 @@ -import { promises as fs, existsSync } from 'node:fs'; +import { promises as fs } from 'node:fs'; import path from 'node:path'; import { buildSchema, introspectionFromSchema } from 'graphql'; import { minifyIntrospectionQuery } from '@urql/introspection'; @@ -23,9 +23,8 @@ export { readFragment as useFragment } from 'gql.tada'; * we are not able to leverage the workspace TS version we will rely on * this function. */ -export async function ensureTadaIntrospection(location: string, outputLocation: string) { - const schemaLocation = path.resolve(location, 'schema.graphql'); - +export async function ensureTadaIntrospection(schemaLocation: string, outputLocation: string) { + const base = process.cwd(); const writeTada = async () => { try { const content = await fs.readFile(schemaLocation, 'utf-8'); @@ -41,10 +40,7 @@ export async function ensureTadaIntrospection(location: string, outputLocation: }); const json = JSON.stringify(minified, null, 2); - const hasSrcDir = existsSync(path.resolve(location, 'src')); - const base = hasSrcDir ? path.resolve(location, 'src') : location; - - const resolvedOutputLocation = path.resolve(base, outputLocation, 'introspection.ts'); + const resolvedOutputLocation = path.resolve(base, outputLocation); const contents = [ preambleComments, tsAnnotationComment,