-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensions.ts
More file actions
97 lines (86 loc) · 4.21 KB
/
extensions.ts
File metadata and controls
97 lines (86 loc) · 4.21 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
import { log, fsUtil } from '../../utils';
import { join } from 'path';
import { ImportConfig, ModuleClassParams } from '../../types';
import { isEmpty } from 'lodash';
import { formatError, sanitizePath } from '@contentstack/cli-utilities';
import BaseImportSetup from './base-setup';
import { MODULE_NAMES, MODULE_CONTEXTS, PROCESS_NAMES, PROCESS_STATUS } from '../../utils';
export default class ExtensionImportSetup extends BaseImportSetup {
private extensionsFilePath: string;
private extensionMapper: Record<string, string>;
private extensionsConfig: ImportConfig['modules']['extensions'];
private extensionsFolderPath: string;
private extUidMapperPath: string;
constructor({ config, stackAPIClient }: ModuleClassParams) {
super({ config, stackAPIClient, dependencies: [] });
this.currentModuleName = MODULE_NAMES[MODULE_CONTEXTS.EXTENSIONS];
this.extensionsFilePath = join(sanitizePath(this.config.contentDir), 'extensions', 'extensions.json');
this.extensionsConfig = config.modules.extensions;
this.extUidMapperPath = join(sanitizePath(this.config.backupDir), 'mapper', 'extensions', 'uid-mapping.json');
this.extensionMapper = {};
}
/**
* Start the extension import setup
* This method reads the extensions from the content folder and generates a mapper file
* @returns {Promise<void>}
*/
async start() {
try {
const extensions: any = await this.withLoadingSpinner('EXTENSIONS: Analyzing import data...', async () => {
return await fsUtil.readFile(this.extensionsFilePath);
});
if (!isEmpty(extensions)) {
const extensionsArray = Object.values(extensions) as any[];
const progress = this.createNestedProgress(this.currentModuleName);
// Add process
progress.addProcess(PROCESS_NAMES.EXTENSIONS_MAPPER_GENERATION, extensionsArray.length);
// Create mapper directory
const mapperFilePath = join(sanitizePath(this.config.backupDir), 'mapper', 'extensions');
fsUtil.makeDirectory(mapperFilePath);
progress
.startProcess(PROCESS_NAMES.EXTENSIONS_MAPPER_GENERATION)
.updateStatus(
PROCESS_STATUS.EXTENSIONS_MAPPER_GENERATION.GENERATING,
PROCESS_NAMES.EXTENSIONS_MAPPER_GENERATION,
);
for (const extension of extensionsArray) {
try {
const targetExtension: any = await this.getExtension(extension);
if (!targetExtension) {
log(this.config, `Extension with the title '${extension.title}' not found in the stack.`, 'info');
this.progressManager?.tick(false, `extension: ${extension.title}`, 'Not found in stack', PROCESS_NAMES.EXTENSIONS_MAPPER_GENERATION);
continue;
}
this.extensionMapper[extension.uid] = targetExtension.uid;
this.progressManager?.tick(true, `extension: ${extension.title}`, null, PROCESS_NAMES.EXTENSIONS_MAPPER_GENERATION);
} catch (error) {
this.progressManager?.tick(false, `extension: ${extension.title}`, formatError(error), PROCESS_NAMES.EXTENSIONS_MAPPER_GENERATION);
}
}
await fsUtil.writeFile(this.extUidMapperPath, this.extensionMapper);
progress.completeProcess(PROCESS_NAMES.EXTENSIONS_MAPPER_GENERATION, true);
this.completeProgress(true);
log(this.config, `The required setup files for extensions have been generated successfully.`, 'success');
} else {
log(this.config, 'No extensions found in the content folder.', 'info');
}
} catch (error) {
this.completeProgress(false, error?.message || 'Extensions mapper generation failed');
log(this.config, `Error occurred while generating the extension mapper: ${formatError(error)}.`, 'error');
}
}
async getExtension(extension: any) {
// Implement this method to get the extension from the stack
return new Promise(async (resolve, reject) => {
const { items: [extensionsInStack] = [] } =
(await this.stackAPIClient
.extension()
.query({ query: { title: extension.title } })
.findOne()
.catch((error: Error) => {
reject(error);
})) || {};
resolve(extensionsInStack);
});
}
}