import { GeneratorOptions } from './generator-options' ; import { npmCompliantName } from './generator-utils' ; import { GlobalNameFormatter } from './global-name-formatter' ; import { inputPaths , ServiceDefinitionPaths } from './input-path-provider' ; import { getServicePath , readServiceMapping , VdmMapping } from './service-mapping' ; import { ServiceNameFormatter } from './service-name-formatter' ; import { readEdmxFile , ServiceMetadata } from './edmx-parser/edmx-file-reader' ; import { readSwaggerFile } from './swagger-parser/swagger-parser' ; import { apiBusinessHubMetadata } from './swagger-parser/swagger-util' ; import { VdmServiceMetadata , VdmServicePackageMetaData } from './vdm-types' ; import { isV2Metadata } from './edmx-to-vdm/edmx-to-vdm-util' ; import { getServiceEntitiesV2 } from './edmx-to-vdm/v2' ; import { getServiceEntitiesV4 } from './edmx-to-vdm/v4' ; class ServiceGenerator { private globalNameFormatter : GlobalNameFormatter ; private serviceMapping : VdmMapping ; constructor ( readonly options ) { this . serviceMapping = readServiceMapping ( options ) ; this . globalNameFormatter = new GlobalNameFormatter ( this . serviceMapping ) ; } public withServiceMapping ( serviceMapping ) { this . serviceMapping = serviceMapping ; return this ; } public withGlobalNameFormatter ( globalNameFormatter ) { this . globalNameFormatter = globalNameFormatter ; return this ; } public generateAllServices ( ) { return inputPaths ( this . options . inputDir , this . options . useSwagger ) . map ( p => this . generateService ( p ) ) ; } public generateService ( serviceDefinitionPaths ) { const serviceMetadata = this . readEdmxAndSwaggerFile ( serviceDefinitionPaths ) ; const vdmServicePackageMetaData = this . getServicePackageMetaData ( serviceMetadata , serviceDefinitionPaths ) ; const vdmServiceEntities = isV2Metadata ( serviceMetadata . edmx ) ? getServiceEntitiesV2 ( serviceMetadata , vdmServicePackageMetaData . className ) : getServiceEntitiesV4 ( serviceMetadata , vdmServicePackageMetaData . className ) ; return { ... vdmServicePackageMetaData , ... vdmServiceEntities } ; } private getServicePackageMetaData ( serviceMetadata , serviceDefinitionPaths ) { const directoryName = this . globalNameFormatter . uniqueDirectoryName ( ServiceNameFormatter . originalToServiceName ( serviceMetadata . edmx . namespaces . length === 1 ? serviceMetadata . edmx . namespaces [ 0 ] : serviceMetadata . edmx . fileName ) , serviceMetadata . edmx . fileName ) ; const npmPackageName = this . globalNameFormatter . uniqueNpmPackageName ( npmCompliantName ( directoryName ) , serviceMetadata . edmx . fileName ) ; const speakingModuleName = ServiceNameFormatter . directoryToSpeakingModuleName ( directoryName ) ; const className = `${ speakingModuleName . replace ( / /g , '' ) }` ; return { oDataVersion : serviceMetadata . edmx . oDataVersion , namespaces : serviceMetadata . edmx . namespaces , originalFileName : serviceMetadata . edmx . fileName , directoryName , npmPackageName , speakingModuleName , servicePath : getServicePath ( serviceMetadata , this . serviceMapping [ serviceMetadata . edmx . fileName ] ) , edmxPath : serviceDefinitionPaths . edmxPath , apiBusinessHubMetadata : apiBusinessHubMetadata ( serviceMetadata . swagger ) , className } ; } private readEdmxAndSwaggerFile ( serviceDefinitionPaths ) { const serviceMetadata = { edmx : readEdmxFile ( serviceDefinitionPaths . edmxPath ) } ; if ( serviceDefinitionPaths . swaggerPath ) { serviceMetadata . swagger = readSwaggerFile ( serviceDefinitionPaths . swaggerPath ) ; } return serviceMetadata ; } } param options returns internal export function parseAllServices ( options ) { return new ServiceGenerator ( options ) . generateAllServices ( ) ; } param serviceDefinitionPaths param options param mappings param globalNameFormatter returns internal export function parseService ( serviceDefinitionPaths , options , mappings , globalNameFormatter ) { return new ServiceGenerator ( options ) . withServiceMapping ( mappings ) . withGlobalNameFormatter ( globalNameFormatter ) . generateService ( serviceDefinitionPaths ) ; } internal export function getServiceName ( service ) { return service . namespaces . length === 1 ? service . namespaces [ 0 ] : service . originalFileName ; }