-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodegen.ts
127 lines (115 loc) · 3.46 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { CodegenConfig, generate } from '@graphql-codegen/cli'
import { Octokit } from 'octokit'
import path from 'path'
export async function getSchemaPath(token?: string): Promise<string[]> {
// ex. $(pwd)/src/chainlink/core/web/schema
// or ../chainlink/core/web/schema
const repoPath = process.env.REPO_PATH
const defaultPath = '../chainlink'
if (repoPath && token) {
throw Error(
'Both $REPO_PATH and $GH_TOKEN were supplied, choose one method of fetching schemas only',
)
}
if (token) {
console.log('Running codegen based off of github files...')
const ref = process.env.REPO_REF
return await listSchemasOnGithub({ token, ref })
}
console.log('Running codegen based off of local files...')
if (repoPath) {
console.log(`User supplied repo path "${repoPath}" given.`)
} else {
console.warn(
`No user supplied repo path given. Defaulting to "${defaultPath}".`,
)
}
return [path.join(repoPath || defaultPath, 'core/web/schema')]
}
interface ListSchemasOnGithubOptions {
/**
* A token with read access to the chainlink repo
*/
token: string
/**
* The ref to pull schema files from
*/
ref?: string
}
async function listSchemasOnGithub({ token, ref }: ListSchemasOnGithubOptions) {
const repo = {
repo: 'chainlink',
owner: 'smartcontractkit',
ref: ref || 'develop',
}
if (!ref) {
console.warn(
`No ref supplied for ${repo.owner}/${repo.repo}, defaulting to ${repo.ref}`,
)
}
const baseDir = 'core/web/schema'
const subDirs = ['.', 'type']
const client = new Octokit({ auth: token })
const paths = subDirs.map((s) => path.join(baseDir, s))
const files = await Promise.all(
paths.map(async (path) => {
// since we are querying for directories, we should always get an array of file objects back
console.log(
`Grabbing schema files from ${JSON.stringify(repo)} in path: ${path}`,
)
const { data: content } = await client.rest.repos.getContent({
...repo,
path,
})
if (!Array.isArray(content)) {
throw Error('Content has invalid shape, it should be an array of files')
}
return content
.filter((f) => f.type === 'file' && f.path.includes('.graphql'))
.map((f) => f.path)
.map((p) => `github:${repo.owner}/${repo.repo}#${repo.ref}:${p}`)
}),
)
const flatFiles = files.flat()
return flatFiles
}
async function getConfig(): Promise<CodegenConfig> {
const token = process.env.GH_TOKEN
const schema = await getSchemaPath(token)
console.log(`Pulling schema files from: ${JSON.stringify(schema, null, 1)}`)
const config: CodegenConfig = {
overwrite: true,
config: {
token,
},
schema,
documents: [...schema, 'src/**/!(*.d).{ts,tsx}'],
generates: {
'src/types/generated/graphql.d.ts': {
plugins: ['typescript', 'typescript-operations'],
config: {
immutableTypes: true,
enumsAsTypes: true,
omitOperationSuffix: true,
globalNamespace: true,
exportFragmentSpreadSubTypes: true,
},
},
'./graphql.schema.json': {
plugins: ['introspection'],
},
'src/types/generated/possibleTypes.ts': {
plugins: ['fragment-matcher'],
config: {
useExplicitTyping: true,
},
},
},
}
return config
}
async function main() {
const config = await getConfig()
if (!process.env.DRY_RUN) await generate(config)
}
main()