diff --git a/codegen/plugin.d.ts b/codegen/plugin.d.ts index e9cac1f..d210b46 100644 --- a/codegen/plugin.d.ts +++ b/codegen/plugin.d.ts @@ -1,7 +1,8 @@ import type { Plugin } from "vite"; -export default function levelupViteCodegen({ schema, out, gPath, debug, }: { - schema: any; - out: any; - gPath: any; +export interface GQueryGenerateOptions { + schema: string; + output: string; + gPath: string; debug?: boolean; -}): Plugin; +} +export default function levelupViteCodegen({ schema, output, gPath, debug }: GQueryGenerateOptions): Plugin; diff --git a/codegen/plugin.js b/codegen/plugin.js index 5214de9..71ad34a 100644 --- a/codegen/plugin.js +++ b/codegen/plugin.js @@ -12,7 +12,7 @@ async function cleanGQ({ debug = false }) { }); } // Runs graphql codegen -async function gQueryGenerate({ schema, out, gPath, debug = false }) { +async function gQueryGenerate({ schema, output, gPath, debug = false }) { debug && console.log("🤖 starting codegen"); // the actual codegen process. await generate({ @@ -20,11 +20,11 @@ async function gQueryGenerate({ schema, out, gPath, debug = false }) { documents: "./src/**/*.graphql", generates: { // * Generates the types for your schema - [`${process.cwd()}/${out}/types.gq.ts`]: { + [`${process.cwd()}/${output}/types.gq.ts`]: { plugins: ["typescript"], }, // * Generates near file .ts files for your fetch functions - [`${process.cwd()}/${out}`]: { + [`${process.cwd()}/${output}`]: { config: { useTypeImports: true, gPath, @@ -45,11 +45,11 @@ async function gQueryGenerate({ schema, out, gPath, debug = false }) { }, }, true); } -export default function levelupViteCodegen({ schema, out, gPath, debug = false, }) { +export default function levelupViteCodegen({ schema, output, gPath, debug = false }) { if (!schema) { throw new Error("No schema provided"); } - if (!out) { + if (!output) { throw new Error("No output directory specified for types."); } if (!gPath) { @@ -61,7 +61,7 @@ export default function levelupViteCodegen({ schema, out, gPath, debug = false, console.log("build start"); try { await cleanGQ({ debug }); - await gQueryGenerate({ schema, out, gPath, debug }); + await gQueryGenerate({ schema, output, gPath, debug }); return; } catch (e) { @@ -79,7 +79,7 @@ export default function levelupViteCodegen({ schema, out, gPath, debug = false, return null; try { await cleanGQ({ debug }); - await gQueryGenerate({ schema, out, gPath }); + await gQueryGenerate({ schema, output, gPath }); } catch (error) { console.log("Something went wrong. Please save the file again."); diff --git a/codegen/plugin.js.map b/codegen/plugin.js.map index d35665f..14ab23c 100644 --- a/codegen/plugin.js.map +++ b/codegen/plugin.js.map @@ -1 +1 @@ -{"version":3,"file":"plugin.js","sourceRoot":"","sources":["plugin.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAErC,8CAA8C;AAE9C,MAAM,SAAS,GAAG,qBAAqB,CAAC;AAExC,kCAAkC;AAClC,KAAK,UAAU,OAAO,CAAC,EAAE,KAAK,GAAG,KAAK,EAAE;IACtC,KAAK,IAAI,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC;IAClD,gCAAgC;IAChC,MAAM,YAAY,CAChB,oEAAoE,EACpE;QACE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ;QACnC,KAAK,EAAE,IAAI;KACZ,CACF,CAAC;AACJ,CAAC;AAED,uBAAuB;AACvB,KAAK,UAAU,cAAc,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,GAAG,KAAK,EAAE;IACjE,KAAK,IAAI,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;IAE5C,8BAA8B;IAC9B,MAAM,QAAQ,CACZ;QACE,MAAM;QACN,SAAS,EAAE,oBAAoB;QAC/B,SAAS,EAAE;YACT,wCAAwC;YACxC,CAAC,GAAG,OAAO,CAAC,GAAG,EAAE,IAAI,GAAG,cAAc,CAAC,EAAE;gBACvC,OAAO,EAAE,CAAC,YAAY,CAAC;aACxB;YACD,2DAA2D;YAC3D,CAAC,GAAG,OAAO,CAAC,GAAG,EAAE,IAAI,GAAG,EAAE,CAAC,EAAE;gBAC3B,MAAM,E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egen/plugin.ts @@ -8,113 +8,105 @@ 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, - } - ); + 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, out, gPath, debug = false }) { - debug && console.log("🤖 starting 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()}/${out}/types.gq.ts`]: { - plugins: ["typescript"], + // 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 + ], + }, + }, }, - // * Generates near file .ts files for your fetch functions - [`${process.cwd()}/${out}`]: { - 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 - ); + true + ); } -export default function levelupViteCodegen({ - schema, - out, - gPath, - debug = false, -}): Plugin { - if (!schema) { - throw new Error("No schema provided"); - } - if (!out) { - 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" - ); - } +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, out, gPath, debug }); + 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; - }, + 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, out, gPath }); - } catch (error) { - console.log("Something went wrong. Please save the file again."); - } - }; + 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); - }, - }; + server.watcher.on("add", listener); + server.watcher.on("change", listener); + }, + }; } diff --git a/docs/src/docs.md b/docs/src/docs.md index 7bc451d..dbaa4cd 100644 --- a/docs/src/docs.md +++ b/docs/src/docs.md @@ -36,7 +36,7 @@ vite: { // schema: 'http://localhost:3001/graphql' // this can also be a url to a graphql api schema: './src/lib/graphql/schema.graphql', // path to schema, schema is required output: './src/lib/graphql', // Where you want the general schema types to output - gPath: '$lib/config/g' // Path to g, created in step 1. + gPath: '$lib/config/g', // Path to g, created in step 1. // Optional debug: false // boolean, this adds logging for gq files deleted and on codegen })