import { existsSync , promises } from 'fs' ; import { parse } from 'path' ; import { unique , UniqueNameGenerator } from '@sap-cloud-sdk/util' ; import { getRelPathWithPosixSeparator } from '../generator' ; import { ParsedGeneratorOptions } from './generator-options' ; const { readFile } = promises ; internal export type OptionsPerService = Record < string , ServiceOptions > ; /**
 * Partial OptionsPerService
 */ type PartialOptionsPerService = Record < string , Partial < ServiceOptions > > ; internal export interface ServiceOptions { /**
     * Name of the directory to store the service in.
     */ directoryName : string ; /**
     * Name of the package to reference in the package.json.
     */ packageName : string ; /**
     * Human readable name of the service. Used in API documentation and readme.
     */ serviceName : string ; } param configPath returns internal export async function getOriginalOptionsPerService ( configPath ) { return configPath && existsSync ( configPath ) ? JSON . parse ( await readFile ( configPath , 'utf8' ) ) : { } ; } param inputPaths param options returns internal export async function getOptionsPerService ( inputPaths , { optionsPerService , skipValidation } ) { const originalOptionsPerService = await getOriginalOptionsPerService ( optionsPerService ) ; const uniqueNameGenerator = new UniqueNameGenerator ( '-' ) ; const directoryNamesByPaths = getDirectoryNamesByPaths ( inputPaths , originalOptionsPerService ) ; if ( ! skipValidation ) { validateDirectoryNames ( directoryNamesByPaths ) ; } const optsPerService = inputPaths . reduce ( ( previousOptions , inputPath ) => { const relativePath = getRelPathWithPosixSeparator ( inputPath ) ; const uniqueDirName = uniqueNameGenerator . generateAndSaveUniqueName ( directoryNamesByPaths [ inputPath ] ) ; previousOptions [ relativePath ] = getServiceOptions ( uniqueDirName , originalOptionsPerService [ relativePath ] ) ; return previousOptions ; } , { } ) ; return optsPerService ; } function getDirectoryNamesByPaths ( inputPaths , originalOptionsPerService ) { return inputPaths . reduce ( ( directoryNamesByPaths , inputPath ) => { const relativePath = getRelPathWithPosixSeparator ( inputPath ) ; const directoryName = originalOptionsPerService [ relativePath ] ?. directoryName || parseDirectoryName ( relativePath ) ; directoryNamesByPaths [ inputPath ] = directoryName ; return directoryNamesByPaths ; } , { } ) ; } function getPathsByDirName ( dirNamesByPaths ) { return Object . entries ( dirNamesByPaths ) . reduce ( ( pathsByDirName , [ inputPath , dirName ] ) => { if ( ! pathsByDirName [ dirName ] ) { pathsByDirName [ dirName ] = [ ] ; } pathsByDirName [ dirName ] . push ( inputPath ) ; return pathsByDirName ; } , { } ) ; } function validateDirectoryNames ( dirNamesByPaths ) { const originalDirNames = Object . values ( dirNamesByPaths ) ; const uniqueDirNames = unique ( originalDirNames ) ; const hasDuplicates = originalDirNames . length !== uniqueDirNames . length ; if ( hasDuplicates ) { const pathsByDirName = getPathsByDirName ( dirNamesByPaths ) ; const duplicates = Object . entries ( pathsByDirName ) . filter ( ( [ , paths ] ) => paths . length > 1 ) ; const duplicatesList = duplicates . map ( ( [ dirName , paths ] ) => `\t\tDirectory name: '${ dirName }', specifications: [\n${ paths . map ( path => `\t\t\t${ path }` ) . join ( ',\n' ) }\n\t\t]` ) . join ( '\n' ) ; const errorMessage = `Duplicate service directory names found. Customize directory names with \`optionsPerService\` or enable automatic name adjustment with \`skipValidation\`.\n\tDuplicates:\n${ duplicatesList }` ; throw new Error ( errorMessage ) ; } } param filePath returns function parseDirectoryName ( filePath ) { return parse ( filePath ) . name . replace ( /-openapi$/ , '' ) ; } param directoryName param serviceOptions returns internal export function getServiceOptions ( directoryName , serviceOptions ? ) { const defaultConfig = { packageName : directoryName , directoryName , serviceName : directoryName } ; return { ... defaultConfig , ... serviceOptions , directoryName } ; }