import { isServer , setStoreItem } from '@cromwell/core' ; let normalizePath ; let nodeRequire ; let fs ; let requireFromString ; let coreBackendPaths ; let coreBackendCmsSettings ; let coreBackendAuthSettings ; let pathResolve ; let hasRequired = false ; const checkBackendModules = ( ) => { setStoreItem ( 'fsRequire' , fsRequire ) ; if ( hasRequired ) return ; hasRequired = true ; normalizePath = eval ( `require('normalize-path');` ) ; nodeRequire = ( name ) => eval ( `require('${ normalizePath ( name ) }');` ) ; fs = nodeRequire ( 'fs-extra' ) ; requireFromString = nodeRequire ( 'require-from-string' ) ; coreBackendPaths = nodeRequire ( '@cromwell/core-backend/dist/helpers/paths' ) ; coreBackendCmsSettings = nodeRequire ( '@cromwell/core-backend/dist/helpers/cms-settings' ) ; coreBackendAuthSettings = nodeRequire ( '@cromwell/core-backend/dist/helpers/auth-settings' ) ; pathResolve = nodeRequire ( 'path' ) . resolve ; } ; export const initRenderer = ( ) : void => { if ( ! isServer ( ) ) return undefined ; checkBackendModules ( ) ; const cmsConfigPath = coreBackendPaths . getCmsConfigPathSync ( ) ; const config = coreBackendCmsSettings . readCMSConfigSync ( cmsConfigPath ) ; setStoreItem ( 'cmsSettings' , config ) ; if ( ! config . serviceSecret ) { ( async ( ) => { try { let serverCachePath = pathResolve ( coreBackendPaths . getServerTempDir ( ) , 'cache' ) ; if ( ! fs . pathExistsSync ( serverCachePath ) ) { serverCachePath = pathResolve ( coreBackendPaths . getServerTempDir ( pathResolve ( process . cwd ( ) , '../../' ) ) , 'cache' ) ; } if ( fs . pathExistsSync ( serverCachePath ) ) { const authSettings = await coreBackendAuthSettings . getAuthSettings ( { serverCachePath , } ) ; config . serviceSecret = authSettings . serviceSecret ; setStoreItem ( 'cmsSettings' , config ) ; } } catch ( error ) { } } ) ( ) ; } } ; export const getPluginCjsPath = async ( pluginName ) : Promise < { cjsPath : string | undefined ; metaPath : string | undefined ; } | undefined > => { if ( ! isServer ( ) ) return undefined ; checkBackendModules ( ) ; const pluginDir = await coreBackendPaths . getNodeModuleDir ( pluginName ) ; if ( ! pluginDir ) return ; const buildDir = pathResolve ( pluginDir , coreBackendPaths . buildDirName ) ; let cjsPath = coreBackendPaths . getPluginFrontendCjsPath ( buildDir ) ; if ( cjsPath ) cjsPath = normalizePath ( cjsPath ) ; if ( cjsPath && ! ( await fs . pathExists ( cjsPath ) ) ) return undefined ; let metaPath = coreBackendPaths . getPluginFrontendMetaPath ( buildDir ) ; if ( ! ( await fs . pathExists ( metaPath ) ) ) metaPath = undefined ; return { cjsPath , metaPath , } ; } ; export const fsRequire = async ( path , json ? ) => { if ( ! isServer ( ) ) return undefined ; checkBackendModules ( ) ; if ( await fs . pathExists ( path ) ) { const str = ( await fs . readFile ( path ) ) . toString ( ) ; if ( json ) return JSON . parse ( str ) ; return requireFromString ( str ) ; } } ; setStoreItem ( 'fsRequire' , fsRequire ) ;