-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdiff-handler.ts
More file actions
138 lines (123 loc) · 4.46 KB
/
diff-handler.ts
File metadata and controls
138 lines (123 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import startCase from 'lodash/startCase';
import camelCase from 'lodash/camelCase';
import { cliux } from '@contentstack/cli-utilities';
import { getbranchConfig } from '../utils';
import { BranchOptions, BranchDiffPayload } from '../interfaces';
import { askBaseBranch, askCompareBranch, askStackAPIKey, selectModule } from '../utils/interactive';
import {
fetchBranchesDiff,
parseSummary,
printSummary,
parseCompactText,
printCompactTextView,
parseVerbose,
printVerboseTextView,
filterBranchDiffDataByModule,
} from '../utils/branch-diff-utility';
import { exportCSVReport } from '../utils/csv-utility';
export default class BranchDiffHandler {
private options: BranchOptions;
constructor(params: BranchOptions) {
this.options = params;
}
async run(): Promise<any> {
await this.validateMandatoryFlags();
await this.initBranchDiffUtility();
}
/**
* @methods validateMandatoryFlags - validate flags and prompt to select required flags
* @returns {*} {Promise<void>}
* @memberof BranchDiff
*/
async validateMandatoryFlags(): Promise<void> {
let baseBranch: string;
if (!this.options.stackAPIKey) {
this.options.stackAPIKey = await askStackAPIKey();
}
if (!this.options.baseBranch) {
baseBranch = getbranchConfig(this.options.stackAPIKey);
if (!baseBranch) {
this.options.baseBranch = await askBaseBranch();
} else {
this.options.baseBranch = baseBranch;
}
}
if (!this.options.compareBranch) {
this.options.compareBranch = await askCompareBranch();
}
if (!this.options.module) {
this.options.module = await selectModule();
}
if (this.options.format === 'detailed-text' && !this.options.csvPath) {
this.options.csvPath = process.cwd();
}
if(baseBranch){
cliux.print(`\nBase branch: ${baseBranch}`, { color: 'grey' });
}
}
/**
* @methods initBranchDiffUtility - call utility function to load data and display it
* @returns {*} {Promise<void>}
* @memberof BranchDiff
*/
async initBranchDiffUtility(): Promise<void> {
const spinner = cliux.loaderV2('Loading branch differences...');
const payload: BranchDiffPayload = {
module: '',
apiKey: this.options.stackAPIKey,
baseBranch: this.options.baseBranch,
compareBranch: this.options.compareBranch,
host: this.options.host
};
if (this.options.module === 'content-types') {
payload.module = 'content_types';
} else if (this.options.module === 'global-fields') {
payload.module = 'global_fields';
}
payload.spinner = spinner;
const branchDiffData = await fetchBranchesDiff(payload);
const diffData = filterBranchDiffDataByModule(branchDiffData);
cliux.loaderV2('', spinner);
if(this.options.module === 'all'){
for (let module in diffData) {
const branchDiff = diffData[module];
payload.module = module;
this.displaySummary(branchDiff, module);
await this.displayBranchDiffTextAndVerbose(branchDiff, payload);
}
}else{
const branchDiff = diffData[payload.module];
this.displaySummary(branchDiff, this.options.module);
await this.displayBranchDiffTextAndVerbose(branchDiff, payload);
}
}
/**
* @methods displaySummary - show branches summary on CLI
* @returns {*} {void}
* @memberof BranchDiff
*/
displaySummary(branchDiffData: any[], module: string): void {
cliux.print(' ');
cliux.print(`${startCase(camelCase(module))} Summary:`, { color: 'yellow' });
const diffSummary = parseSummary(branchDiffData, this.options.baseBranch, this.options.compareBranch);
printSummary(diffSummary);
}
/**
* @methods displayBranchDiffTextAndVerbose - to show branch differences in compactText or detailText format
* @returns {*} {void}
* @memberof BranchDiff
*/
async displayBranchDiffTextAndVerbose(branchDiffData: any[], payload: BranchDiffPayload): Promise<void> {
const spinner = cliux.loaderV2('Loading branch differences...');
if (this.options.format === 'compact-text') {
const branchTextRes = parseCompactText(branchDiffData);
cliux.loaderV2('', spinner);
printCompactTextView(branchTextRes);
} else if (this.options.format === 'detailed-text') {
const verboseRes = await parseVerbose(branchDiffData, payload);
cliux.loaderV2('', spinner);
printVerboseTextView(verboseRes);
exportCSVReport(payload.module, verboseRes, this.options.csvPath);
}
}
}