11import startCase from 'lodash/startCase' ;
22import camelCase from 'lodash/camelCase' ;
33import { cliux } from '@contentstack/cli-utilities' ;
4- import { getbranchConfig } from '../utils' ;
5- import { BranchOptions , BranchDiffPayload } from '../interfaces' ;
4+ import { getbranchConfig , cleanupSession } from '../utils' ;
5+ import { BranchCompareCacheRef , BranchOptions , BranchCompactTextRes , BranchDiffPayload } from '../interfaces' ;
66import { askBaseBranch , askCompareBranch , askStackAPIKey , selectModule } from '../utils/interactive' ;
77import {
88 fetchBranchesDiff ,
@@ -13,7 +13,14 @@ import {
1313 parseVerbose ,
1414 printVerboseTextView ,
1515 filterBranchDiffDataByModule ,
16+ isBranchCompareCacheRef ,
17+ isInlineBranchCompareResult ,
18+ summaryFromCompact ,
19+ flatCompactToRawArray ,
20+ parseSummaryFromJsonlPath ,
21+ printCompactTextViewFromJsonlModulePath ,
1622} from '../utils/branch-diff-utility' ;
23+ import { readAllJsonLines } from '../utils/cache-manager' ;
1724import { exportCSVReport } from '../utils/csv-utility' ;
1825
1926export default class BranchDiffHandler {
@@ -24,8 +31,15 @@ export default class BranchDiffHandler {
2431 }
2532
2633 async run ( ) : Promise < any > {
27- await this . validateMandatoryFlags ( ) ;
28- await this . initBranchDiffUtility ( ) ;
34+ let cacheRef : BranchCompareCacheRef | undefined ;
35+ try {
36+ await this . validateMandatoryFlags ( ) ;
37+ cacheRef = await this . initBranchDiffUtility ( ) ;
38+ } finally {
39+ if ( cacheRef ?. kind === 'cache' ) {
40+ await cleanupSession ( cacheRef . sessionId ) . catch ( ( ) => undefined ) ;
41+ }
42+ }
2943 }
3044
3145 /**
@@ -40,7 +54,7 @@ export default class BranchDiffHandler {
4054 }
4155
4256 if ( ! this . options . baseBranch ) {
43- baseBranch = getbranchConfig ( this . options . stackAPIKey ) ;
57+ baseBranch = getbranchConfig ( this . options . stackAPIKey ) ;
4458 if ( ! baseBranch ) {
4559 this . options . baseBranch = await askBaseBranch ( ) ;
4660 } else {
@@ -60,24 +74,24 @@ export default class BranchDiffHandler {
6074 this . options . csvPath = process . cwd ( ) ;
6175 }
6276
63- if ( baseBranch ) {
77+ if ( baseBranch ) {
6478 cliux . print ( `\nBase branch: ${ baseBranch } ` , { color : 'grey' } ) ;
6579 }
6680 }
6781
6882 /**
6983 * @methods initBranchDiffUtility - call utility function to load data and display it
70- * @returns {* } {Promise<void >}
84+ * @returns {* } {Promise<BranchCompareCacheRef | undefined >}
7185 * @memberof BranchDiff
7286 */
73- async initBranchDiffUtility ( ) : Promise < void > {
87+ async initBranchDiffUtility ( ) : Promise < BranchCompareCacheRef | undefined > {
7488 const spinner = cliux . loaderV2 ( 'Loading branch differences...' ) ;
7589 const payload : BranchDiffPayload = {
7690 module : '' ,
7791 apiKey : this . options . stackAPIKey ,
7892 baseBranch : this . options . baseBranch ,
7993 compareBranch : this . options . compareBranch ,
80- host : this . options . host
94+ host : this . options . host ,
8195 } ;
8296
8397 if ( this . options . module === 'content-types' ) {
@@ -86,21 +100,107 @@ export default class BranchDiffHandler {
86100 payload . module = 'global_fields' ;
87101 }
88102 payload . spinner = spinner ;
89- const branchDiffData = await fetchBranchesDiff ( payload ) ;
90- const diffData = filterBranchDiffDataByModule ( branchDiffData ) ;
103+ const fetchResult = await fetchBranchesDiff ( payload ) ;
91104 cliux . loaderV2 ( '' , spinner ) ;
92-
93- if ( this . options . module === 'all' ) {
94- for ( let module in diffData ) {
95- const branchDiff = diffData [ module ] ;
96- payload . module = module ;
97- this . displaySummary ( branchDiff , module ) ;
98- await this . displayBranchDiffTextAndVerbose ( branchDiff , payload ) ;
99- }
100- } else {
101- const branchDiff = diffData [ payload . module ] ;
102- this . displaySummary ( branchDiff , this . options . module ) ;
105+
106+ if ( isBranchCompareCacheRef ( fetchResult ) ) {
107+ await this . displayFromCacheRef ( fetchResult , payload ) ;
108+ return fetchResult ;
109+ }
110+
111+ if ( isInlineBranchCompareResult ( fetchResult ) ) {
112+ await this . displayFromInlineResult ( fetchResult , payload ) ;
113+ return undefined ;
114+ }
115+
116+ const branchDiffData = fetchResult ;
117+ const diffData = filterBranchDiffDataByModule ( branchDiffData ) ;
118+
119+ if ( this . options . module === 'all' ) {
120+ for ( const module in diffData ) {
121+ const branchDiff = diffData [ module ] ;
122+ payload . module = module ;
123+ this . displaySummary ( branchDiff , module ) ;
103124 await this . displayBranchDiffTextAndVerbose ( branchDiff , payload ) ;
125+ }
126+ } else {
127+ const branchDiff = diffData [ payload . module ] ;
128+ this . displaySummary ( branchDiff , this . options . module ) ;
129+ await this . displayBranchDiffTextAndVerbose ( branchDiff , payload ) ;
130+ }
131+ return undefined ;
132+ }
133+
134+ private async displayFromCacheRef ( cacheRef : BranchCompareCacheRef , payload : BranchDiffPayload ) : Promise < void > {
135+ const modulesToShow =
136+ this . options . module === 'all'
137+ ? ( [ 'content_types' , 'global_fields' ] as const )
138+ : payload . module === 'content_types'
139+ ? ( [ 'content_types' ] as const )
140+ : ( [ 'global_fields' ] as const ) ;
141+
142+ for ( const module of modulesToShow ) {
143+ const modulePath = cacheRef . paths [ module ] ;
144+ const displayModule = module === 'content_types' ? 'content-types' : 'global-fields' ;
145+ payload . module = module ;
146+ cliux . print ( ' ' ) ;
147+ cliux . print ( `${ startCase ( camelCase ( module ) ) } Summary:` , { color : 'yellow' } ) ;
148+ const diffSummary = await parseSummaryFromJsonlPath (
149+ modulePath ,
150+ this . options . baseBranch ,
151+ this . options . compareBranch ,
152+ ) ;
153+ printSummary ( diffSummary ) ;
154+ const spinner1 = cliux . loaderV2 ( 'Loading branch differences...' ) ;
155+ if ( this . options . format === 'compact-text' ) {
156+ cliux . loaderV2 ( '' , spinner1 ) ;
157+ await printCompactTextViewFromJsonlModulePath ( modulePath ) ;
158+ } else if ( this . options . format === 'detailed-text' ) {
159+ const branchModuleData = await readAllJsonLines ( modulePath ) ;
160+ const verboseRes = await parseVerbose ( branchModuleData , payload ) ;
161+ cliux . loaderV2 ( '' , spinner1 ) ;
162+ printVerboseTextView ( verboseRes ) ;
163+ exportCSVReport ( displayModule , verboseRes , this . options . csvPath ) ;
164+ if ( verboseRes . verboseCacheSessionId ) {
165+ await cleanupSession ( verboseRes . verboseCacheSessionId ) . catch ( ( ) => undefined ) ;
166+ }
167+ }
168+ }
169+ }
170+
171+ private async displayFromInlineResult (
172+ inline : { content_types : BranchCompactTextRes ; global_fields : BranchCompactTextRes } ,
173+ payload : BranchDiffPayload ,
174+ ) : Promise < void > {
175+ const modulesToShow =
176+ this . options . module === 'all'
177+ ? ( [ 'content_types' , 'global_fields' ] as const )
178+ : payload . module === 'content_types'
179+ ? ( [ 'content_types' ] as const )
180+ : ( [ 'global_fields' ] as const ) ;
181+
182+ for ( const module of modulesToShow ) {
183+ const compact = inline [ module ] ;
184+ const displayModule = module === 'content_types' ? 'content-types' : 'global-fields' ;
185+ payload . module = module ;
186+ cliux . print ( ' ' ) ;
187+ cliux . print ( `${ startCase ( camelCase ( module ) ) } Summary:` , { color : 'yellow' } ) ;
188+ const diffSummary = summaryFromCompact ( compact , this . options . baseBranch , this . options . compareBranch ) ;
189+ printSummary ( diffSummary ) ;
190+ const spinner1 = cliux . loaderV2 ( 'Loading branch differences...' ) ;
191+ if ( this . options . format === 'compact-text' ) {
192+ cliux . loaderV2 ( '' , spinner1 ) ;
193+ printCompactTextView ( compact ) ;
194+ } else if ( this . options . format === 'detailed-text' ) {
195+ const branchModuleData = flatCompactToRawArray ( compact ) ;
196+ const verboseRes = await parseVerbose ( branchModuleData , payload ) ;
197+ cliux . loaderV2 ( '' , spinner1 ) ;
198+ printVerboseTextView ( verboseRes ) ;
199+ exportCSVReport ( displayModule , verboseRes , this . options . csvPath ) ;
200+ if ( verboseRes . verboseCacheSessionId ) {
201+ await cleanupSession ( verboseRes . verboseCacheSessionId ) . catch ( ( ) => undefined ) ;
202+ }
203+ }
104204 }
105205 }
106206
@@ -133,6 +233,9 @@ export default class BranchDiffHandler {
133233 printVerboseTextView ( verboseRes ) ;
134234
135235 exportCSVReport ( payload . module , verboseRes , this . options . csvPath ) ;
236+ if ( verboseRes . verboseCacheSessionId ) {
237+ await cleanupSession ( verboseRes . verboseCacheSessionId ) . catch ( ( ) => undefined ) ;
238+ }
136239 }
137240 }
138241}
0 commit comments