-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabels.ts
More file actions
138 lines (115 loc) · 5.13 KB
/
labels.ts
File metadata and controls
138 lines (115 loc) · 5.13 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
import omit from 'lodash/omit';
import isEmpty from 'lodash/isEmpty';
import { resolve as pResolve } from 'node:path';
import { handleAndLogError, messageHandler, log } from '@contentstack/cli-utilities';
import BaseClass from './base-class';
import { LabelConfig, ModuleClassParams } from '../../types';
import { fsUtil, getExportBasePath, MODULE_CONTEXTS, MODULE_NAMES } from '../../utils';
export default class ExportLabels extends BaseClass {
private labels: Record<string, Record<string, string>>;
private labelConfig: LabelConfig;
public labelsFolderPath: string;
private qs: {
include_count: boolean;
skip?: number;
};
constructor({ exportConfig, stackAPIClient }: ModuleClassParams) {
super({ exportConfig, stackAPIClient });
this.labels = {};
this.labelConfig = exportConfig.modules.labels;
this.qs = { include_count: true };
this.exportConfig.context.module = MODULE_CONTEXTS.LABELS;
this.currentModuleName = MODULE_NAMES[MODULE_CONTEXTS.LABELS];
}
async start(): Promise<void> {
try {
log.debug('Starting labels export process...', this.exportConfig.context);
// Setup with loading spinner
const [totalCount] = await this.withLoadingSpinner('LABELS: Analyzing labels...', async () => {
this.labelsFolderPath = pResolve(
getExportBasePath(this.exportConfig),
this.labelConfig.dirName,
);
await fsUtil.makeDirectory(this.labelsFolderPath);
log.debug(`Labels folder path: ${this.labelsFolderPath}`, this.exportConfig.context);
// Get count for progress tracking
const countResponse = await this.stack
.label()
.query({ ...this.qs, include_count: true, limit: 1 })
.find();
return [countResponse.count || 0];
});
if (totalCount === 0) {
log.info(messageHandler.parse('LABELS_NOT_FOUND'), this.exportConfig.context);
return;
}
// Create simple progress manager with total count
const progress = this.createSimpleProgress(this.currentModuleName, totalCount);
progress.updateStatus('Fetching labels...');
await this.getLabels();
log.debug(`Retrieved ${Object.keys(this.labels || {}).length} labels`, this.exportConfig.context);
if (this.labels === undefined || isEmpty(this.labels)) {
log.info(messageHandler.parse('LABELS_NOT_FOUND'), this.exportConfig.context);
} else {
const labelsFilePath = pResolve(this.labelsFolderPath, this.labelConfig.fileName);
log.debug(`Writing labels to: ${labelsFilePath}`, this.exportConfig.context);
fsUtil.writeFile(labelsFilePath, this.labels);
}
this.completeProgressWithMessage();
} catch (error) {
handleAndLogError(error, { ...this.exportConfig.context });
this.completeProgress(false, error?.message || 'Labels export failed');
}
}
async getLabels(skip = 0): Promise<void> {
if (skip) {
this.qs.skip = skip;
log.debug(`Fetching labels with skip: ${skip}`, this.exportConfig.context);
} else {
log.debug('Fetching labels with initial query', this.exportConfig.context);
}
log.debug(`Query parameters: ${JSON.stringify(this.qs)}`, this.exportConfig.context);
await this.stack
.label()
.query(this.qs)
.find()
.then(async (data: any) => {
const { items, count } = data;
log.debug(`Fetched ${items?.length || 0} labels out of total ${count}`, this.exportConfig.context);
if (items?.length) {
log.debug(`Processing ${items.length} labels`, this.exportConfig.context);
this.sanitizeAttribs(items);
skip += this.labelConfig.limit || 100;
if (skip >= count) {
log.debug('Completed fetching all labels', this.exportConfig.context);
return;
}
log.debug(`Continuing to fetch labels with skip: ${skip}`, this.exportConfig.context);
return await this.getLabels(skip);
} else {
log.debug('No labels found to process', this.exportConfig.context);
}
})
.catch((error: any) => {
this.progressManager?.tick(false, 'labels', error?.message || 'Failed to export labels');
log.debug('Error occurred while fetching labels', this.exportConfig.context);
handleAndLogError(error, { ...this.exportConfig.context });
});
}
sanitizeAttribs(labels: Record<string, string>[]) {
log.debug(`Sanitizing ${labels.length} labels`, this.exportConfig.context);
for (let index = 0; index < labels?.length; index++) {
const labelUid = labels[index].uid;
const labelName = labels[index]?.name;
log.debug(`Processing label: ${labelName} (${labelUid})`, this.exportConfig.context);
this.labels[labelUid] = omit(labels[index], this.labelConfig.invalidKeys);
log.info(messageHandler.parse('LABEL_EXPORT_SUCCESS', labelName), this.exportConfig.context);
// Track progress for each label
this.progressManager?.tick(true, `label: ${labelName}`);
}
log.debug(
`Sanitization complete. Total labels processed: ${Object.keys(this.labels).length}`,
this.exportConfig.context,
);
}
}