-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathimport-setup.ts
More file actions
114 lines (96 loc) · 3.75 KB
/
import-setup.ts
File metadata and controls
114 lines (96 loc) · 3.75 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
import { ImportConfig, Modules } from '../types';
import { backupHandler, log, setupBranchConfig } from '../utils';
import { ContentstackClient } from '@contentstack/cli-utilities';
import { validateBranch } from '../utils';
export default class ImportSetup {
protected config: ImportConfig;
private managementAPIClient: ContentstackClient;
private importConfig: ImportConfig;
private stackAPIClient: any;
public dependencyTree: { [key: string]: string[] } = {};
constructor(config: ImportConfig, managementAPIClient: ContentstackClient) {
this.config = config;
this.managementAPIClient = managementAPIClient;
this.stackAPIClient = this.managementAPIClient.stack({
api_key: this.config.apiKey,
management_token: this.config.management_token,
});
}
/**
* Generate mapper logic
* This method generates dependency tree based on the selected modules
* @returns {Promise<Array<void | string>>}
*/
protected async generateDependencyTree() {
type ModulesKey = keyof typeof this.config.modules;
const visited: Set<string> = new Set();
const assignedDependencies: Set<string> = new Set(); // Track assigned dependencies
const getAllDependencies = (module: ModulesKey): Modules[] => {
if (visited.has(module)) return [];
visited.add(module);
const dependencies: Modules[] = this.config.modules[module]?.dependencies || [];
let allDeps: Modules[] = [...dependencies];
for (const dependency of dependencies) {
allDeps.push(...getAllDependencies(dependency as ModulesKey));
}
return allDeps;
};
this.dependencyTree = {}; // Reset before building
for (const module of this.config.selectedModules) {
let allDependencies = getAllDependencies(module as ModulesKey);
allDependencies = allDependencies.filter((dep) => !assignedDependencies.has(dep)); // Remove assigned ones
this.dependencyTree[module] = allDependencies;
// Mark these dependencies as assigned so they won't be included in later modules
allDependencies.forEach((dep) => assignedDependencies.add(dep));
}
}
/**
* Run module imports based on the selected modules
* This method dynamically imports modules based on the selected modules
* and runs the start method of each module
* @returns {Promise<void>}
*/
protected async runModuleImports() {
for (const moduleName in this.dependencyTree) {
try {
const modulePath = `./modules/${moduleName}`;
const { default: ModuleClass } = await import(modulePath);
const modulePayload = {
config: this.config,
dependencies: this.dependencyTree[moduleName],
stackAPIClient: this.stackAPIClient,
};
const moduleInstance = new ModuleClass(modulePayload);
await moduleInstance.start();
} catch (error) {
log(this.config, `Error occurred while importing '${moduleName}'`, 'error');
throw error;
}
}
}
/**
* Start the import setup process
* This method generates mapper logic and runs module imports
* based on the selected modules
* @returns {Promise<void>}
*/
async start() {
try {
if (!this.config.management_token) {
const stackDetails: Record<string, unknown> = await this.stackAPIClient.fetch();
this.config.stackName = stackDetails.name as string;
this.config.org_uid = stackDetails.org_uid as string;
}
const backupDir = await backupHandler(this.config);
if (backupDir) {
this.config.backupDir = backupDir;
}
await setupBranchConfig(this.config, this.stackAPIClient);
await this.generateDependencyTree();
await this.runModuleImports();
} catch (error) {
console.log(error);
throw error;
}
}
}