@@ -8,7 +8,7 @@ let { default: importCmd } = require('@contentstack/cli-cm-import');
88const { CustomAbortController } = require ( './abort-controller' ) ;
99const prompt = require ( 'prompt' ) ;
1010const colors = require ( '@colors/colors/safe' ) ;
11- const cloneDeep = require ( " lodash/cloneDeep" )
11+ const cloneDeep = require ( ' lodash/cloneDeep' ) ;
1212
1313const {
1414 HandleOrgCommand,
@@ -21,7 +21,7 @@ const {
2121 Clone,
2222 HandleBranchCommand,
2323} = require ( '../helpers/command-helpers' ) ;
24- const { configHandler } = require ( '@contentstack/cli-utilities' )
24+ const { configHandler, getBranchFromAlias } = require ( '@contentstack/cli-utilities' ) ;
2525
2626let client = { } ;
2727let config ;
@@ -137,20 +137,26 @@ class CloneHandler {
137137 let spinner ;
138138 try {
139139 const stackAPIClient = client . stack ( {
140- api_key : config . target_stack ? config . target_stack : config . source_stack ,
140+ api_key : isSource ? config . source_stack : config . target_stack ,
141141 management_token : config . management_token ,
142142 } ) ;
143143
144144 // NOTE validate if source branch is exist
145145 if ( isSource && config . sourceStackBranch ) {
146146 await this . validateIfBranchExist ( stackAPIClient , true ) ;
147147 return resolve ( ) ;
148+ } else if ( isSource && config . sourceStackBranchAlias ) {
149+ await this . resolveBranchAliases ( true ) ;
150+ return resolve ( ) ;
148151 }
149152
150153 // NOTE Validate target branch is exist
151154 if ( ! isSource && config . targetStackBranch ) {
152155 await this . validateIfBranchExist ( stackAPIClient , false ) ;
153156 return resolve ( ) ;
157+ } else if ( ! isSource && config . targetStackBranchAlias ) {
158+ await this . resolveBranchAliases ( ) ;
159+ return resolve ( ) ;
154160 }
155161 spinner = ora ( 'Fetching Branches' ) . start ( ) ;
156162 const result = await stackAPIClient
@@ -272,6 +278,8 @@ class CloneHandler {
272278 return reject ( 'Org not found.' ) ;
273279 }
274280 } else {
281+ this . setExectingCommand ( 2 ) ;
282+ await this . handleBranchSelection ( { api_key : config . sourceStack } ) ;
275283 const exportRes = await cloneCommand . execute ( new HandleExportCommand ( null , this ) ) ;
276284 await cloneCommand . execute ( new SetBranchCommand ( null , this ) ) ;
277285
@@ -471,7 +479,7 @@ class CloneHandler {
471479 } else {
472480 organizations = await client . organization ( ) . fetchAll ( { limit : 100 } ) ;
473481 }
474-
482+
475483 spinner . succeed ( 'Fetched Organization' ) ;
476484 for ( const element of organizations . items || [ organizations ] ) {
477485 orgUidList [ element . name ] = element . uid ;
@@ -579,6 +587,20 @@ class CloneHandler {
579587 } ) ;
580588 }
581589
590+ async resolveBranchAliases ( isSource = false ) {
591+ try {
592+ if ( isSource ) {
593+ const sourceStack = client . stack ( { api_key : config . source_stack } ) ;
594+ config . sourceStackBranch = await getBranchFromAlias ( sourceStack , config . sourceStackBranchAlias ) ;
595+ } else {
596+ const targetStack = client . stack ( { api_key : config . target_stack } ) ;
597+ config . targetStackBranch = await getBranchFromAlias ( targetStack , config . targetStackBranchAlias ) ;
598+ }
599+ } catch ( error ) {
600+ throw error ;
601+ }
602+ }
603+
582604 async cloneTypeSelection ( ) {
583605 console . clear ( ) ;
584606 return new Promise ( async ( resolve , reject ) => {
@@ -618,7 +640,7 @@ class CloneHandler {
618640 async cmdExport ( ) {
619641 return new Promise ( ( resolve , reject ) => {
620642 // Creating export specific config by merging external configurations
621- let exportConfig = Object . assign ( { } , cloneDeep ( config ) , { ...config ?. export } ) ;
643+ let exportConfig = Object . assign ( { } , cloneDeep ( config ) , { ...config ?. export } ) ;
622644 delete exportConfig . import ;
623645 delete exportConfig . export ;
624646
@@ -634,10 +656,6 @@ class CloneHandler {
634656 cmd . push ( '--branch' , exportConfig . sourceStackBranch ) ;
635657 }
636658
637- if ( ! exportConfig . sourceStackBranch && exportConfig . sourceStackBranchAlias ) {
638- cmd . push ( '--branch-alias' , exportConfig . sourceStackBranchAlias ) ;
639- }
640-
641659 if ( exportConfig . forceStopMarketplaceAppsPrompt ) cmd . push ( '-y' ) ;
642660
643661 cmd . push ( '-c' ) ;
@@ -652,7 +670,7 @@ class CloneHandler {
652670 async cmdImport ( ) {
653671 return new Promise ( async ( resolve , _reject ) => {
654672 // Creating export specific config by merging external configurations
655- let importConfig = Object . assign ( { } , cloneDeep ( config ) , { ...config ?. import } ) ;
673+ let importConfig = Object . assign ( { } , cloneDeep ( config ) , { ...config ?. import } ) ;
656674 delete importConfig . import ;
657675 delete importConfig . export ;
658676
@@ -667,10 +685,6 @@ class CloneHandler {
667685 if ( importConfig . targetStackBranch ) {
668686 cmd . push ( '--branch' , importConfig . targetStackBranch ) ;
669687 }
670-
671- if ( ! importConfig . targetStackBranch && importConfig . targetStackBranchAlias ) {
672- cmd . push ( '--branch-alias' , importConfig . targetStackBranchAlias ) ;
673- }
674688 if ( importConfig . importWebhookStatus ) {
675689 cmd . push ( '--import-webhook-status' , importConfig . importWebhookStatus ) ;
676690 }
@@ -681,7 +695,7 @@ class CloneHandler {
681695
682696 fs . writeFileSync ( path . join ( __dirname , 'dummyConfig.json' ) , JSON . stringify ( importConfig ) ) ;
683697 await importCmd . run ( cmd ) ;
684- fs . writeFileSync ( path . join ( __dirname , 'dummyConfig.json' ) , JSON . stringify ( { } ) )
698+ fs . writeFileSync ( path . join ( __dirname , 'dummyConfig.json' ) , JSON . stringify ( { } ) ) ;
685699 return resolve ( ) ;
686700 } ) ;
687701 }
0 commit comments