Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add warnings and write correctly #60

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions packages/cli-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -19,30 +19,32 @@ 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;
}

const foundPlugin = tsconfig.compilerOptions.plugins.find(
(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;
}

Expand All @@ -60,28 +62,28 @@ 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');
let tsConfig: TsConfigJson;
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!);
});
}

Expand Down
24 changes: 8 additions & 16 deletions packages/cli-utils/src/tada.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promises as fs, watch, 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';
Expand All @@ -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, shouldWatch: boolean) {
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');
Expand All @@ -41,28 +40,21 @@ export async function ensureTadaIntrospection(location: string, shouldWatch: boo
});

const json = JSON.stringify(minified, null, 2);
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);
const contents = [
preambleComments,
tsAnnotationComment,
`const introspection = ${json} as const;\n`,
'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';
Expand Down
Loading