-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathextensions.ts
More file actions
74 lines (67 loc) · 2.76 KB
/
extensions.ts
File metadata and controls
74 lines (67 loc) · 2.76 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
import * as chalk from 'chalk';
import { log, fsUtil } from '../../utils';
import { join } from 'path';
import { ImportConfig, ModuleClassParams } from '../../types';
import { isEmpty } from 'lodash';
export default class ExtensionImportSetup {
private config: ImportConfig;
private extensionsFilePath: string;
private extensionMapper: Record<string, string>;
private stackAPIClient: ModuleClassParams['stackAPIClient'];
private dependencies: ModuleClassParams['dependencies'];
private extensionsConfig: ImportConfig['modules']['extensions'];
private mapperDirPath: string;
private extensionsFolderPath: string;
private extUidMapperPath: string;
constructor({ config, stackAPIClient }: ModuleClassParams) {
this.config = config;
this.stackAPIClient = stackAPIClient;
this.extensionsFilePath = join(this.config.contentDir, 'extensions', 'extensions.json');
this.extensionsConfig = config.modules.extensions;
this.extUidMapperPath = join(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 fsUtil.readFile(this.extensionsFilePath);
if (!isEmpty(extensions)) {
// 2. Create mapper directory
const mapperFilePath = join(this.config.backupDir, 'mapper', 'extensions');
fsUtil.makeDirectory(mapperFilePath); // Use fsUtil
for (const extension of Object.values(extensions) as any) {
const targetExtension: any = await this.getExtension(extension);
if (!targetExtension) {
log(this.config, `Extension with title '${extension.title}' not found in the stack!`, 'info');
continue;
}
this.extensionMapper[extension.uid] = targetExtension.uid;
}
await fsUtil.writeFile(this.extUidMapperPath, this.extensionMapper);
log(this.config, `Generate required setup files for extension`, 'success');
} else {
log(this.config, 'No extensions found in the content folder!', 'error');
}
} catch (error) {
log(this.config, `Error generating extension mapper: ${error.message}`, '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) => {
reject(true);
})) || {};
resolve(extensionsInStack);
});
}
}