Skip to content

Commit cd2a552

Browse files
committed
refactor: Move matchers to separate modules
1 parent 7e6b862 commit cd2a552

5 files changed

Lines changed: 84 additions & 37 deletions

File tree

src/index.ts

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
import { Plugin, ResolvedConfig } from 'vite';
22
import { CodegenContext, generate, loadContext } from '@graphql-codegen/cli';
3-
import {
4-
normalizeInstanceOrArray,
5-
Types,
6-
} from '@graphql-codegen/plugin-helpers';
7-
import minimatch from 'minimatch';
3+
import { createMatcher, isDocumentMatch, isGeneratedMatch } from './matchers';
84

95
export default function VitePluginGraphQLCodegen(): Plugin {
106
let viteConfig: ResolvedConfig;
117
let codegenContext: CodegenContext;
128

13-
const getRelativePath = (path: string) => {
14-
return path.split(`${viteConfig.root}/`).slice(-1)[0];
15-
};
16-
179
return {
1810
name: 'graphql-codegen',
1911
async config(config) {
@@ -33,36 +25,14 @@ export default function VitePluginGraphQLCodegen(): Plugin {
3325
}
3426
},
3527
configureServer(server) {
36-
const listener = async (path = '') => {
37-
const relativePath = getRelativePath(path);
38-
const match = (pattern: string) => minimatch(relativePath, pattern);
28+
const listener = async (absolutePath = '') => {
29+
const matcher = createMatcher(absolutePath, viteConfig);
3930

40-
const codegenConfig = codegenContext.getConfig<Types.Config>();
41-
const { generates = {}, documents } = codegenConfig;
31+
const isGenerated = await isGeneratedMatch(matcher, codegenContext);
32+
if (isGenerated) return;
4233

43-
// If modified file is generated
44-
for (const [genPath, genConfig] of Object.entries(generates)) {
45-
// If generated path is file
46-
const fileExt = genPath.split('.').slice(-1)[0];
47-
const isFile = !!fileExt;
48-
if (isFile && match(genPath)) return;
49-
50-
// If generated path is directory
51-
const { preset = '', presetConfig } = genConfig;
52-
const isNearOperationFilePreset = preset === 'near-operation-file';
53-
const presetExt = presetConfig?.extension ?? '.generated.ts';
54-
if (isNearOperationFilePreset && match(`**/*${presetExt}`)) return;
55-
}
56-
57-
// If modified file is operation document
58-
if (!documents) return;
59-
const normalized = normalizeInstanceOrArray(documents);
60-
const documentFiles = codegenContext.loadDocuments(normalized);
61-
62-
for (const docFile of await documentFiles) {
63-
if (!docFile.location) break;
64-
if (!match(getRelativePath(docFile.location))) return;
65-
}
34+
const isDocument = await isDocumentMatch(matcher, codegenContext);
35+
if (!isDocument) return;
6636

6737
try {
6838
await generate(codegenContext);

src/matchers/createMatcher.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ResolvedConfig } from 'vite';
2+
import minimatch from 'minimatch';
3+
4+
export type Matcher = ReturnType<typeof createMatcher>;
5+
6+
export default function createMatcher(
7+
absolutePath: string,
8+
config: ResolvedConfig
9+
) {
10+
const getRelativePath = (path: string) => {
11+
return path.split(`${config.root}/`).slice(-1)[0];
12+
};
13+
14+
const relativePath = getRelativePath(absolutePath);
15+
16+
const match = (pattern: string) => {
17+
return minimatch(relativePath, pattern);
18+
};
19+
20+
return { match, getRelativePath } as const;
21+
}

src/matchers/documents.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { CodegenContext } from '@graphql-codegen/cli';
2+
import {
3+
normalizeInstanceOrArray,
4+
Types,
5+
} from '@graphql-codegen/plugin-helpers';
6+
import { Matcher } from './createMatcher';
7+
8+
export async function isDocumentMatch(
9+
matcher: Matcher,
10+
context: CodegenContext
11+
): Promise<boolean> {
12+
const config = context.getConfig<Types.Config>();
13+
14+
if (!config.documents) return false;
15+
16+
const normalized = normalizeInstanceOrArray(config.documents);
17+
const documents = await context.loadDocuments(normalized);
18+
19+
if (!documents) return false;
20+
21+
for (const document of documents) {
22+
if (!document.location) break;
23+
const relativeDocumentPath = matcher.getRelativePath(document.location);
24+
return matcher.match(relativeDocumentPath);
25+
}
26+
27+
return false;
28+
}

src/matchers/generated.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { CodegenContext } from '@graphql-codegen/cli';
2+
import { Types } from '@graphql-codegen/plugin-helpers';
3+
import { Matcher } from './createMatcher';
4+
5+
export async function isGeneratedMatch(
6+
matcher: Matcher,
7+
context: CodegenContext
8+
): Promise<boolean> {
9+
const config = context.getConfig<Types.Config>();
10+
11+
const generates = Object.entries(config.generates);
12+
13+
if (!generates.length) return false;
14+
15+
for (const [genPath, genConfig] of generates) {
16+
const { preset = '', presetConfig } = genConfig;
17+
const isNearOperationFilePreset = preset === 'near-operation-file';
18+
const presetExt = presetConfig?.extension ?? '.generated.ts';
19+
if (isNearOperationFilePreset) return matcher.match(`**/*${presetExt}`);
20+
21+
return matcher.match(genPath);
22+
}
23+
24+
return false;
25+
}

src/matchers/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { default as createMatcher } from './createMatcher';
2+
export * from './documents';
3+
export * from './generated';

0 commit comments

Comments
 (0)