import path from 'path' ; import fs from 'fs' ; import type { CommonOptions , ConfigFile , GenerationOptions , OutputFileOptions } from './types' ; import { isValidUrl , prettify } from './utils' ; export { ConfigFile } from './types' ; export async function generateEndpoints ( options ) { const schemaLocation = options . schemaFile ; const schemaAbsPath = isValidUrl ( options . schemaFile ) ? options . schemaFile : path . resolve ( process . cwd ( ) , schemaLocation ) ; const sourceCode = await enforceOazapftsTsVersion ( ( ) => { const { generateApi } = require ( './generate' ) ; return generateApi ( schemaAbsPath , options ) ; } ) ; const outputFile = options . outputFile ; if ( outputFile ) { fs . writeFileSync ( path . resolve ( process . cwd ( ) , outputFile ) , await prettify ( outputFile , sourceCode ) ) ; } else { return await prettify ( null , sourceCode ) ; } } export function parseConfig ( fullConfig ) { const outFiles = [ ] ; if ( 'outputFiles' in fullConfig ) { const { outputFiles , ... commonConfig } = fullConfig ; for ( const [ outputFile , specificConfig ] of Object . entries ( outputFiles ) ) { outFiles . push ( { ... commonConfig , ... specificConfig , outputFile , } ) ; } } else { outFiles . push ( fullConfig ) ; } return outFiles ; } /**
 * Enforces `oazapfts` to use the same TypeScript version as this module itself uses.
 * That should prevent enums from running out of sync if both libraries use different TS versions.
 */ function enforceOazapftsTsVersion < T > ( cb ) { const ozTsPath = require . resolve ( 'typescript' , { paths : [ require . resolve ( 'oazapfts' ) ] } ) ; const tsPath = require . resolve ( 'typescript' ) ; const originalEntry = require . cache [ ozTsPath ] ; try { require . cache [ ozTsPath ] = require . cache [ tsPath ] ; return cb ( ) ; } finally { if ( originalEntry ) { require . cache [ ozTsPath ] = originalEntry ; } else { delete require . cache [ ozTsPath ] ; } } }