Skip to content

Commit

Permalink
feat: run schema and document checks at the same time
Browse files Browse the repository at this point in the history
resolves #21
  • Loading branch information
danielwaltz committed Aug 19, 2023
1 parent 695a61d commit aa8c92c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
69 changes: 38 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
loadContext,
} from '@graphql-codegen/cli';
import {
type FileMatcher,
isCodegenConfig,
isGraphQLDocument,
isGraphQLSchema,
Expand Down Expand Up @@ -183,44 +184,50 @@ export default function VitePluginGraphQLCodegen(options?: Options): Plugin {
const listener = async (filePath = '') => {
log('File changed:', filePath);

const isConfig = isCodegenConfig(filePath, codegenContext);
const isConfig = await isCodegenConfig(filePath, codegenContext);

if (isConfig) {
log('Codegen config file changed, restarting vite');
server.restart();
return;
}

if (matchOnDocuments) {
try {
const isDocument = await isGraphQLDocument(
filePath,
codegenContext,
);
log('Document check successful in file watcher');

if (!isDocument && !matchOnSchemas) return;

log('File matched a graphql document');
} catch (error) {
// GraphQL Codegen handles logging useful errors
log('Document check failed in file watcher');
}
}

if (matchOnSchemas) {
try {
const isSchema = await isGraphQLSchema(filePath, codegenContext);
log('Schema check successful in file watcher');

if (!isSchema) return;

log('File matched a graphql schema');
} catch (error) {
// GraphQL Codegen handles logging useful errors
log('Schema check failed in file watcher');
}
}
const matchers: [
enabled: boolean,
name: string,
matcher: FileMatcher,
][] = [
[matchOnDocuments, 'document', isGraphQLDocument],
[matchOnSchemas, 'schema', isGraphQLSchema],
];

const matcherResults = await Promise.all(
matchers.map(async ([enabled, name, matcher]) => {
if (!enabled) {
log(`Check for ${name} file skipped in file watcher by config`);
return false;
}

try {
const isMatch = await matcher(filePath, codegenContext);

log(`Check for ${name} file successful in file watcher`);

if (isMatch) log(`File matched a graphql ${name}`);
else log(`File did not match a graphql ${name}`);

return isMatch;
} catch (error) {
// GraphQL Codegen handles logging useful errors
log(`Check for ${name} file failed in file watcher`);
return false;
}
}),
);

const isMatch = matcherResults.some((result) => result);

if (!isMatch) return;

try {
await generateWithOverride(configOverrideWatcher);
Expand Down
22 changes: 9 additions & 13 deletions src/utils/fileMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,29 @@ import { normalizePath } from 'vite';
import { type CodegenContext } from '@graphql-codegen/cli';
import { getDocumentPaths, getSchemaPaths } from '@/utils/configPaths';

export function isCodegenConfig(
export type FileMatcher = (
filePath: string,
context: CodegenContext,
): boolean {
) => Promise<boolean>;

export const isCodegenConfig: FileMatcher = async (filePath, context) => {
if (!context.filepath) return false;

return normalizePath(filePath) === normalizePath(context.filepath);
}
};

export async function isGraphQLDocument(
filePath: string,
context: CodegenContext,
): Promise<boolean> {
export const isGraphQLDocument: FileMatcher = async (filePath, context) => {
const documentPaths = await getDocumentPaths(context);

if (!documentPaths.length) return false;

return documentPaths.some((path) => path === normalizePath(filePath));
}
};

export async function isGraphQLSchema(
filePath: string,
context: CodegenContext,
): Promise<boolean> {
export const isGraphQLSchema: FileMatcher = async (filePath, context) => {
const schemaPaths = await getSchemaPaths(context);

if (!schemaPaths.length) return false;

return schemaPaths.some((path) => normalizePath(filePath).includes(path));
}
};

0 comments on commit aa8c92c

Please sign in to comment.