2021-10-06 21:41:44 +00:00
|
|
|
import { generate } from "@graphql-codegen/cli";
|
2021-12-14 16:20:26 +00:00
|
|
|
import { execaCommand } from "execa";
|
2021-10-26 19:13:50 +00:00
|
|
|
const fileRegex = /\.(graphql)$/; // not used, will be for the vite rerun
|
2021-10-06 21:41:44 +00:00
|
|
|
export default function levelupViteCodegen(options) {
|
|
|
|
|
if (!options.schema) {
|
|
|
|
|
throw new Error("No schema provided");
|
|
|
|
|
}
|
|
|
|
|
if (!options.output) {
|
2021-12-14 16:20:26 +00:00
|
|
|
throw new Error("No output directory specified for types.");
|
2021-10-06 21:41:44 +00:00
|
|
|
}
|
2021-10-13 17:06:31 +00:00
|
|
|
if (!options.gPath) {
|
|
|
|
|
throw new Error("No gPath directory specified. gPath is where you've initialized the 'g' client");
|
|
|
|
|
}
|
|
|
|
|
const { schema, output, gPath } = options;
|
2021-10-26 19:13:50 +00:00
|
|
|
console.log("running plugin");
|
2021-10-06 21:41:44 +00:00
|
|
|
return {
|
2021-10-26 19:13:50 +00:00
|
|
|
name: "g-query-codegen",
|
2021-10-06 21:41:44 +00:00
|
|
|
async buildStart() {
|
|
|
|
|
try {
|
2021-11-15 23:10:40 +00:00
|
|
|
// *1. Remove all .gq files
|
2021-12-14 16:20:26 +00:00
|
|
|
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: "inherit",
|
|
|
|
|
shell: true,
|
|
|
|
|
});
|
2021-10-26 19:13:50 +00:00
|
|
|
// *2. Generate
|
2021-12-14 16:20:26 +00:00
|
|
|
console.log("🤖 starting codegen");
|
2021-10-26 19:13:50 +00:00
|
|
|
await generate({
|
2021-10-06 21:41:44 +00:00
|
|
|
schema,
|
|
|
|
|
documents: "./src/**/*.graphql",
|
|
|
|
|
generates: {
|
2021-10-26 19:13:50 +00:00
|
|
|
// * Generates the types for your schema
|
2021-11-15 23:10:40 +00:00
|
|
|
[`${process.cwd()}/${output}/types.gq.ts`]: {
|
2021-10-06 21:41:44 +00:00
|
|
|
plugins: ["typescript"],
|
|
|
|
|
},
|
2021-10-26 19:13:50 +00:00
|
|
|
// * Generates near file .ts files for your fetch functions
|
2021-10-13 17:06:31 +00:00
|
|
|
[`${process.cwd()}/${output}`]: {
|
|
|
|
|
config: {
|
2021-10-26 19:13:50 +00:00
|
|
|
useTypeImports: true,
|
2021-10-13 17:06:31 +00:00
|
|
|
gPath,
|
2021-10-26 19:13:50 +00:00
|
|
|
importDocumentNodeExternallyFrom: "near-operation-file",
|
|
|
|
|
inlineFragmentTypes: "combine",
|
2021-10-13 17:06:31 +00:00
|
|
|
},
|
|
|
|
|
preset: "near-operation-file",
|
|
|
|
|
presetConfig: {
|
2021-11-15 23:10:40 +00:00
|
|
|
extension: ".gq.ts",
|
2021-10-13 17:06:31 +00:00
|
|
|
folder: "./",
|
2021-11-15 23:10:40 +00:00
|
|
|
baseTypesPath: `types.gq.ts`,
|
2021-10-13 17:06:31 +00:00
|
|
|
},
|
|
|
|
|
plugins: [
|
|
|
|
|
"typescript-operations",
|
2021-10-26 19:13:50 +00:00
|
|
|
"@leveluptuts/g-query/codegen-plugin", // g-query codegen plugin.
|
2021-10-13 17:06:31 +00:00
|
|
|
],
|
|
|
|
|
},
|
2021-10-06 21:41:44 +00:00
|
|
|
},
|
|
|
|
|
}, true);
|
|
|
|
|
}
|
|
|
|
|
catch (e) {
|
2021-12-14 16:20:26 +00:00
|
|
|
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.");
|
2021-10-06 21:41:44 +00:00
|
|
|
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;
|
|
|
|
|
},
|
|
|
|
|
transform(src, id) { },
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
//# sourceMappingURL=plugin.js.map
|