@@ -4,6 +4,7 @@ import { cliux, pathValidator, sanitizePath } from '@contentstack/cli-utilities'
44import { continueBootstrapCommand } from '../bootstrap/interactive' ;
55import { AppConfig } from '../config' ;
66import messageHandler from '../messages' ;
7+ import { regions } from '@contentstack/cli-config/lib/utils/region-handler' ;
78
89interface EnvironmentVariables {
910 api_key : string ;
@@ -187,27 +188,59 @@ const envFileHandler = async (
187188 const cdnHost = region ?. cda ?. substring ( '8' ) ;
188189 const appHost = region ?. uiHost ?. substring ( 8 ) ;
189190 const isUSRegion = regionName === 'us' || regionName === 'na' ;
191+
192+ const isPredefinedRegion = region ?. name && Object . keys ( regions ) . some (
193+ key => key . toLowerCase ( ) === region . name . toLowerCase ( )
194+ ) ;
195+
190196 if ( regionName !== 'eu' && ! isUSRegion ) {
191197 customHost = region ?. cma ?. substring ( 8 ) ;
192198 }
193- let graphqlHost = "graphql.contentstack.com" ;
194- if ( regionName != 'na' ) {
195- graphqlHost = `${ regionName } -.graphql.contentstack.com` ;
196- }
197-
198199
199- // Construct image hostname based on the actual host being used
200- let imageHostname = '*-images.contentstack.com' ; // default fallback
201- if ( region ?. cda ) {
200+ const getGraphqlHost = ( ) : string => {
201+ if ( ! isPredefinedRegion ) {
202+ return cdnHost . replace ( '-cdn.' , '-graphql.' ) ;
203+ }
204+ const normalizedRegion = regionName ?. toLowerCase ( ) ;
205+ if ( ! normalizedRegion || normalizedRegion === 'na' || normalizedRegion === 'aws-na' || normalizedRegion === 'us' ) {
206+ return cdnHost . replace ( 'cdn.' , 'graphql.' ) . replace ( '.io' , '.com' ) ;
207+ }
208+ return cdnHost . replace ( '-cdn.' , '-graphql.' ) ;
209+ } ;
210+ const graphqlHost = getGraphqlHost ( ) ;
211+
212+ let imageHostname : string ;
213+ if ( isPredefinedRegion && region ?. cda ) {
202214 const baseHost = region . cda . replace ( / ^ h t t p s ? : \/ \/ / , '' ) . replace ( / ^ [ ^ . ] + \. / , '' ) ;
203215 imageHostname = `images.${ baseHost } ` ;
216+ } else if ( region ?. cda ) {
217+ const baseHost = region . cda . replace ( / ^ h t t p s ? : \/ \/ / , '' ) . replace ( / ^ [ ^ . ] + \. / , '' ) ;
218+ imageHostname = `*-images.${ baseHost } ` ;
219+ } else {
220+ //default
221+ imageHostname = '*-images.contentstack.com' ;
204222 }
205223 const production = environmentVariables . environment === 'production' ? true : false ;
206224 switch ( appConfigKey ) {
207225 case 'kickstart-next' :
208226 case 'kickstart-next-ssr' :
209227 case 'kickstart-next-ssg' :
210228 case 'kickstart-next-middleware' :
229+ fileName = `.env` ;
230+ filePath = pathValidator ( path . join ( sanitizePath ( clonedDirectory ) , sanitizePath ( fileName ) ) ) ;
231+ content = `NEXT_PUBLIC_CONTENTSTACK_API_KEY=${ environmentVariables . api_key
232+ } \nNEXT_PUBLIC_CONTENTSTACK_DELIVERY_TOKEN=${ environmentVariables . deliveryToken
233+ } \nNEXT_PUBLIC_CONTENTSTACK_PREVIEW_TOKEN=${ environmentVariables . preview_token || ''
234+ } \nNEXT_PUBLIC_CONTENTSTACK_ENVIRONMENT=${ environmentVariables . environment
235+ } \nNEXT_PUBLIC_CONTENTSTACK_REGION=${ regionName
236+ } \nNEXT_PUBLIC_CONTENTSTACK_PREVIEW=${ livePreviewEnabled ? 'true' : 'false'
237+ } \nNEXT_PUBLIC_CONTENTSTACK_CONTENT_DELIVERY = ${ cdnHost
238+ } \nNEXT_PUBLIC_CONTENTSTACK_CONTENT_APPLICATION = ${ appHost
239+ } \nNEXT_PUBLIC_CONTENTSTACK_PREVIEW_HOST = ${ previewHost
240+ } \nNEXT_PUBLIC_CONTENTSTACK_IMAGE_HOSTNAME=${ imageHostname } `;
241+
242+ result = await writeEnvFile ( content , filePath ) ;
243+ break ;
211244 case 'kickstart-next-graphql' :
212245 fileName = `.env` ;
213246 filePath = pathValidator ( path . join ( sanitizePath ( clonedDirectory ) , sanitizePath ( fileName ) ) ) ;
0 commit comments