11import { LogType , fecLogger } from '@redhat-cloud-services/frontend-components-config-utilities' ;
2+ import path from 'path' ;
23import createConfig , { CreateConfigOptions } from './createConfig' ;
34import createPlugins , { CreatePluginsOptions } from './createPlugins' ;
5+ import { hasFEOFeaturesEnabled , readFrontendCRD } from '@redhat-cloud-services/frontend-components-config-utilities/feo/crd-check' ;
6+ import { FrontendCRD } from '@redhat-cloud-services/frontend-components-config-utilities/feo/feo-types' ;
47const { sync } = require ( 'glob' ) ;
58export * from './createConfig' ;
69export * from './createPlugins' ;
@@ -29,12 +32,13 @@ const getAppEntry = (rootFolder: string, isProd?: boolean) => {
2932 return `${ rootFolder } /${ entries [ 0 ] } ` ;
3033} ;
3134
32- type FecConfigurationOptions = Omit < CreateConfigOptions , 'publicPath' | 'appEntry' | 'appName' > &
33- CreatePluginsOptions & {
35+ type FecConfigurationOptions = Omit < CreateConfigOptions , 'publicPath' | 'appEntry' | 'appName' | 'cdnPath' > &
36+ Omit < CreatePluginsOptions , 'cdnPath' > & {
3437 deployment ?: string ;
3538 debug ?: boolean ;
3639 appEntry ?: string ;
3740 blockLegacyChrome ?: boolean ;
41+ publicPath ?: 'auto' ;
3842 } ;
3943
4044const createFecConfig = (
@@ -54,8 +58,27 @@ const createFecConfig = (
5458 gitBranch = 'main' ;
5559 }
5660 const appDeployment = typeof configurations . deployment === 'string' ? configurations . deployment : configurations . deployment || 'apps' ;
61+ const { frontendCRDPath = path . resolve ( configurations . rootFolder , 'deploy/frontend.yaml' ) } = configurations ;
5762
58- const publicPath = `/${ appDeployment } /${ insights . appname } /` ;
63+ const frontendCrdRef : { current ?: FrontendCRD } = { current : undefined } ;
64+ let FEOFeaturesEnabled = false ;
65+ try {
66+ frontendCrdRef . current = readFrontendCRD ( frontendCRDPath ) ;
67+ FEOFeaturesEnabled = hasFEOFeaturesEnabled ( frontendCrdRef . current ) ;
68+ } catch ( e ) {
69+ fecLogger (
70+ LogType . warn ,
71+ `FEO features are not enabled. Unable to find frontend CRD file at ${ frontendCRDPath } . If you want FEO features for local development, make sure to have a "deploy/frontend.yaml" file in your project or specify its location via "frontendCRDPath" attribute.`
72+ ) ;
73+ }
74+ let cdnPath : string ;
75+ // Could be written on a single line, but this is nice and readable
76+ if ( FEOFeaturesEnabled && configurations . publicPath === 'auto' && frontendCrdRef . current ) {
77+ // All service should eventually use this path
78+ cdnPath = `${ frontendCrdRef . current ?. objects [ 0 ] ?. spec . frontend . paths [ 0 ] } /` . replace ( / \/ \/ / , '/' ) ;
79+ } else {
80+ cdnPath = `/${ appDeployment } /${ insights . appname } /` ;
81+ }
5982 const appEntry = configurations . appEntry || getAppEntry ( configurations . rootFolder , isProd ) ;
6083 const generateSourceMaps = ! isProd ;
6184
@@ -66,7 +89,7 @@ const createFecConfig = (
6689 fecLogger ( LogType . debug , `Current branch: ${ gitBranch } ` ) ;
6790 ! generateSourceMaps && fecLogger ( LogType . debug , `Source map generation for "${ gitBranch } " deployment has been disabled.` ) ;
6891 fecLogger ( LogType . debug , `Using deployments: ${ appDeployment } ` ) ;
69- fecLogger ( LogType . debug , `Public path: ${ publicPath } ` ) ;
92+ fecLogger ( LogType . debug , `CDN path: ${ cdnPath } ` ) ;
7093 fecLogger ( LogType . debug , `App entry: ${ appEntry } ` ) ;
7194 fecLogger ( LogType . debug , `Use proxy: ${ configurations . useProxy ? 'true' : 'false' } ` ) ;
7295 if ( ! ( configurations . useProxy || configurations . standalone ) ) {
@@ -81,7 +104,8 @@ const createFecConfig = (
81104 return {
82105 config : createConfig ( {
83106 ...configurations ,
84- publicPath,
107+ cdnPath,
108+ publicPath : configurations . publicPath ,
85109 appEntry,
86110 appName : insights . appname ,
87111 } ) ,
0 commit comments