1- import startCase from 'lodash/startCase' ;
2- import camelCase from 'lodash/camelCase' ;
31import { cliux } from '@contentstack/cli-utilities' ;
2+ import camelCase from 'lodash/camelCase' ;
3+ import startCase from 'lodash/startCase' ;
4+
5+ import { BranchDiffPayload , BranchOptions } from '../interfaces' ;
46import { getbranchConfig } from '../utils' ;
5- import { BranchOptions , BranchDiffPayload } from '../interfaces' ;
6- import { askBaseBranch , askCompareBranch , askStackAPIKey , selectModule } from '../utils/interactive' ;
77import {
88 fetchBranchesDiff ,
9- parseSummary ,
10- printSummary ,
9+ filterBranchDiffDataByModule ,
1110 parseCompactText ,
12- printCompactTextView ,
11+ parseSummary ,
1312 parseVerbose ,
13+ printCompactTextView ,
14+ printSummary ,
1415 printVerboseTextView ,
15- filterBranchDiffDataByModule ,
1616} from '../utils/branch-diff-utility' ;
1717import { exportCSVReport } from '../utils/csv-utility' ;
18+ import { askBaseBranch , askCompareBranch , askStackAPIKey , selectModule } from '../utils/interactive' ;
1819
1920export default class BranchDiffHandler {
2021 private options : BranchOptions ;
@@ -23,46 +24,36 @@ export default class BranchDiffHandler {
2324 this . options = params ;
2425 }
2526
26- async run ( ) : Promise < any > {
27- await this . validateMandatoryFlags ( ) ;
28- await this . initBranchDiffUtility ( ) ;
29- }
30-
3127 /**
32- * @methods validateMandatoryFlags - validate flags and prompt to select required flags
33- * @returns {* } {Promise< void> }
28+ * @methods displayBranchDiffTextAndVerbose - to show branch differences in compactText or detailText format
29+ * @returns {* } {void}
3430 * @memberof BranchDiff
3531 */
36- async validateMandatoryFlags ( ) : Promise < void > {
37- let baseBranch : string ;
38- if ( ! this . options . stackAPIKey ) {
39- this . options . stackAPIKey = await askStackAPIKey ( ) ;
40- }
41-
42- if ( ! this . options . baseBranch ) {
43- baseBranch = getbranchConfig ( this . options . stackAPIKey ) ;
44- if ( ! baseBranch ) {
45- this . options . baseBranch = await askBaseBranch ( ) ;
46- } else {
47- this . options . baseBranch = baseBranch ;
48- }
49- }
50-
51- if ( ! this . options . compareBranch ) {
52- this . options . compareBranch = await askCompareBranch ( ) ;
53- }
54-
55- if ( ! this . options . module ) {
56- this . options . module = await selectModule ( ) ;
57- }
32+ async displayBranchDiffTextAndVerbose ( branchDiffData : any [ ] , payload : BranchDiffPayload ) : Promise < void > {
33+ const spinner = cliux . loaderV2 ( 'Loading branch differences...' ) ;
34+ if ( this . options . format === 'compact-text' ) {
35+ const branchTextRes = parseCompactText ( branchDiffData ) ;
36+ cliux . loaderV2 ( '' , spinner ) ;
37+ printCompactTextView ( branchTextRes ) ;
38+ } else if ( this . options . format === 'detailed-text' ) {
39+ const verboseRes = await parseVerbose ( branchDiffData , payload ) ;
40+ cliux . loaderV2 ( '' , spinner ) ;
41+ printVerboseTextView ( verboseRes ) ;
5842
59- if ( this . options . format === 'detailed-text' && ! this . options . csvPath ) {
60- this . options . csvPath = process . cwd ( ) ;
43+ exportCSVReport ( payload . module , verboseRes , this . options . csvPath ) ;
6144 }
45+ }
6246
63- if ( baseBranch ) {
64- cliux . print ( `\nBase branch: ${ baseBranch } ` , { color : 'grey' } ) ;
65- }
47+ /**
48+ * @methods displaySummary - show branches summary on CLI
49+ * @returns {* } {void}
50+ * @memberof BranchDiff
51+ */
52+ displaySummary ( branchDiffData : any [ ] , module : string ) : void {
53+ cliux . print ( ' ' ) ;
54+ cliux . print ( `${ startCase ( camelCase ( module ) ) } Summary:` , { color : 'yellow' } ) ;
55+ const diffSummary = parseSummary ( branchDiffData , this . options . baseBranch , this . options . compareBranch ) ;
56+ printSummary ( diffSummary ) ;
6657 }
6758
6859 /**
@@ -73,11 +64,11 @@ export default class BranchDiffHandler {
7364 async initBranchDiffUtility ( ) : Promise < void > {
7465 const spinner = cliux . loaderV2 ( 'Loading branch differences...' ) ;
7566 const payload : BranchDiffPayload = {
76- module : '' ,
7767 apiKey : this . options . stackAPIKey ,
7868 baseBranch : this . options . baseBranch ,
7969 compareBranch : this . options . compareBranch ,
80- host : this . options . host
70+ host : this . options . host ,
71+ module : ''
8172 } ;
8273
8374 if ( this . options . module === 'content-types' ) {
@@ -91,7 +82,7 @@ export default class BranchDiffHandler {
9182 cliux . loaderV2 ( '' , spinner ) ;
9283
9384 if ( this . options . module === 'all' ) {
94- for ( let module in diffData ) {
85+ for ( const module in diffData ) {
9586 const branchDiff = diffData [ module ] ;
9687 payload . module = module ;
9788 this . displaySummary ( branchDiff , module ) ;
@@ -104,35 +95,45 @@ export default class BranchDiffHandler {
10495 }
10596 }
10697
107- /**
108- * @methods displaySummary - show branches summary on CLI
109- * @returns {* } {void}
110- * @memberof BranchDiff
111- */
112- displaySummary ( branchDiffData : any [ ] , module : string ) : void {
113- cliux . print ( ' ' ) ;
114- cliux . print ( `${ startCase ( camelCase ( module ) ) } Summary:` , { color : 'yellow' } ) ;
115- const diffSummary = parseSummary ( branchDiffData , this . options . baseBranch , this . options . compareBranch ) ;
116- printSummary ( diffSummary ) ;
98+ async run ( ) : Promise < any > {
99+ await this . validateMandatoryFlags ( ) ;
100+ await this . initBranchDiffUtility ( ) ;
117101 }
118102
119103 /**
120- * @methods displayBranchDiffTextAndVerbose - to show branch differences in compactText or detailText format
121- * @returns {* } {void}
104+ * @methods validateMandatoryFlags - validate flags and prompt to select required flags
105+ * @returns {* } {Promise< void> }
122106 * @memberof BranchDiff
123107 */
124- async displayBranchDiffTextAndVerbose ( branchDiffData : any [ ] , payload : BranchDiffPayload ) : Promise < void > {
125- const spinner = cliux . loaderV2 ( 'Loading branch differences...' ) ;
126- if ( this . options . format === 'compact-text' ) {
127- const branchTextRes = parseCompactText ( branchDiffData ) ;
128- cliux . loaderV2 ( '' , spinner ) ;
129- printCompactTextView ( branchTextRes ) ;
130- } else if ( this . options . format === 'detailed-text' ) {
131- const verboseRes = await parseVerbose ( branchDiffData , payload ) ;
132- cliux . loaderV2 ( '' , spinner ) ;
133- printVerboseTextView ( verboseRes ) ;
108+ async validateMandatoryFlags ( ) : Promise < void > {
109+ let baseBranch : string ;
110+ if ( ! this . options . stackAPIKey ) {
111+ this . options . stackAPIKey = await askStackAPIKey ( ) ;
112+ }
134113
135- exportCSVReport ( payload . module , verboseRes , this . options . csvPath ) ;
114+ if ( ! this . options . baseBranch ) {
115+ baseBranch = getbranchConfig ( this . options . stackAPIKey ) ;
116+ if ( ! baseBranch ) {
117+ this . options . baseBranch = await askBaseBranch ( ) ;
118+ } else {
119+ this . options . baseBranch = baseBranch ;
120+ }
121+ }
122+
123+ if ( ! this . options . compareBranch ) {
124+ this . options . compareBranch = await askCompareBranch ( ) ;
125+ }
126+
127+ if ( ! this . options . module ) {
128+ this . options . module = await selectModule ( ) ;
129+ }
130+
131+ if ( this . options . format === 'detailed-text' && ! this . options . csvPath ) {
132+ this . options . csvPath = process . cwd ( ) ;
133+ }
134+
135+ if ( baseBranch ) {
136+ cliux . print ( `\nBase branch: ${ baseBranch } ` , { color : 'grey' } ) ;
136137 }
137138 }
138139}
0 commit comments