Skip to content

Commit baafe70

Browse files
committed
chore: rewrite file watcher
1 parent 9b2174a commit baafe70

File tree

2 files changed

+34
-72
lines changed

2 files changed

+34
-72
lines changed

src/index.ts

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ import {
55
generate,
66
loadContext,
77
} from '@graphql-codegen/cli';
8-
import {
9-
type FileMatcher,
10-
isCodegenConfig,
11-
isGraphQLDocument,
12-
isGraphQLSchema,
13-
} from './utils/fileMatchers';
8+
import { isCodegenConfig, isFileMatched } from './utils/fileMatchers';
149
import { isBuildMode, isServeMode, type ViteMode } from './utils/viteModes';
1510
import { debugLog } from './utils/debugLog';
11+
import { getDocumentPaths, getSchemaPaths } from './utils/configPaths';
1612

1713
export interface Options {
1814
/**
@@ -178,56 +174,41 @@ export function GraphQLCodegen(options?: Options): Plugin {
178174
}
179175
}
180176
},
181-
configureServer(server) {
177+
async configureServer(server) {
182178
if (!enableWatcher) return;
183179

184-
const listener = async (filePath = '') => {
185-
log('File changed:', filePath);
180+
const documentPaths = await getDocumentPaths(codegenContext);
181+
const schemaPaths = await getSchemaPaths(codegenContext);
186182

187-
const isConfig = await isCodegenConfig(filePath, codegenContext);
183+
const isMatch = (filePath: string) => {
184+
let matched = false;
185+
if (matchOnDocuments) {
186+
if (isFileMatched(filePath, documentPaths)) {
187+
matched = true;
188+
log(`Graphql document file matched: ${filePath}`);
189+
}
190+
}
191+
192+
if (matchOnSchemas) {
193+
if (isFileMatched(filePath, schemaPaths)) {
194+
matched = true;
195+
log(`Graphql schema file matched: ${filePath}`);
196+
}
197+
}
198+
199+
return matched;
200+
};
201+
202+
const listener = async (filePath = '') => {
203+
const isConfig = isCodegenConfig(filePath, codegenContext.filepath);
188204

189205
if (isConfig) {
190206
log('Codegen config file changed, restarting vite');
191-
server.restart();
207+
await server.restart();
192208
return;
193209
}
194210

195-
const matchers: [
196-
enabled: boolean,
197-
name: string,
198-
matcher: FileMatcher,
199-
][] = [
200-
[matchOnDocuments, 'document', isGraphQLDocument],
201-
[matchOnSchemas, 'schema', isGraphQLSchema],
202-
];
203-
204-
const matcherResults = await Promise.all(
205-
matchers.map(async ([enabled, name, matcher]) => {
206-
if (!enabled) {
207-
log(`Check for ${name} file skipped in file watcher by config`);
208-
return false;
209-
}
210-
211-
try {
212-
const isMatch = await matcher(filePath, codegenContext);
213-
214-
log(`Check for ${name} file successful in file watcher`);
215-
216-
if (isMatch) log(`File matched a graphql ${name}`);
217-
else log(`File did not match a graphql ${name}`);
218-
219-
return isMatch;
220-
} catch (error) {
221-
// GraphQL Codegen handles logging useful errors
222-
log(`Check for ${name} file failed in file watcher`);
223-
return false;
224-
}
225-
}),
226-
);
227-
228-
const isMatch = matcherResults.some((result) => result);
229-
230-
if (!isMatch) return;
211+
if (!isMatch(filePath)) return;
231212

232213
try {
233214
await generateWithOverride(configOverrideWatcher);

src/utils/fileMatchers.ts

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
11
import { normalizePath } from 'vite';
2-
import type { CodegenContext } from '@graphql-codegen/cli';
3-
import { getDocumentPaths, getSchemaPaths } from '../utils/configPaths';
42

5-
export type FileMatcher = (
6-
filePath: string,
7-
context: CodegenContext,
8-
) => Promise<boolean>;
3+
export const isCodegenConfig = (filePath: string, configPath?: string) => {
4+
if (!configPath) return false;
95

10-
export const isCodegenConfig: FileMatcher = async (filePath, context) => {
11-
if (!context.filepath) return false;
12-
13-
return normalizePath(filePath) === normalizePath(context.filepath);
14-
};
15-
16-
export const isGraphQLDocument: FileMatcher = async (filePath, context) => {
17-
const documentPaths = await getDocumentPaths(context);
18-
19-
if (!documentPaths.length) return false;
20-
21-
const normalizedFilePath = normalizePath(filePath);
22-
23-
return documentPaths.some((path) => normalizedFilePath.includes(path));
6+
return normalizePath(filePath) === normalizePath(configPath);
247
};
258

26-
export const isGraphQLSchema: FileMatcher = async (filePath, context) => {
27-
const schemaPaths = await getSchemaPaths(context);
28-
29-
if (!schemaPaths.length) return false;
9+
export const isFileMatched = (filePath: string, lookupPaths: string[]) => {
10+
if (!lookupPaths.length) return false;
3011

3112
const normalizedFilePath = normalizePath(filePath);
3213

33-
return schemaPaths.some((path) => normalizedFilePath.includes(path));
14+
return lookupPaths.includes(normalizedFilePath);
3415
};

0 commit comments

Comments
 (0)