import inquirer from 'inquirer' ; import { Types } from '@graphql-codegen/plugin-helpers' ; import { getQuestions } from './questions' ; import { guessTargets } from './targets' ; import { Answers , Tags } from './types' ; import { writeConfig , writePackage , bold } from './helpers' ; function log ( ... msgs ) { console . log ( ... msgs ) ; } export async function init ( ) { log ( `
    Welcome to ${ bold ( 'GraphQL Code Generator' ) }!
    Answer few questions and we will setup everything for you.
  ` ) ; const possibleTargets = await guessTargets ( ) ; const answers = await inquirer . prompt < Answers > ( getQuestions ( possibleTargets ) ) ; const config = { overwrite : true , schema : answers . schema , documents : answers . targets . includes ( Tags . browser ) ? answers . documents : null , generates : { [ answers . output ] : { plugins : answers . plugins . map ( p => p . value ) , } , } , } ; if ( answers . introspection ) { addIntrospection ( config ) ; } const { relativePath } = await writeConfig ( answers , config ) ; log ( `Fetching latest versions of selected plugins...` ) ; await writePackage ( answers , relativePath ) ; log ( `
    Config file generated at ${ bold ( relativePath ) }
    
      ${ bold ( '$ npm install' ) }

    To install the plugins.

      ${ bold ( `$ npm run ${ answers . script }` ) }

    To run GraphQL Code Generator.
  ` ) ; } function addIntrospection ( config ) { config . generates [ './graphql.schema.json' ] = { plugins : [ 'introspection' ] , } ; }