import fs from 'fs';
import path from 'path';
import isGlob from 'is-glob';
import Glob from 'glob';

const recursiveReadDirSync = dir =>
  fs.readdirSync(dir)
    .reduce((files, file) => (
      fs.statSync(path.join(dir, file)).isDirectory() ?
        files.concat(recursiveReadDirSync(path.join(dir, file))) :
        files.concat(path.join(dir, file))
      ),
      []);

const readDirSync = dir =>
  fs.readdirSync(dir)
    .reduce((files, file) => (
      fs.statSync(path.join(dir, file)).isDirectory() ?
        files :
        files.concat(path.join(dir, file))
      ),
      []);

const readGlobSync = (pattern, options) =>
  Glob.sync(pattern, options);

const getSchemaFiles = (dir, recursive, globOptions) => {
  if (isGlob(dir)) {
    return readGlobSync(dir, globOptions);
  }

  if (recursive === true) {
    return recursiveReadDirSync(dir);
  }

  return readDirSync(dir);
};

const DEFAULT_EXTENSIONS = ['.ts', '.js', '.gql', '.graphql', '.graphqls'];

const fileLoader = (folderPath,
  {
    recursive = false,
    extensions = DEFAULT_EXTENSIONS,
    globOptions = {},
  } = {}) => {
  const dir = folderPath;
  const schemafiles = getSchemaFiles(dir, recursive, globOptions);

  const files = schemafiles
            .map(f => ({ f, pathObj: path.parse(f) }))
            .filter(({ pathObj }) => pathObj.name.toLowerCase() !== 'index')
            .filter(({ pathObj }) => extensions.includes(pathObj.ext))
            .map(({ f, pathObj }) => {
              let returnVal;

              switch (pathObj.ext) {
                case '.ts':
                case '.js': {
                  const file = require(f); // eslint-disable-line
                  returnVal = file.default || file;
                  break;
                }

                case '.graphqls':
                case '.gql':
                case '.graphql': {
                  const file = fs.readFileSync(f, 'utf8');
                  returnVal = file;
                  break;
                }

                default:
                    // we don't know how to handle other extensions
              }

              return returnVal;
            })
            .filter(v => !!v);    // filter files that we don't know how to handle

  return files;
};

export default fileLoader;

@@ -1227,28 +1227,24 @@
 les%0A        
-    
 .map(f =%3E (%7B
@@ -1274,36 +1274,32 @@
 (f) %7D))%0A        
-   
- 
 .filter((%7B pathO
@@ -1347,36 +1347,32 @@
 index')%0A        
-    
 .filter((%7B pathO
@@ -1418,28 +1418,24 @@
 t))%0A        
- 
-   
 .map((%7B f, p
@@ -1459,20 +1459,16 @@
         
-   
- 
 let retu
@@ -1475,20 +1475,16 @@
 rnVal;%0A%0A
-    
         
@@ -1516,28 +1516,24 @@
             
- 
-   
 case '.ts':%0A
@@ -1528,28 +1528,24 @@
 case '.ts':%0A
-    
             
@@ -1554,28 +1554,24 @@
 se '.js': %7B%0A
-    
             
@@ -1626,36 +1626,32 @@
 e%0A              
-  
-  
 returnVal = file
@@ -1674,36 +1674,32 @@
 ;%0A              
-    
 break;%0A         
@@ -1693,36 +1693,32 @@
 ak;%0A            
-    
 %7D%0A%0A             
@@ -1708,36 +1708,32 @@
  %7D%0A%0A            
-  
-  
 case '.graphqls'
@@ -1738,36 +1738,32 @@
 s':%0A            
-    
 case '.gql':%0A   
@@ -1763,36 +1763,32 @@
 l':%0A            
- 
-   
 case '.graphql':
@@ -1796,36 +1796,32 @@
 %7B%0A              
-   
- 
 const file = fs.
@@ -1851,36 +1851,32 @@
 ;%0A              
-    
 returnVal = file
@@ -1883,36 +1883,32 @@
 ;%0A              
-    
 break;%0A         
@@ -1906,35 +1906,27 @@
             
-   
- 
 %7D%0A%0A
-    
             
@@ -1934,20 +1934,16 @@
 efault:%0A
-    
         
@@ -1998,20 +1998,16 @@
 ensions%0A
-    
         
@@ -2017,28 +2017,24 @@
 %0A%0A          
-    
 return retur
@@ -2051,19 +2051,11 @@
     
-    %7D)%0A    
+%7D)%0A
     

Fix indenting to 6 instead of 14 spaces