import { promises } from 'fs' ; import { resolve } from 'path' ; import yargs from 'yargs' ; import { ErrorWithCause , createLogger } from '@sap-cloud-sdk/util' ; import { generatorOptions } from './options' ; const { readFile , lstat } = promises ; const logger = createLogger ( 'openapi-generator' ) ; /**
 * Options that can be used to configure the generation when using the generator programmatically.
 * The options match the CLI options.
 */ export interface GeneratorOptions { input : string ; outputDir : string ; transpile ? : boolean ; include ? : string ; overwrite ? : boolean ; clearOutputDir ? : boolean ; skipValidation ? : boolean ; tsConfig ? : string ; packageJson ? : boolean ; verbose ? : boolean ; optionsPerService ? : string ; packageVersion ? : string ; readme ? : boolean ; metadata ? : boolean ; config ? : string ; } internal export interface ParsedGeneratorOptions { input : string ; outputDir : string ; transpile : boolean ; include ? : string [ ] ; overwrite : boolean ; clearOutputDir : boolean ; skipValidation : boolean ; tsConfig ? : string ; packageJson : boolean ; verbose : boolean ; optionsPerService ? : string ; packageVersion : string ; readme : boolean ; metadata : boolean ; config ? : string ; } param options returns internal export function parseGeneratorOptions ( options ) { return Object . entries ( generatorOptions ) . reduce ( ( parsedOptions , [ name , flag ] ) => { const value = options [ name ] ; if ( typeof value !== 'undefined' ) { parsedOptions [ name ] = flag . coerce ? flag . coerce ( value ) : value ; } else if ( typeof flag . default !== 'undefined' ) { parsedOptions [ name ] = flag . default ; } return parsedOptions ; } , { } as ParsedGeneratorOptions ) ; } param configPath returns internal export async function parseOptionsFromConfig ( configPath ) { try { if ( ( await lstat ( configPath ) ) . isDirectory ( ) ) { configPath = resolve ( configPath , 'config.json' ) ; } const generatorOpts = JSON . parse ( await readFile ( configPath , 'utf8' ) ) ; Object . keys ( generatorOpts ) . forEach ( key => { if ( ! Object . keys ( generatorOptions ) . includes ( key ) ) { logger . warn ( `"${ key }" is not part of the configuration and will be ignored.` ) ; } } ) ; return parseGeneratorOptions ( generatorOpts ) ; } catch ( err ) { throw new ErrorWithCause ( `Could not read configuration file at ${ configPath }.` , err ) ; } } param options param rawInputFlags returns internal export function getSpecifiedFlags ( options , rawInputFlags ) { return rawInputFlags . reduce ( ( reducedOptions , name ) => { const value = options [ name ] ; if ( value !== undefined ) { reducedOptions [ name ] = value ; } return reducedOptions ; } , { } as ParsedGeneratorOptions ) ; }