import { join } from 'path' ; import { create as createStorage } from 'mem-fs' ; import { create , Editor } from 'mem-fs-editor' ; import { render } from 'ejs' ; import { ProxyBackend , UI5Config } from '@sap-ux/ui5-config' ; import prettifyXml from 'prettify-xml' ; import { enhanceData } from './data' ; import { t } from './i18n' ; import { OdataService , OdataVersion } from './types' ; param { string } basePath param { Editor } fs function validateBasePath ( basePath , fs ) { [ join ( basePath , 'package.json' ) , join ( basePath , 'webapp' , 'manifest.json' ) , join ( basePath , 'ui5.yaml' ) ] . forEach ( ( path ) => { if ( ! fs . exists ( path ) ) { throw new Error ( t ( 'error.requiredProjectFileNotFound' , { path } ) ) ; } } ) ; } param { string } basePath param { OdataService } data param { Editor } fs throws returns { Promise < Editor > } async function generate ( basePath , data , fs ? ) { if ( ! fs ) { fs = create ( createStorage ( ) ) ; } validateBasePath ( basePath , fs ) ; enhanceData ( data ) ; const templateRoot = join ( __dirname , '..' , 'templates' ) ; const extRoot = join ( templateRoot , 'extend' ) ; const manifestPath = join ( basePath , 'webapp' , 'manifest.json' ) ; const manifest = fs . readJSON ( manifestPath ) ; const appProp = 'sap.app' ; const appid = manifest ?. [ appProp ] ?. id ; if ( ! appid ) { throw new Error ( t ( 'error.requiredProjectPropertyNotFound' , { property : `'${ appProp }'.id` , path : manifestPath } ) ) ; } const manifestJsonExt = fs . read ( join ( extRoot , `manifest.json` ) ) ; fs . extendJSON ( manifestPath , JSON . parse ( render ( manifestJsonExt , data ) ) ) ; const ui5ConfigPath = join ( basePath , 'ui5.yaml' ) ; const ui5Config = await UI5Config . newInstance ( fs . read ( ui5ConfigPath ) ) ; ui5Config . addBackendToFioriToolsProxydMiddleware ( data . previewSettings as ProxyBackend ) ; const ui5LocalConfigPath = join ( basePath , 'ui5-local.yaml' ) ; const ui5LocalConfig = await UI5Config . newInstance ( fs . read ( ui5LocalConfigPath ) ) ; ui5LocalConfig . addFioriToolsProxydMiddleware ( { backend : [ data . previewSettings as ProxyBackend ] } ) ; if ( data . metadata ) { const mockDevDeps = { devDependencies : { '@sap/ux-ui5-fe-mockserver-middleware' : '1' } } ; const packagePath = join ( basePath , 'package.json' ) ; fs . extendJSON ( packagePath , mockDevDeps ) ; const packageJson = JSON . parse ( fs . read ( packagePath ) ) ; packageJson . ui5 . dependencies . push ( '@sap/ux-ui5-fe-mockserver-middleware' ) ; fs . writeJSON ( packagePath , packageJson ) ; const ui5MockConfig = await UI5Config . newInstance ( ui5Config . toString ( ) ) ; ui5MockConfig . addMockServerMiddleware ( data . path ) ; fs . write ( join ( basePath , 'ui5-mock.yaml' ) , ui5MockConfig . toString ( ) ) ; ui5LocalConfig . addMockServerMiddleware ( data . path ) ; fs . write ( join ( basePath , 'webapp' , 'localService' , 'metadata.xml' ) , prettifyXml ( data . metadata , { indent : 4 } ) ) ; } fs . write ( ui5ConfigPath , ui5Config . toString ( ) ) ; fs . write ( ui5LocalConfigPath , ui5LocalConfig . toString ( ) ) ; if ( data . annotations ?. xml ) { fs . write ( join ( basePath , 'webapp' , 'localService' , `${ data . annotations . technicalName }.xml` ) , prettifyXml ( data . annotations . xml , { indent : 4 } ) ) ; } return fs ; } export { generate , OdataVersion , OdataService } ;