-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.ts
More file actions
360 lines (319 loc) · 13.1 KB
/
stack.ts
File metadata and controls
360 lines (319 loc) · 13.1 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import find from 'lodash/find';
import omit from 'lodash/omit';
import { resolve as pResolve } from 'node:path';
import {
handleAndLogError,
isAuthenticated,
managementSDKClient,
log,
} from '@contentstack/cli-utilities';
import { PATH_CONSTANTS } from '../../constants';
import BaseClass from './base-class';
import {
fsUtil,
PROCESS_NAMES,
MODULE_CONTEXTS,
PROCESS_STATUS,
MODULE_NAMES,
} 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.exportDir,
this.exportConfig.branchName || '',
this.stackConfig.dirName,
);
this.exportConfig.context.module = MODULE_CONTEXTS.STACK;
this.currentModuleName = MODULE_NAMES[MODULE_CONTEXTS.STACK];
}
async start(): Promise<void> {
try {
log.debug('Starting stack export process...', this.exportConfig.context);
// Initial analysis with loading spinner (skip getStack when using management token — no SDK snapshot)
const [stackData] = await this.withLoadingSpinner('STACK: Analyzing stack configuration...', async () => {
const stackData =
this.exportConfig.management_token || !isAuthenticated() ? null : await this.getStack();
return [stackData];
});
// Create nested progress manager
const progress = this.createNestedProgress(this.currentModuleName);
const orgUid = stackData?.org_uid ?? stackData?.organization_uid;
if (orgUid) {
log.debug(`Found organization UID: '${orgUid}'.`, this.exportConfig.context);
this.exportConfig.org_uid = orgUid;
this.exportConfig.sourceStackName = stackData.name;
log.debug(`Set source stack name: ${stackData.name}`, this.exportConfig.context);
}
if (!this.exportConfig.management_token) {
progress.addProcess(PROCESS_NAMES.STACK_SETTINGS, 1);
}
progress.addProcess(PROCESS_NAMES.STACK_DETAILS, 1);
if (!this.exportConfig.preserveStackVersion && !this.exportConfig.hasOwnProperty('master_locale')) {
progress.addProcess(PROCESS_NAMES.STACK_LOCALE, 1);
}
let stackDetailsExportResult: any;
// Execute processes
if (!this.exportConfig.management_token) {
progress
.startProcess(PROCESS_NAMES.STACK_SETTINGS)
.updateStatus(
PROCESS_STATUS[PROCESS_NAMES.STACK_SETTINGS].EXPORTING,
PROCESS_NAMES.STACK_SETTINGS,
);
await this.exportStackSettings();
progress.completeProcess(PROCESS_NAMES.STACK_SETTINGS, true);
progress
.startProcess(PROCESS_NAMES.STACK_DETAILS)
.updateStatus(
PROCESS_STATUS[PROCESS_NAMES.STACK_DETAILS].EXPORTING,
PROCESS_NAMES.STACK_DETAILS,
);
stackDetailsExportResult = await this.exportStack(stackData);
progress.completeProcess(PROCESS_NAMES.STACK_DETAILS, true);
} else {
log.info(
'Skipping stack settings export: Operation is not supported when using a management token.',
this.exportConfig.context,
);
progress
.startProcess(PROCESS_NAMES.STACK_DETAILS)
.updateStatus(
PROCESS_STATUS[PROCESS_NAMES.STACK_DETAILS].EXPORTING,
PROCESS_NAMES.STACK_DETAILS,
);
stackDetailsExportResult = await this.writeStackJsonFromConfigApiKeyOnly();
progress.completeProcess(PROCESS_NAMES.STACK_DETAILS, true);
}
if (!this.exportConfig.preserveStackVersion && !this.exportConfig.hasOwnProperty('master_locale')) {
progress
.startProcess(PROCESS_NAMES.STACK_LOCALE)
.updateStatus(
PROCESS_STATUS[PROCESS_NAMES.STACK_LOCALE].FETCHING,
PROCESS_NAMES.STACK_LOCALE,
);
const masterLocale = await this.getLocales();
progress.completeProcess(PROCESS_NAMES.STACK_LOCALE, true);
if (masterLocale?.code) {
this.exportConfig.master_locale = { code: masterLocale.code };
log.debug(`Set master locale: ${masterLocale.code}`, this.exportConfig.context);
}
this.completeProgress(true);
return masterLocale;
} else if (this.exportConfig.preserveStackVersion) {
this.completeProgress(true);
return stackDetailsExportResult;
} else {
log.debug('Locale locale already set, skipping locale fetch', this.exportConfig.context);
}
this.completeProgressWithMessage();
} catch (error) {
log.debug('Error occurred during stack export', this.exportConfig.context);
handleAndLogError(error, { ...this.exportConfig.context });
this.completeProgress(false, error?.message || 'Stack export failed');
throw error;
}
}
async getStack(): Promise<any> {
log.debug(`Fetching stack data for: '${this.exportConfig.apiKey}'...`, this.exportConfig.context);
const tempAPIClient = await managementSDKClient({ host: this.exportConfig.host });
log.debug(`Created Management SDK client with host: '${this.exportConfig.host}'.`, this.exportConfig.context);
return await tempAPIClient
.stack({ api_key: this.exportConfig.apiKey })
.fetch()
.then((data: any) => {
log.debug(`Successfully fetched stack data for: '${this.exportConfig.apiKey}'.`, this.exportConfig.context);
return data;
})
.catch((error: any) => {
log.debug(`Failed to fetch stack data for: '${this.exportConfig.apiKey}'.`, this.exportConfig.context);
return {};
});
}
async getLocales(skip: number = 0) {
if (skip) {
this.qs.skip = skip;
log.debug(`Fetching locales with skip: ${skip}.`, this.exportConfig.context);
} else {
log.debug('Fetching locales with initial query...', this.exportConfig.context);
}
log.debug(`Query parameters: ${JSON.stringify(this.qs)}.`, this.exportConfig.context);
return await this.stack
.locale()
.query(this.qs)
.find()
.then(async (data: any) => {
const { items, count } = data;
log.debug(`Fetched ${items?.length || 0} locales out of ${count}.`, this.exportConfig.context);
if (items?.length) {
log.debug(`Processing ${items.length} locales to find master locale`, this.exportConfig.context);
// Track progress for each locale processed
this.progressManager?.tick(true, 'Fetch locale', null, PROCESS_NAMES.STACK_LOCALE);
skip += this.stackConfig.limit || 100;
const masterLocalObj = find(items, (locale: any) => {
if (locale.fallback_locale === null) {
log.debug(`Found master locale: '${locale.name}' (code: ${locale.code}).`, this.exportConfig.context);
return locale;
}
});
if (masterLocalObj) {
log.debug(`Returning master locale: '${masterLocalObj.name}'.`, this.exportConfig.context);
return masterLocalObj;
} else if (skip >= count) {
log.error(
`Locale locale not found in the stack ${this.exportConfig.apiKey}. Please ensure that the stack has a master locale.`,
this.exportConfig.context,
);
log.debug('Completed search. Master locale not found.', this.exportConfig.context);
return;
} else {
log.debug(
`Locale locale not found in current batch, continuing with skip: ${skip}`,
this.exportConfig.context,
);
return await this.getLocales(skip);
}
} else {
log.debug('No locales found to process.', this.exportConfig.context);
}
})
.catch((error: any) => {
log.debug(
`Error occurred while fetching locales for stack: ${this.exportConfig.apiKey}`,
this.exportConfig.context,
);
this.progressManager?.tick(
false,
'locale fetch',
error?.message || PROCESS_STATUS[PROCESS_NAMES.STACK_LOCALE].FAILED,
PROCESS_NAMES.STACK_LOCALE,
);
handleAndLogError(
error,
{ ...this.exportConfig.context },
`Failed to fetch locales for stack ${this.exportConfig.apiKey}`,
);
throw error;
});
}
/**
* Reuse stack snapshot from `getStack()` when present so we do not call `stack.fetch()` twice
* (same GET /stacks payload as writing stack.json). Falls back to `this.stack.fetch()` otherwise.
*/
async exportStack(preloadedStack?: Record<string, any> | null): Promise<any> {
log.debug(`Starting stack export for: '${this.exportConfig.apiKey}'...`, this.exportConfig.context);
await fsUtil.makeDirectory(this.stackFolderPath);
log.debug(`Created stack directory at: '${this.stackFolderPath}'`, this.exportConfig.context);
if (this.isStackFetchPayload(preloadedStack)) {
log.debug('Reusing stack payload from analysis step (no extra stack.fetch).', this.exportConfig.context);
try {
return this.persistStackJsonPayload(preloadedStack);
} catch (error: any) {
this.progressManager?.tick(
false,
'stack export',
error?.message || PROCESS_STATUS[PROCESS_NAMES.STACK_DETAILS].FAILED,
PROCESS_NAMES.STACK_DETAILS,
);
handleAndLogError(error, { ...this.exportConfig.context });
return undefined;
}
}
return this.stack
.fetch()
.then((resp: any) => {
return this.persistStackJsonPayload(resp);
})
.catch((error: any) => {
log.debug(`Error occurred while exporting stack: ${this.exportConfig.apiKey}`, this.exportConfig.context);
this.progressManager?.tick(
false,
'stack export',
error?.message || PROCESS_STATUS[PROCESS_NAMES.STACK_DETAILS].FAILED,
PROCESS_NAMES.STACK_DETAILS,
);
handleAndLogError(error, { ...this.exportConfig.context });
});
}
private isStackFetchPayload(data: unknown): data is Record<string, any> {
return (
typeof data === 'object' &&
data !== null &&
!Array.isArray(data) &&
('api_key' in data || 'uid' in data)
);
}
/**
* Management-token exports cannot use Stack CMA endpoints for full metadata; write api_key from config only.
*/
private async writeStackJsonFromConfigApiKeyOnly(): Promise<{ api_key: string }> {
if (!this.exportConfig.apiKey || typeof this.exportConfig.apiKey !== 'string') {
throw new Error('Stack API key is required to write stack.json when using a management token.');
}
log.debug('Writing config-based stack.json (api_key only, no stack fetch).', this.exportConfig.context);
await fsUtil.makeDirectory(this.stackFolderPath);
const payload = { api_key: this.exportConfig.apiKey };
const stackFilePath = pResolve(this.stackFolderPath, this.stackConfig.fileName);
fsUtil.writeFile(stackFilePath, payload);
this.progressManager?.tick(
true,
`stack: ${this.exportConfig.apiKey}`,
null,
PROCESS_NAMES.STACK_DETAILS,
);
log.success(
`Stack identifier written to stack.json from config for stack ${this.exportConfig.apiKey}`,
this.exportConfig.context,
);
return payload;
}
private persistStackJsonPayload(resp: Record<string, any>): any {
const sanitized = omit(resp, this.stackConfig.invalidKeys ?? []);
const stackFilePath = pResolve(this.stackFolderPath, this.stackConfig.fileName);
log.debug(`Writing stack data to: '${stackFilePath}'`, this.exportConfig.context);
fsUtil.writeFile(stackFilePath, sanitized);
this.progressManager?.tick(
true,
`stack: ${this.exportConfig.apiKey}`,
null,
PROCESS_NAMES.STACK_DETAILS,
);
log.success(
`Stack details exported successfully for stack ${this.exportConfig.apiKey}`,
this.exportConfig.context,
);
log.debug('Stack export completed successfully.', this.exportConfig.context);
return sanitized;
}
async exportStackSettings(): Promise<any> {
log.info('Exporting stack settings...', this.exportConfig.context);
await fsUtil.makeDirectory(this.stackFolderPath);
return this.stack
.settings()
.then((resp: any) => {
fsUtil.writeFile(pResolve(this.stackFolderPath, PATH_CONSTANTS.FILES.SETTINGS), resp);
// Track progress for stack settings completion
this.progressManager?.tick(true, 'stack settings', null, PROCESS_NAMES.STACK_SETTINGS);
log.success('Exported stack settings successfully!', this.exportConfig.context);
return resp;
})
.catch((error: any) => {
this.progressManager?.tick(
false,
'stack settings',
error?.message || PROCESS_STATUS[PROCESS_NAMES.STACK_SETTINGS].FAILED,
PROCESS_NAMES.STACK_SETTINGS,
);
handleAndLogError(error, { ...this.exportConfig.context });
});
}
}