import type { Required } from 'utility-types' ; import type { Reporter } from 'gatsby' ; import type { GraphQLTagPluckOptions } from '@graphql-tools/graphql-tag-pluck' ; import type { GatsbyStore } from './gatsby-utils' ; import type { PluginOptions , SchemaOutputOptions , DeprecatedPluginOptions , } from './types' ; import path from 'path' ; import { formatLanguage } from './common' ; import { gatsbyInternalScalars } from './gatsby-utils' ; const DEFAULT_SCHEMA_OUTPUT_OPTION = { format : 'introspection' , commentDescriptions : true , } as const ; type MapEmitSchemaOption < T > = T extends { [ key ] : infer V ; } ? V extends true ? { [ key ] : typeof DEFAULT_SCHEMA_OUTPUT_OPTION ; } : { [ key ] : SchemaOutputOptions ; } : never ; export type RequiredPluginOptions = Required < Omit < PluginOptions , ( keyof DeprecatedPluginOptions | 'emitSchema' ) > & { emitSchema : MapEmitSchemaOption < PluginOptions [ 'emitSchema' ] > ; } > ; interface RequirePluginOptionsFn { ( options , props ) : RequiredPluginOptions ; } export const requirePluginOptions = ( options , { store , reporter , } ) => { const { program } = store . getState ( ) ; const basePath = program . directory ; const pluginOptions = options as PluginOptions ; const { language = 'typescript' , namespace = 'GatsbyTypes' , emitSchema : emitSchemaOptionMap = { } , includeResolvers = false , autoFix = true , emitPluginDocuments = { } , schemaOutputPath , typeDefsOutputPath , scalars = { } , } = pluginOptions ; const emitSchema = { } ; for ( const [ key , options ] of Object . entries ( emitSchemaOptionMap ) ) { if ( options === true ) { emitSchema [ key ] = { ... DEFAULT_SCHEMA_OUTPUT_OPTION , format : /\.(gql|graphql)$/ . test ( key ) ? 'sdl' : 'introspection' , } ; } } if ( schemaOutputPath ) { reporter . warn ( '`schemaOutputPath` was deprecated, please use `emitSchema` instead.' ) ; emitSchema [ schemaOutputPath ] = DEFAULT_SCHEMA_OUTPUT_OPTION ; } if ( typeDefsOutputPath ) { reporter . warn ( '`typeDefsOutputPath` was deprecated, please use `outputPath` instead.' ) ; } const outputPath = pluginOptions . outputPath || typeDefsOutputPath || ( language === 'typescript' ? path . resolve ( basePath , 'src/__generated__/gatsby-types.ts' ) : path . resolve ( basePath , 'src/__generated__/gatsby-types.js' ) ) ; if ( ( language === 'typescript' ) !== /\.tsx?$/ . test ( outputPath ) ) { reporter . warn ( reporter . stripIndent ( `The language you specified is not match to file extension.
        - language: ${ formatLanguage ( language ) }
        - outputPath: ${ outputPath }
      ` ) ) ; } for ( const type of gatsbyInternalScalars ) { if ( scalars [ type ] ) { reporter . warn ( `[typegen] You couldn't override type for \`${ type }\` scalar because it is reserved by Gatsby internal.` ) ; delete scalars [ type ] ; } } return { language , namespace , outputPath , includeResolvers , autoFix , emitSchema , emitPluginDocuments , scalars , } ; } ; export const GRAPHQL_TAG_PLUCK_OPTIONS = { modules : [ { name : 'gatsby' , identifier : 'graphql' , } , ] , } ;