-
Notifications
You must be signed in to change notification settings - Fork 1
/
codegen.ts
92 lines (88 loc) · 3.05 KB
/
codegen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Environment file parsing and updating
import * as DotEnv from 'dotenv'
import { expand } from 'dotenv-expand'
import path from 'node:path'
import fs from 'node:fs'
import figures from 'figures'
import chalk from 'chalk'
// Process environment files, to ensure the enviornment configuration is applied
const envFiles : string[] = [".env", ".env.local"]
if (process.env.NODE_ENV) {
envFiles.push(`.env.${ process.env.NODE_ENV }`)
envFiles.push(`.env.${ process.env.NODE_ENV }.local`)
}
envFiles.map(s => path.join(process.cwd(), s)).filter(s => fs.existsSync(s)).reverse().forEach(fileName => {
var result = DotEnv.config({ path: fileName, override: false })
expand(result)
console.log(`${ chalk.greenBright(figures.tick) } Processed ${fileName}`)
})
// Actual code generation setup
import { CodegenConfig } from '@graphql-codegen/cli'
import getSchemaInfo from '@remkoj/optimizely-graph-client/codegen'
import OptimizelyGraphPreset, { type PresetOptions as OptimizelyGraphPresetConfig } from '@remkoj/optimizely-graph-functions/preset'
const config: CodegenConfig = {
schema: getSchemaInfo(),
documents: [
'src/**/*.jsx',
'src/**/*.tsx',
'src/**/*.graphql'
],
ignoreNoDocuments: true,
generates: {
'./src/gql/': {
preset: OptimizelyGraphPreset,
presetConfig: {
gqlTagName: 'gql',
recursion: false,
injections: [
{
// Add from all pages, except colocated blocks
into: "PageData",
pathRegex: "src\/components\/page\/.+(?<!\.block)\.[tj]sx$"
},
{
// Add blocks colocated with pages
into: "BlockData",
pathRegex: "src\/components\/page\/.+\.block\.[tj]sx$"
},
{
// Add from all blocks
into: "BlockData",
pathRegex: "src\/components\/component\/block\/.+\.[tj]sx$"
},
{
// Add from all elements
into: "ElementData",
pathRegex: "src\/components\/component\/element\/.+\.[tj]sx$"
},
{
// Add all block fragments from GraphQL files
into: "BlockData",
pathRegex: "\.block\.graphql$"
},
{
// Add all component fragments from GraphQL files
into: "BlockData",
pathRegex: "\.component\.graphql$"
},
{
// Add all page fragments from GraphQL files
into: "PageData",
pathRegex: "\.page\.graphql$"
},
{
// Add all experience fragments from GraphQL files
into: "PageData",
pathRegex: "\.experience\.graphql$"
},
{
// Add all element fragments from GraphQL files
into: "ElementData",
pathRegex: "\.element\.graphql$"
}
]
} as OptimizelyGraphPresetConfig,
}
}
}
export default config