-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
plugin.ts
60 lines (52 loc) · 1.4 KB
/
plugin.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
import { createContext, generate as codeGen_generate } from '@graphql-codegen/cli'
import { Log } from '@kitql/helper'
import { resolve } from 'path'
import type { Plugin } from 'vite'
import watch_and_run from 'vite-plugin-watch-and-run'
import type { KitQLVite } from './KitQLVite.js'
import { generate as kitql_generate } from './generate.js'
const log = new Log('KitQL')
export function kitql(config?: KitQLVite): Plugin[] {
return [
{
name: 'kitql',
async buildStart() {
await gooo(config)
},
},
watch_and_run([
{
name: 'kitql',
watch: resolve('src/**/*.(graphql)'),
run: () => gooo(config),
quiet: true,
},
]),
]
}
async function gooo(config?: KitQLVite) {
try {
// Codegen
const context = await createContext({
project: config?.projectName ?? 'init',
config: '',
watch: false,
require: [],
overwrite: true,
silent: true,
errorsOnly: false,
profile: false,
})
await codeGen_generate(context)
// KitQL (needs to be second as we write in the codegen module files)
kitql_generate(log, config)
} catch (e) {
if (e.name === 'AggregateError') {
// No '*.graphql' found. Maybe schema is coming from mesh only?
// Still, let's run KitQL generate to clean up old files
kitql_generate(log, config)
} else {
console.error(e)
}
}
}