#! /usr/bin/env node
const args = require('args')
const path = require('path')
const { printSchema } = require('graphql')
const chalk = require('chalk')

const gest = require('../src/index')
const REPL = require('../src/REPL')
const { readFile, checkPath, flagsToOptions, colorResponse, errorMessage, findFiles } = require('../src/util')

args
  .option(['S', 'schema'], 'Path to your GraphQL schema')
  .option(['I', 'inspect'], 'Print your GraphQL schema options')
  .option(['H', 'header'], 'HTTP request header')
  .option(['B', 'baseUrl'], 'Base URL for sending HTTP requests')
  .option('all', 'Run all *.query files')

const flags = args.parse(process.argv)
try {
  if (!flags.help && !flags.version) {
    let config
    try {
      config = require(path.join(process.cwd(), 'package.json')).gest
    } catch (e) {}

    const options = Object.assign({ schema: 'schema.js' }, config, flagsToOptions(flags))

    const schema = require(path.join(process.cwd(), options.schema))

    if (flags.inspect) {
      console.log(printSchema(schema))
      process.exit()
    }

    if (flags.all) {
      findFiles()
        .then(values =>
          values.map(v => {
            const rep = chalk.dim(v.replace(process.cwd(), '.'))
            console.log(`${chalk.black.bgYellow(' RUNS ')} ${rep}`)
            return readFile(v)
              .then(gest(schema, options))
              .then(value => {
                if (value.errors && value.data) return `${chalk.black.bgYellow(' WARNING ')} ${rep}`
                if (value.errors) return `${chalk.black.bgRed(' FAIL ')} ${rep}`
                return `${chalk.black.bgGreen(' PASS ')} ${rep}`
              })
              .then(console.log)
              .catch(console.log)
          }))
        .catch(console.log)
    } else {
      // DEFAULT COMMAND
      if (args.sub && args.sub.length) {
        checkPath(path.join(process.cwd(), args.sub[0]))
        .then(readFile)
        .catch(() => args.sub[0])
        .then(gest(schema, options))
        .then(colorResponse)
        .then(console.log)
        .catch(console.log)
      } else {
        // REPL
        REPL(schema, options)
      }
    }
  }
} catch (e) {
  console.log(errorMessage(e))
}

@@ -1144,16 +1144,28 @@
         
+Promise.all(
 values.m
@@ -1460,23 +1460,28 @@
 e.data) 
-return 
+console.log(
 %60$%7Bchalk
@@ -1509,32 +1509,33 @@
 NING ')%7D $%7Brep%7D%60
+)
 %0A               
@@ -1527,32 +1527,37 @@
                 
+else 
 if (value.errors
@@ -1558,23 +1558,28 @@
 errors) 
-return 
+console.log(
 %60$%7Bchalk
@@ -1601,32 +1601,33 @@
 FAIL ')%7D $%7Brep%7D%60
+)
 %0A               
@@ -1627,23 +1627,33 @@
         
-return 
+else console.log(
 %60$%7Bchalk
@@ -1685,16 +1685,60 @@
  $%7Brep%7D%60
+)%0A                return %5Brep, value.errors%5D
 %0A       
@@ -1754,36 +1754,37 @@
 %0A              .
-then
+catch
 (console.log)%0A  
@@ -1795,44 +1795,180 @@
     
-    .catch(console.log)%0A          %7D)
+%7D)))%0A        .then(values =%3E%0A          values.map((%5Brep, errors%5D) =%3E%0A            (errors ? %60$%7Bchalk.dim.red(rep)%7D: $%7Berrors%7D%5Cn%60 : '')).join('%5Cn'))%0A        .then(console.log
 )%0A  

Print error messages for each file