import { GraphQLSchema, generateTypeDefs } from './graphql';
import Vulcan from '../modules/config.js';
import fs from 'fs';

if (Meteor.isServer) {
  /*

  Can be called from the Meteor shell (type `meteor shell` in your app repo)

  */
  Vulcan.getGraphQLSchema = () => {
    let schema;
    if (!GraphQLSchema.finalSchema) {
      schema = generateTypeDefs(GraphQLSchema)[0];
      // eslint-disable-next-line no-console
      console.log(
        'Warning: trying to access final GraphQL schema before it has been created by the server.'
      );
    } else {
      schema = GraphQLSchema.finalSchema[0];
    }

    // the server path is of type "/Users/foo/bar/appName/.meteor/local/build/programs/server"
    // we remove the last five segments to get the app directory
    // eslint-disable-next-line no-undef
    const path = __meteor_bootstrap__.serverDir.split('/').slice(1,-5).join('/');
    const fullPath = `/${path}/schema.graphql`;

    fs.writeFile(fullPath, schema, error => {
      // throws an error, you could also catch it here
      if (error) throw error;

      // eslint-disable-next-line no-console
      console.log(`GraphQL schema saved to ${fullPath}`);
    });
    return schema;
  };

  Vulcan.logToFile = (fileName, object) => {
    // eslint-disable-next-line no-undef
    const path = __meteor_bootstrap__.serverDir.split('/').slice(1,-5).join('/');
    const fullPath = `/${path}/${fileName}`;

    fs.writeFile(fullPath, JSON.stringify(object, null, 2), error => {
      // throws an error, you could also catch it here
      if (error) throw error;

      // eslint-disable-next-line no-console
      console.log(`Object saved to ${fullPath}`);
    });
  };
  
}
@@ -119,16 +119,375 @@
  'fs';%0A%0A
+import %7B Collections %7D from '../modules/collections.js';%0Aimport %7B extractCollectionInfo, extractFragmentInfo %7D from '../modules/handleOptions';%0A%0Aimport %7B multiClientTemplate, singleClientTemplate %7D from '../modules/graphql_templates';%0Aimport %7B Fragments %7D from '../modules/fragments';%0Aimport %7B Utils %7D from '../modules/utils';%0A%0Aimport get from 'lodash/get';%0A%0A
 if (Mete
@@ -501,16 +501,16 @@
 rver) %7B%0A
-
   /*%0A%0A  
@@ -622,10 +622,16 @@
 a = 
-()
+fileName
  =%3E 
@@ -974,16 +974,73 @@
 %0A    %7D%0A%0A
+    const name = fileName ? fileName : 'schema.graphql';%0A
     // t
@@ -1345,30 +1345,23 @@
 $%7Bpath%7D/
-schema.graphql
+$%7Bname%7D
 %60;%0A%0A    
@@ -2107,8 +2107,1467 @@
  %7D;%0A
+%0A
   
+Vulcan.generateGraphQLQueries = fileName =%3E %7B%0A    let fd;%0A%0A    const name = fileName ? fileName : 'queries.graphql';%0A%0A    try %7B%0A      // eslint-disable-next-line no-undef%0A      const path = __meteor_bootstrap__.serverDir%0A        .split('/')%0A        .slice(1, -5)%0A        .join('/');%0A      const fullPath = %60/$%7Bpath%7D/$%7Bname%7D%60;%0A      fd = fs.openSync(fullPath, 'w');%0A%0A      Object.keys(Fragments).forEach(fragment =%3E fs.appendFileSync(fd, Fragments%5Bfragment%5D.fragmentText + '%5Cn'));%0A%0A      fs.appendFileSync(fd, '%5Cn');%0A%0A      Collections.forEach(collection =%3E %7B%0A        const %7B collectionName %7D = extractCollectionInfo(%7B collection %7D);%0A        const %7B fragmentName %7D = extractFragmentInfo(%7B%7D, collectionName);%0A%0A        const typeName = collection.options.typeName;%0A%0A%0A        if (get(GraphQLSchema.resolvers, %60Query.$%7BUtils.camelCaseify(typeName)%7D%60)) %7B%0A          const singleQueryString = singleClientTemplate(%7B%0A            typeName,%0A            fragmentName,%0A          %7D);%0A          fs.appendFileSync(fd, singleQueryString + '%5Cn');%0A        %7D%0A%0A        if (get(GraphQLSchema.resolvers, %60Query.$%7BUtils.camelCaseify(Utils.pluralize(typeName))%7D%60)) %7B%0A%0A          const multiQueryString = multiClientTemplate(%7B%0A            typeName,%0A            fragmentName,%0A          %7D);%0A          fs.appendFileSync(fd, multiQueryString + '%5Cn');%0A        %7D%0A%0A      %7D);%0A    %7D catch (err) %7B%0A      console.log(err);%0A    %7D finally %7B%0A      if (fd !== undefined) fs.closeSync(fd);%0A    %7D%0A  %7D;%0A%7D
 %0A
-%7D

Add filename param to Vulcan.getGraphQLSchema and add Vulcan.generateGraphQLQueries