From f65f65914e0514aff946271fdd193334ee523eec Mon Sep 17 00:00:00 2001
From: Hyeseong Kim <cometkim.kr@gmail.com>
Date: Fri, 6 Sep 2019 02:29:35 +0900
Subject: [PATCH] Add plugin options to customize output paths

---
 README.md      | 31 +++++++++++++++++++++++++++++--
 gatsby-node.ts | 18 +++++++++++++-----
 package.json   |  3 ++-
 types.ts       | 13 +++++++++++++
 4 files changed, 57 insertions(+), 8 deletions(-)
 create mode 100644 types.ts

diff --git a/README.md b/README.md
index 00b36a2..5740514 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@ Watch changes, Automatically generates TypeScript definitions.
 
 - [x] Schema extraction
 - [x] Generates code using graphql-codegen
-- [ ] Options to customize inputs/outputs (More plugins, Schema-JSON/SDL output option, Type definitions, etc)
+- [x] Options to customize paths
 - [ ] Auto-fixing codes using `<StaticQuery>` and `useStaticQuery()` with generated type parameters.
 
 ## Requirements
@@ -28,9 +28,36 @@ yarn add gatsby-plugin-typegen
 plugins: [`gatsby-plugin-typegen`]
 ```
 
+Also you can customize output path of generated files
+
+```ts
+// Example of type-safe usage (optional)
+import { PluginOptions as TypegenPluginOptions } from 'gatsby-plugin-typegen';
+
+type Plugin = (
+  | string
+  | { resolve: string, options: any }
+  | { resolve: `gatsby-plugin-typegen` options: TypegenPluginOptions }
+);
+
+const plugins: Plugin[] = {
+  resolve: `gatsby-plugin-typegen`,
+  options: {
+    schemaOutputPath: `${__dirname}/.cache/caches/gatsby-plugin-typegen/schema.json`,
+    typeDefsOutputPath: `${__dirname}/node_modules/generated/types/gatsby.ts`,
+  },
+};
+
+module.exports = {
+  plugins,
+};
+```
+
 ## Available options
 
-TODO
+- `schemaOutputPath`: (`string?`) Path to where the schema file is being generated.
+
+- `typeDefsOutputPath`: (`string?`) Path to where the type definition file is being generated.
 
 ## Acknowledgements
 
diff --git a/gatsby-node.ts b/gatsby-node.ts
index 083cceb..03aaf08 100644
--- a/gatsby-node.ts
+++ b/gatsby-node.ts
@@ -6,16 +6,24 @@ import debounce from 'lodash.debounce';
 import { codegen } from '@graphql-codegen/core';
 import * as typescriptPlugin from '@graphql-codegen/typescript';
 import { loadDocuments, loadSchema } from 'graphql-toolkit';
-
 import { GatsbyNode } from 'gatsby';
 // @ts-ignore
 import { graphql, introspectionQuery } from 'gatsby/graphql';
 
+import { PluginOptions } from './types';
+
 const resolve = (...paths: string[]) => path.resolve(process.cwd(), ...paths);
 const log = (message: string) => console.log(`[gatsby-plugin-typegen] ${message}`);
-const schemaOutputPath = resolve('.cache/caches/gatsby-plugin-typegen/schema.json');
 
-export const onPostBootstrap: GatsbyNode['onPostBootstrap'] = async ({ store }) => {
+const DEFAULT_SCHEMA_OUTPUT_PATH = resolve('.cache/caches/gatsby-plugin-typegen/schema.json');
+const DEFAULT_TYPE_DEFS_OUTPUT_PATH = resolve('node_modules/generated/types/gatsby.ts');
+
+export const onPostBootstrap: GatsbyNode['onPostBootstrap'] = async ({ store }, options) => {
+  const {
+    schemaOutputPath = DEFAULT_SCHEMA_OUTPUT_PATH,
+    typeDefsOutputPath = DEFAULT_TYPE_DEFS_OUTPUT_PATH,
+  } = options as PluginOptions;
+
   let cache = '';
 
   const extractSchema = async () => {
@@ -60,8 +68,8 @@ export const onPostBootstrap: GatsbyNode['onPostBootstrap'] = async ({ store })
   const writeTypeDefinition = debounce(async () => {
     // @ts-ignore
     const output = await codegen(config);
-    await fs.promises.mkdir(resolve('generated/types'), { recursive: true });
-    await fs.promises.writeFile(resolve('generated/types/gatsby.ts'), output, 'utf-8');
+    await fs.promises.mkdir(path.dirname(typeDefsOutputPath), { recursive: true });
+    await fs.promises.writeFile(typeDefsOutputPath, output, 'utf-8');
     log('Type definitions have been generated');
   }, 1000);
 
diff --git a/package.json b/package.json
index bc7db19..20bd857 100644
--- a/package.json
+++ b/package.json
@@ -19,7 +19,8 @@
   },
   "files": [
     "index.js",
-    "gatsby-node.js"
+    "gatsby-node.js",
+    "types.ts"
   ],
   "peerDependencies": {
     "gatsby": ">=2.0.0",
diff --git a/types.ts b/types.ts
new file mode 100644
index 0000000..15da894
--- /dev/null
+++ b/types.ts
@@ -0,0 +1,13 @@
+export type PluginOptions = {
+  /**
+   * Path to where the schema file is being generated.
+   * @default `${CWD}/.cache/caches/gatsby-plugin-typegen/schema.json`
+   */
+  schemaOutputPath?: string,
+
+  /**
+   * Path to where the type definition file is being generated.
+   * @default `${CWD}/node_modules/generated/types/gatsby.ts`
+   */
+  typeDefsOutputPath?: string,
+};
-- 
GitLab