<commit_before>import { FileOutput, MultiFileTemplates, Settings } from './types';
import { GeneratorConfig } from 'graphql-codegen-generators';
import {
  Enum,
  Fragment,
  Interface,
  Operation,
  Scalar,
  SchemaTemplateContext,
  Type,
  Union,
  Document, debugLog
} from 'graphql-codegen-core';
import { sanitizeFilename } from './sanitizie-filename';
import { prepareSchemaForDocumentsOnly } from './prepare-documents-only';
import * as path from 'path';

const handlersMap = {
  type: handleType,
  inputType: handleInputType,
  union: handleUnion,
  'enum': handleEnum,
  scalar: handleScalar,
  'interface': handleInterface,
  operation: handleOperation,
  fragment: handleFragment,
  schema: handleSchema,
  documents: handleDocuments,
  all: handleAll,
};

export const ALLOWED_CUSTOM_TEMPLATE_EXT = [
  'template',
  'handlebars',
  'tmpl',
  'gqlgen'
];

function handleSchema(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleSchema] called`);

  return [{
    filename: prefixAndPath + '.' + (fileExtension || ''),
    content: compiledTemplate({
      ...schemaContext
    }),
  }];
}

function handleAll(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleAll] called`);

  return [{
    filename: prefixAndPath + '.' + (fileExtension || ''),
    content: compiledTemplate({
      ...schemaContext,
      operations: documents.operations,
      fragments: documents.fragments,
      hasFragments: documents.hasFragments,
      hasOperations: documents.hasOperations,
    }),
  }];
}

function handleDocuments(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleDocuments] called`);

  return [{
    filename: prefixAndPath + '.' + (fileExtension || ''),
    content: compiledTemplate({
      operations: documents.operations,
      fragments: documents.fragments,
      hasFragments: documents.hasFragments,
      hasOperations: documents.hasOperations,
    }),
  }];
}

function handleType(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleType] called`);

  return schemaContext.types.map((type: Type) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(type.name, 'type') + '.' + (fileExtension || ''),
    content: compiledTemplate(type),
  }));
}

function handleInputType(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleInputType] called`);

  return schemaContext.inputTypes.map((type: Type) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(type.name, 'input-type') + '.' + (fileExtension || ''),
    content: compiledTemplate(type),
  }));
}

function handleUnion(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleUnion] called`);

  return schemaContext.unions.map((union: Union) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(union.name, 'union') + '.' + (fileExtension || ''),
    content: compiledTemplate(union),
  }));
}

function handleEnum(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleEnum] called`);

  return schemaContext.enums.map((en: Enum) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(en.name, 'enum') + '.' + (fileExtension || ''),
    content: compiledTemplate(en),
  }));
}

function handleScalar(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleScalar] called`);

  return schemaContext.scalars.map((scalar: Scalar) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(scalar.name, 'scalar') + '.' + (fileExtension || ''),
    content: compiledTemplate(scalar),
  }));
}

function handleInterface(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleInterface] called`);

  return schemaContext.interfaces.map((inf: Interface) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(inf.name, 'interface') + '.' + (fileExtension || ''),
    content: compiledTemplate(inf),
  }));
}

function handleOperation(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleOperation] called`);

  return documents.operations.map((operation: Operation) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(operation.name, operation.operationType) + '.' + (fileExtension || ''),
    content: compiledTemplate(operation),
  }));
}

function handleFragment(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleFragment] called`);

  return documents.fragments.map((fragment: Fragment) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(fragment.name, 'fragment') + '.' + (fileExtension || ''),
    content: compiledTemplate(fragment),
  }));
}

function parseTemplateName(templateName: string): { prefix: string; handler: Function; fileExtension: string; } {
  const splitted = (path.basename(templateName)).split('.');

  if (splitted.length === 3) {
    splitted.unshift('');
  }

  if (splitted.length !== 4) {
    throw new Error(`Invalid template name: ${templateName}!`);
  }

  const templateExtension = splitted[3];

  if (templateExtension && ALLOWED_CUSTOM_TEMPLATE_EXT.includes(templateExtension)) {
    const compilationContext = splitted[2];
    const prefix = splitted[0];
    const fileExtension = splitted[1];
    const handler = handlersMap[compilationContext];

    if (handler) {
      return {
        prefix: path.resolve(path.dirname(templateName), '/', prefix),
        handler,
        fileExtension,
      };
    }
  }

  return null;
}

export function generateMultipleFiles(templates: MultiFileTemplates, executionSettings: Settings, config: GeneratorConfig, templateContext: SchemaTemplateContext, documents: Document): FileOutput[] {
  debugLog(`[generateMultipleFiles] Compiling multiple files...`);<commit_msg>try fo fix multiple export without prefix<commit_after>import { FileOutput, MultiFileTemplates, Settings } from './types';
import { GeneratorConfig } from 'graphql-codegen-generators';
import {
  Enum,
  Fragment,
  Interface,
  Operation,
  Scalar,
  SchemaTemplateContext,
  Type,
  Union,
  Document, debugLog
} from 'graphql-codegen-core';
import { sanitizeFilename } from './sanitizie-filename';
import { prepareSchemaForDocumentsOnly } from './prepare-documents-only';
import * as path from 'path';

const handlersMap = {
  type: handleType,
  inputType: handleInputType,
  union: handleUnion,
  'enum': handleEnum,
  scalar: handleScalar,
  'interface': handleInterface,
  operation: handleOperation,
  fragment: handleFragment,
  schema: handleSchema,
  documents: handleDocuments,
  all: handleAll,
};

export const ALLOWED_CUSTOM_TEMPLATE_EXT = [
  'template',
  'handlebars',
  'tmpl',
  'gqlgen'
];

function handleSchema(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleSchema] called`);

  return [{
    filename: prefixAndPath + '.' + (fileExtension || ''),
    content: compiledTemplate({
      ...schemaContext
    }),
  }];
}

