-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule-exporter.ts
More file actions
147 lines (129 loc) · 5.08 KB
/
module-exporter.ts
File metadata and controls
147 lines (129 loc) · 5.08 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
139
140
141
142
143
144
145
146
147
import {
ContentstackClient,
handleAndLogError,
messageHandler,
log,
getBranchFromAlias,
CLIProgressManager,
} from '@contentstack/cli-utilities';
import startModuleExport from './modules';
import { ExportConfig, Modules } from '../types';
import { setupBranches, setupExportDir } from '../utils';
class ModuleExporter {
private managementAPIClient: ContentstackClient;
private exportConfig: ExportConfig;
private stackAPIClient: ReturnType<ContentstackClient['stack']>;
constructor(managementAPIClient: ContentstackClient, exportConfig: ExportConfig) {
this.managementAPIClient = managementAPIClient;
this.stackAPIClient = this.managementAPIClient.stack({
api_key: exportConfig.apiKey,
management_token: exportConfig.management_token,
});
this.exportConfig = exportConfig;
}
async start(): Promise<any> {
// setup the branches
try {
if (!this.exportConfig.branchName && this.exportConfig.branchAlias) {
this.exportConfig.branchName = await getBranchFromAlias(this.stackAPIClient, this.exportConfig.branchAlias);
}
await setupBranches(this.exportConfig, this.stackAPIClient);
await setupExportDir(this.exportConfig);
// if branches available run it export by branches
if (this.exportConfig.branches) {
this.exportConfig.branchEnabled = true;
return this.exportByBranches();
}
// If branches disabled then initialize the global summary
CLIProgressManager.initializeGlobalSummary('EXPORT', this.exportConfig.branchName, 'Exporting content...');
return this.export();
} catch (error) {
throw error;
}
}
async exportByBranches(): Promise<void> {
let targetBranch;
if (this.exportConfig.branchName) {
// User specified a branch - export only that branch
targetBranch = this.exportConfig.branches.find((branch) => branch.uid === this.exportConfig.branchName);
if (!targetBranch) {
throw new Error(`Branch '${this.exportConfig.branchName}' not found in available branches`);
}
} else {
// No specific branch mentioned - export only the main branch
targetBranch = this.exportConfig.branches.find((branch) => branch.uid === 'main');
if (!targetBranch) {
throw new Error('No main branch or available branches found');
}
}
try {
this.exportConfig.branchName = targetBranch.uid;
this.stackAPIClient.stackHeaders.branch = targetBranch.uid;
this.exportConfig.branchDir = this.exportConfig.exportDir;
// Initialize progress manager for the target branch
CLIProgressManager.clearGlobalSummary();
CLIProgressManager.initializeGlobalSummary(
`EXPORT-${targetBranch.uid}`,
targetBranch.uid,
`Exporting "${targetBranch.uid}" branch content...`,
);
log.info(`Exporting content from '${targetBranch.uid}' branch`, this.exportConfig.context);
await this.export();
CLIProgressManager.printGlobalSummary();
log.success(
`The content of branch ${targetBranch.uid} has been exported successfully!`,
this.exportConfig.context,
);
} catch (error) {
handleAndLogError(
error,
{ ...this.exportConfig.context, branch: targetBranch?.uid },
messageHandler.parse('FAILED_EXPORT_CONTENT_BRANCH', { branch: targetBranch?.uid }),
);
throw new Error(messageHandler.parse('FAILED_EXPORT_CONTENT_BRANCH', { branch: targetBranch?.uid }));
}
}
async export() {
log.info(`Started to export content`, this.exportConfig.context);
// checks for single module or all modules
if (this.exportConfig.singleModuleExport) {
return this.exportSingleModule(this.exportConfig.moduleName);
}
return this.exportAllModules();
}
async exportByModuleByName(moduleName: Modules) {
log.info(`Exporting module: '${moduleName}'...`, this.exportConfig.context);
// export the modules by name
// calls the module runner which inturn calls the module itself
await startModuleExport({
stackAPIClient: this.stackAPIClient,
exportConfig: this.exportConfig,
moduleName,
});
}
async exportSingleModule(moduleName: Modules): Promise<void> {
// Note stack is always exported
let exportModules: Modules[] = [];
if (!this.exportConfig.skipStackSettings) {
exportModules.push('stack');
}
if (!this.exportConfig.skipDependencies) {
const moduleConfig = this.exportConfig.modules[moduleName as keyof typeof this.exportConfig.modules];
const dependencies = (moduleConfig as any)?.dependencies || [];
if (dependencies.length > 0) {
exportModules = exportModules.concat(dependencies);
}
}
exportModules.push(moduleName);
for (const moduleName of exportModules) {
await this.exportByModuleByName(moduleName);
}
}
async exportAllModules(): Promise<any> {
// use the algorithm to determine the parallel and sequential execution of modules
for (const moduleName of this.exportConfig.modules.types) {
await this.exportByModuleByName(moduleName);
}
}
}
export default ModuleExporter;