-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathstack.ts
More file actions
59 lines (52 loc) · 2.17 KB
/
stack.ts
File metadata and controls
59 lines (52 loc) · 2.17 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
import { join } from 'node:path';
import { fileHelper, fsUtil } from '../../utils';
import BaseClass from './base-class';
import { ModuleClassParams } from '../../types';
import { log, handleAndLogError } from '@contentstack/cli-utilities';
export default class ImportStack extends BaseClass {
// classname
private stackSettingsPath: string;
private envUidMapperPath: string;
private stackSettings: Record<string, any> | null = null;
private envUidMapper: Record<string, string> = {};
constructor({ importConfig, stackAPIClient }: ModuleClassParams) {
super({ importConfig, stackAPIClient });
this.stackSettingsPath = join(this.importConfig.backupDir, 'stack', 'settings.json');
this.envUidMapperPath = join(this.importConfig.backupDir, 'mapper', 'environments', 'uid-mapping.json');
}
async start(): Promise<void> {
if (this.importConfig.management_token) {
log.info(
'Skipping stack settings import: Operation is not supported when using a management token.',
this.importConfig.context,
);
return;
}
if (fileHelper.fileExistsSync(this.stackSettingsPath)) {
this.stackSettings = fsUtil.readFile(this.stackSettingsPath, true) as Record<string, any>;
} else {
log.info('No stack setting found!', this.importConfig.context);
return;
}
if (fileHelper.fileExistsSync(this.envUidMapperPath)) {
this.envUidMapper = fsUtil.readFile(this.envUidMapperPath, true) as Record<string, string>;
} else {
log.warn(
'Skipping stack settings import. Please run the environments migration first.',
this.importConfig.context,
);
return;
}
if (this.stackSettings?.live_preview && this.stackSettings?.live_preview['default-env']) {
const oldEnvUid = this.stackSettings.live_preview['default-env'];
const mappedEnvUid = this.envUidMapper[oldEnvUid];
this.stackSettings.live_preview['default-env'] = mappedEnvUid;
}
try {
await this.stack.addSettings(this.stackSettings);
log.success('Successfully imported stack', this.importConfig.context);
} catch (error) {
handleAndLogError(error, { ...this.importConfig.context });
}
}
}