-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathstack.ts
More file actions
119 lines (111 loc) · 3.94 KB
/
stack.ts
File metadata and controls
119 lines (111 loc) · 3.94 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
import find from 'lodash/find';
import { resolve as pResolve } from 'node:path';
import { isAuthenticated, managementSDKClient } from '@contentstack/cli-utilities';
import BaseClass from './base-class';
import { log, formatError, fsUtil } from '../../utils';
import { StackConfig, ModuleClassParams } from '../../types';
export default class ExportStack extends BaseClass {
private stackConfig: StackConfig;
private stackFolderPath: string;
private qs: {
include_count: boolean;
skip?: number;
};
constructor({ exportConfig, stackAPIClient }: ModuleClassParams) {
super({ exportConfig, stackAPIClient });
this.stackConfig = exportConfig.modules.stack;
this.qs = { include_count: true };
this.stackFolderPath = pResolve(
this.exportConfig.data,
this.exportConfig.branchName || '',
this.stackConfig.dirName,
);
}
async start(): Promise<void> {
if (isAuthenticated()) {
const stackData = await this.getStack();
if (stackData?.org_uid) {
this.exportConfig.org_uid = stackData.org_uid;
this.exportConfig.sourceStackName = stackData.name;
}
}
if (this.exportConfig.management_token) {
log(this.exportConfig, 'Skipping stack settings export: Operation is not supported when using a management token.', 'info');
} else {
await this.exportStackSettings();
}
if (!this.exportConfig.preserveStackVersion && !this.exportConfig.hasOwnProperty('master_locale')) {
//fetch master locale details
return this.getLocales();
} else if (this.exportConfig.preserveStackVersion) {
return this.exportStack();
}
}
async getStack(): Promise<any> {
const tempAPIClient = await managementSDKClient({ host: this.exportConfig.host });
return await tempAPIClient
.stack({ api_key: this.exportConfig.source_stack })
.fetch()
.catch((error: any) => {});
}
async getLocales(skip: number = 0) {
if (skip) {
this.qs.skip = skip;
}
return await this.stack
.locale()
.query(this.qs)
.find()
.then(async (data: any) => {
const { items, count } = data;
if (items?.length) {
skip += this.stackConfig.limit || 100;
const masterLocalObj = find(items, (locale: any) => {
if (locale.fallback_locale === null) {
return locale;
}
});
if (masterLocalObj) {
return masterLocalObj;
} else if (skip >= count) {
log(this.exportConfig, 'Master locale not found', 'error');
return;
} else {
return await this.getLocales(skip);
}
}
})
.catch((error: any) => {
log(this.exportConfig, `Failed to export locales. ${formatError(error)}`, 'error');
log(this.exportConfig, error, 'error');
});
}
async exportStack(): Promise<any> {
log(this.exportConfig, 'Exporting stack details', 'success');
await fsUtil.makeDirectory(this.stackFolderPath);
return this.stack
.fetch()
.then((resp: any) => {
fsUtil.writeFile(pResolve(this.stackFolderPath, this.stackConfig.fileName), resp);
log(this.exportConfig, 'Exported stack details successfully!', 'success');
return resp;
})
.catch((error: any) => {
log(this.exportConfig, `Failed to export stack. ${formatError(error)}`, 'error');
});
}
async exportStackSettings(): Promise<any> {
log(this.exportConfig, 'Exporting stack settings', 'success');
await fsUtil.makeDirectory(this.stackFolderPath);
return this.stack
.settings()
.then((resp: any) => {
fsUtil.writeFile(pResolve(this.stackFolderPath, 'settings.json'), resp);
log(this.exportConfig, 'Exported stack settings successfully!', 'success');
return resp;
})
.catch((error: any) => {
log(this.exportConfig, `Failed to export stack settings. ${formatError(error)}`, 'error');
});
}
}