function handleAll(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleAll] called`);

  return [{
    filename: prefixAndPath + '.' + (fileExtension || ''),
    content: compiledTemplate({
      ...schemaContext,
      operations: documents.operations,
      fragments: documents.fragments,
      hasFragments: documents.hasFragments,
      hasOperations: documents.hasOperations,
    }),
  }];
}

function handleDocuments(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleDocuments] called`);

  return [{
    filename: prefixAndPath + '.' + (fileExtension || ''),
    content: compiledTemplate({
      operations: documents.operations,
      fragments: documents.fragments,
      hasFragments: documents.hasFragments,
      hasOperations: documents.hasOperations,
    }),
  }];
}

function handleType(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleType] called`);

  return schemaContext.types.map((type: Type) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(type.name, 'type') + '.' + (fileExtension || ''),
    content: compiledTemplate(type),
  }));
}

function handleInputType(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleInputType] called`);

  return schemaContext.inputTypes.map((type: Type) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(type.name, 'input-type') + '.' + (fileExtension || ''),
    content: compiledTemplate(type),
  }));
}

function handleUnion(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleUnion] called`);

  return schemaContext.unions.map((union: Union) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(union.name, 'union') + '.' + (fileExtension || ''),
    content: compiledTemplate(union),
  }));
}

function handleEnum(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleEnum] called`);

  return schemaContext.enums.map((en: Enum) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(en.name, 'enum') + '.' + (fileExtension || ''),
    content: compiledTemplate(en),
  }));
}

function handleScalar(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleScalar] called`);

  return schemaContext.scalars.map((scalar: Scalar) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(scalar.name, 'scalar') + '.' + (fileExtension || ''),
    content: compiledTemplate(scalar),
  }));
}

function handleInterface(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleInterface] called`);

  return schemaContext.interfaces.map((inf: Interface) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(inf.name, 'interface') + '.' + (fileExtension || ''),
    content: compiledTemplate(inf),
  }));
}

function handleOperation(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleOperation] called`);

  return documents.operations.map((operation: Operation) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(operation.name, operation.operationType) + '.' + (fileExtension || ''),
    content: compiledTemplate(operation),
  }));
}

function handleFragment(compiledTemplate: Function, schemaContext: SchemaTemplateContext, documents: Document, fileExtension: string, prefixAndPath: string = ''): FileOutput[] {
  debugLog(`[handleFragment] called`);

  return documents.fragments.map((fragment: Fragment) => ({
    filename: (prefixAndPath === '' ? '' : prefixAndPath + '.') + sanitizeFilename(fragment.name, 'fragment') + '.' + (fileExtension || ''),
    content: compiledTemplate(fragment),
  }));
}

function parseTemplateName(templateName: string): { prefix: string; handler: Function; fileExtension: string; } {
  const splitted = (path.basename(templateName)).split('.');

  if (splitted.length === 3) {
    splitted.unshift('');
  }

  if (splitted.length !== 4) {
    throw new Error(`Invalid template name: ${templateName}!`);
  }

  const templateExtension = splitted[3];

  if (templateExtension && ALLOWED_CUSTOM_TEMPLATE_EXT.includes(templateExtension)) {
    const compilationContext = splitted[2];
    const prefix = splitted[0];
    const fileExtension = splitted[1];
    const handler = handlersMap[compilationContext];

    if (handler) {
      return {
        prefix: path.resolve(path.dirname(templateName) + '/', prefix),
        handler,
        fileExtension,
      };
    }
  }

  return null;
}

export function generateMultipleFiles(templates: MultiFileTemplates, executionSettings: Settings, config: GeneratorConfig, templateContext: SchemaTemplateContext, documents: Document): FileOutput[] {
  debugLog(`[generateMultipleFiles] Compiling multiple files...`);
{'fraction_non_alphanumeric': 0.14913713514665153, 'fraction_numerical': 0.0008522122008380086, 'mean_word_length': 5.2035242290748895, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '257254', 'n_tokens_mistral': 3892, 'n_tokens_neox': 3883, 'n_words': 835}