This repository was archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathplugin.ts
More file actions
112 lines (101 loc) · 3.96 KB
/
plugin.ts
File metadata and controls
112 lines (101 loc) · 3.96 KB
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
import type { Plugin } from "vite";
import { generate } from "@graphql-codegen/cli";
import { execaCommand } from "execa";
// Only load up graphql, graphqls or gql files
const filterExt = /\.(graphqls?|gql)$/i;
// Delete all gq (generated) files
async function cleanGQ({ debug = false }) {
debug && console.log("🧹 removing all .gq files");
// Find and remove all .gq files
await execaCommand("find ./ -path '*.gq.ts' -type f -prune -print -exec rm -f '{}' +; ", {
stdio: debug ? "inherit" : "ignore",
shell: true,
});
}
export interface GQueryGenerateOptions {
schema: string;
output: string;
gPath: string;
debug?: boolean;
}
// Runs graphql codegen
async function gQueryGenerate({ schema, output, gPath, debug = false }: GQueryGenerateOptions) {
debug && console.log("🤖 starting codegen");
// the actual codegen process.
await generate(
{
schema,
documents: "./src/**/*.graphql",
generates: {
// * Generates the types for your schema
[`${process.cwd()}/${output}/types.gq.ts`]: {
plugins: ["typescript"],
},
// * Generates near file .ts files for your fetch functions
[`${process.cwd()}/${output}`]: {
config: {
useTypeImports: true,
gPath,
importDocumentNodeExternallyFrom: "near-operation-file",
inlineFragmentTypes: "combine",
},
preset: "near-operation-file",
presetConfig: {
extension: ".gq.ts",
folder: "./",
baseTypesPath: `types.gq.ts`,
},
plugins: [
"typescript-operations", // operations, gets you types for operations (queries and mutations)
"@leveluptuts/g-query/codegen-plugin", // g-query codegen plugin. ./codegen.ts
],
},
},
},
true
);
}
export default function levelupViteCodegen({ schema, output, gPath, debug = false }: GQueryGenerateOptions): Plugin {
if (!schema) {
throw new Error("No schema provided");
}
if (!output) {
throw new Error("No output directory specified for types.");
}
if (!gPath) {
throw new Error("No gPath directory specified. gPath is where you've initialized the 'g' client");
}
return {
name: "g-query-codegen",
async buildStart() {
console.log("build start");
try {
await cleanGQ({ debug });
await gQueryGenerate({ schema, output, gPath, debug });
return;
} catch (e) {
// TODO - These errors aren't good enough
console.log("❓ gFetch Error - Something Happened - Here is the error and some things to consider.", e);
console.log("❓ gFetch Error - Make sure `.graphql` are files found.");
console.log(
"❓ gFetch Warning - If you would like the gQuery generator to work (we reccomend you do)."
);
console.log("❓ gFetch Warning - If you would like to add them, you will need to restart SvelteKit.");
}
return;
},
configureServer(server) {
const listener = async (absolutePath = "") => {
if (!filterExt.test(absolutePath)) return null;
try {
await cleanGQ({ debug });
await gQueryGenerate({ schema, output, gPath });
} catch (error) {
console.log("Something went wrong. Please save the file again.");
}
};
server.watcher.on("add", listener);
server.watcher.on("change", listener);
},
};
}