@@ -19,7 +19,7 @@ interface EnviornmentVariables {
1919 * Create delivery token
2020 * Create enviroment
2121 */
22-
22+ let managementTokenResult : any ;
2323export const setupEnvironments = async (
2424 managementAPIClient : any ,
2525 api_key : string ,
@@ -34,6 +34,51 @@ export const setupEnvironments = async (
3434 . environment ( )
3535 . query ( )
3636 . find ( ) ;
37+
38+ //create management token if not present
39+ if ( ! managementToken ) {
40+ const managementBody = {
41+ "token" :{
42+ "name" :"sample app" ,
43+ "description" :"This is a sample management token." ,
44+ "scope" :[
45+ {
46+ "module" :"content_type" ,
47+ "acl" :{
48+ "read" :true ,
49+ "write" :true
50+ }
51+ } ,
52+ {
53+ "module" :"branch" ,
54+ "branches" :[
55+ "main"
56+ ] ,
57+ "acl" :{
58+ "read" :true
59+ }
60+ }
61+ ] ,
62+ "expires_on" : "3000-01-01" ,
63+ "is_email_notification_enabled" :false
64+ }
65+ }
66+ managementTokenResult = await managementAPIClient
67+ . stack ( { api_key : api_key } )
68+ . managementToken ( )
69+ . create ( managementBody ) ;
70+ if ( ! managementTokenResult . uid ) {
71+ cliux . print (
72+ `Info: Failed to generate a management token.\nNote: Management token is not available in your plan. Please contact the admin for support.` ,
73+ {
74+ color : 'yellow' ,
75+ } ,
76+ ) ;
77+ if ( ( await continueBootstrapCommand ( ) ) === 'no' ) {
78+ return ;
79+ }
80+ }
81+ }
3782 if ( Array . isArray ( environmentResult . items ) && environmentResult . items . length > 0 ) {
3883 for ( const environment of environmentResult . items ) {
3984 if ( environment . name ) {
@@ -144,6 +189,7 @@ const envFileHandler = async (
144189 const managementAPIHost = region ?. cma ?. substring ( '8' ) ;
145190 const regionName = region && region . name && region . name . toLowerCase ( ) ;
146191 previewHost = region ?. uiHost ?. substring ( 8 ) ?. replace ( 'app' , 'rest-preview' ) ;
192+ const cdnHost = region ?. cda ?. substring ( '8' ) ;
147193 appHost = region ?. uiHost ?. substring ( 8 ) ;
148194 const isUSRegion = regionName === 'us' || regionName === 'na' ;
149195 if ( regionName !== 'eu' && ! isUSRegion ) {
@@ -194,19 +240,20 @@ const envFileHandler = async (
194240 filePath = pathValidator ( path . join ( sanitizePath ( clonedDirectory ) , sanitizePath ( fileName ) ) ) ;
195241 content = `CONTENTSTACK_API_KEY=${ environmentVariables . api_key } \nCONTENTSTACK_DELIVERY_TOKEN=${
196242 environmentVariables . deliveryToken
197- } \n ${
243+ } \nCONTENTSTACK_BRANCH=main ${
198244 livePreviewEnabled
199245 ? `\nCONTENTSTACK_PREVIEW_TOKEN=${
200246 environmentVariables . preview_token || `''`
201- } \nCONTENTSTACK_PREVIEW_HOST=${ previewHost } \nCONTENTSTACK_APP_HOST= ${ appHost } \ n`
247+ } \nCONTENTSTACK_PREVIEW_HOST=${ previewHost } \n`
202248 : '\n'
203- } CONTENTSTACK_ENVIRONMENT=${ environmentVariables . environment } \nCONTENTSTACK_API_HOST=${
249+ } CONTENTSTACK_ENVIRONMENT=${ environmentVariables . environment } ${
250+ ! isUSRegion && ! customHost ? '\nCONTENTSTACK_REGION=' + region . name : ''
251+ } \nCONTENTSTACK_LIVE_PREVIEW=${ livePreviewEnabled } \nCONTENTSTACK_LIVE_EDIT_TAGS=false\nCONTENTSTACK_API_HOST=${
204252 customHost ? customHost : managementAPIHost
205253 } ${
206254 ! isUSRegion && ! customHost ? '\nCONTENTSTACK_REGION=' + region . name : ''
207- } \nCONTENTSTACK_LIVE_PREVIEW =${ livePreviewEnabled } \nCONTENTSTACK_LIVE_EDIT_TAGS=false `;
255+ } \nCONTENTSTACK_APP_HOST =${ appHost } \nCONTENTSTACK_MANAGEMENT_TOKEN= ${ managementTokenResult . uid } \nCONTENTSTACK_HOST= ${ cdnHost } `;
208256 result = await writeEnvFile ( content , filePath ) ;
209- break ;
210257 case 'gatsby' :
211258 case 'gatsby-starter' :
212259 fileName = `.env.${ environmentVariables . environment } ` ;
0 commit comments