import path from 'path' ; import fs from 'fs-extra' ; import { normalizeOutputPath } from './path' ; const memo = < T extends ( ... args : any [ ] ) => any > ( fn ) => { const cache = new Map ( ) ; return ( ... params ) : ReturnType < T > => { const stringifiedParams = JSON . stringify ( params ) ; const cachedResult = cache . get ( stringifiedParams ) ; if ( cachedResult ) { return cachedResult ; } const res = fn ( ... params ) ; cache . set ( stringifiedParams , res ) ; return res ; } ; } ; export const createRuntimeExportsUtils = memo ( ( pwd = '' , namespace , ts = false ) => { const entryExportFile = path . join ( pwd , `.runtime-exports/${ namespace ? `${ namespace }.js` : 'index.js' }` ) ; const entryExportTsFile = path . join ( pwd , `.runtime-exports/${ namespace ? `${ namespace }.d.ts` : 'index.d.ts' }` ) ; const addExport = ( statement ) => { statement = normalizeOutputPath ( statement ) ; try { fs . ensureFileSync ( entryExportFile ) ; fs . ensureFileSync ( entryExportTsFile ) ; if ( ! fs . readFileSync ( entryExportFile , 'utf8' ) . includes ( statement ) ) { fs . appendFileSync ( entryExportFile , `${ statement }\n` ) ; ts && fs . appendFileSync ( entryExportTsFile , `${ statement . replace ( '.js' , '.d' ) }\n` ) ; } } catch { } } ; const getPath = ( ) => entryExportFile ; return { addExport , getPath , } ; } ) ;