import { NamedTypeNode , OperationDefinitionNode , TypeNode , VariableDefinitionNode } from 'graphql' ; type OclifFlagType = 'boolean' | 'integer' | 'string' | 'enum' ; interface TypeConfig { required : boolean ; list : boolean ; innerType : NamedTypeNode ; } export const getFlagConfigForVariableDefinition = ( definition ) => { const { list , required , innerType } = getInnerType ( definition . type ) ; const oclifType = mapVariableTypeToOclifType ( innerType ) ; const parser = getParserForType ( innerType ) ; return `${ definition . variable . name . value }: flags.${ oclifType }({
  multiple: ${ list },
  required: ${ required },${ parser ? `\n  parse: ${ parser }` : '' }
})` ; } ; const getParserForType = ( type ) : string | void => { if ( type . name . value === 'Float' ) { return 'input => Number(input)' ; } } ; const mapVariableTypeToOclifType = ( type ) : OclifFlagType => { if ( type . name . value === 'Boolean' ) { return 'boolean' ; } else if ( [ 'Float' , 'Int' ] . includes ( type . name . value ) ) { return 'integer' ; } else { return 'string' ; } } ; const getInnerType = ( type ) : TypeConfig => { const result = { list : false , required : false , } ; let _type = type ; while ( _type . kind !== 'NamedType' ) { if ( _type . kind === 'ListType' ) { result . list = true ; } else if ( _type . kind === 'NonNullType' ) { result . required = true ; } _type = _type . type ; } result . innerType = _type ; return result as TypeConfig ; } ; export const omitOclifDirectives = ( node ) => { const directives = node . directives . filter ( directive => directive . name . value !== 'oclif' ) ; return Object . assign ( { } , node , { directives } ) ; } ;