-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal-fields.ts
More file actions
147 lines (125 loc) · 5.62 KB
/
global-fields.ts
File metadata and controls
147 lines (125 loc) · 5.62 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
import * as path from 'path';
import { ContentstackClient, handleAndLogError, messageHandler, log, sanitizePath } from '@contentstack/cli-utilities';
import BaseClass from './base-class';
import { ExportConfig, ModuleClassParams } from '../../types';
import { fsUtil, getExportBasePath, MODULE_CONTEXTS, MODULE_NAMES } from '../../utils';
export default class GlobalFieldsExport extends BaseClass {
private stackAPIClient: ReturnType<ContentstackClient['stack']>;
public exportConfig: ExportConfig;
private qs: {
include_count: boolean;
asc: string;
skip?: number;
limit?: number;
include_global_field_schema?: boolean;
};
private globalFieldsConfig: {
dirName?: string;
fileName?: string;
validKeys?: string[];
fetchConcurrency?: number;
writeConcurrency?: number;
limit?: number;
};
private globalFieldsDirPath: string;
private globalFields: Record<string, unknown>[];
constructor({ exportConfig, stackAPIClient }: ModuleClassParams) {
super({ exportConfig, stackAPIClient });
this.stackAPIClient = stackAPIClient;
this.globalFieldsConfig = exportConfig.modules['global-fields'];
this.qs = {
skip: 0,
asc: 'updated_at',
include_count: true,
limit: this.globalFieldsConfig.limit,
include_global_field_schema: true,
};
this.globalFieldsDirPath = path.resolve(
sanitizePath(getExportBasePath(exportConfig)),
sanitizePath(this.globalFieldsConfig.dirName),
);
this.globalFields = [];
this.applyQueryFilters(this.qs, 'global-fields');
this.exportConfig.context.module = MODULE_CONTEXTS.GLOBAL_FIELDS;
this.currentModuleName = MODULE_NAMES[MODULE_CONTEXTS.GLOBAL_FIELDS];
}
async start() {
try {
log.debug('Starting global fields export process...', this.exportConfig.context);
// Get global fields count and setup with loading spinner
const [totalCount] = await this.withLoadingSpinner('GLOBAL-FIELDS: Analyzing global fields...', async () => {
await fsUtil.makeDirectory(this.globalFieldsDirPath);
const countResponse = await this.stackAPIClient
.globalField()
.query({ ...this.qs, include_count: true, limit: 1 })
.find();
return [countResponse.count || 0];
});
// Create simple progress manager for global fields
const progress = this.createSimpleProgress(this.currentModuleName, totalCount);
if (totalCount === 0) {
log.info(messageHandler.parse('GLOBAL_FIELDS_NOT_FOUND'), this.exportConfig.context);
const globalFieldsFilePath = path.join(this.globalFieldsDirPath, this.globalFieldsConfig.fileName);
log.debug(`Writing global fields to: ${globalFieldsFilePath}`, this.exportConfig.context);
fsUtil.writeFile(globalFieldsFilePath, this.globalFields);
this.completeProgress(true);
return;
}
progress.updateStatus('Fetching global fields...');
await this.getGlobalFields();
const globalFieldsFilePath = path.join(this.globalFieldsDirPath, this.globalFieldsConfig.fileName);
log.debug(`Writing global fields to: ${globalFieldsFilePath}`, this.exportConfig.context);
fsUtil.writeFile(globalFieldsFilePath, this.globalFields);
this.completeProgressWithMessage();
} catch (error) {
log.debug('Error occurred during global fields export', this.exportConfig.context);
handleAndLogError(error, { ...this.exportConfig.context });
this.completeProgress(false, error?.message || 'Global fields export failed');
}
}
async getGlobalFields(skip: number = 0): Promise<any> {
if (skip) {
this.qs.skip = skip;
log.debug(`Fetching global fields with skip: ${skip}`, this.exportConfig.context);
}
log.debug(`Query parameters: ${JSON.stringify(this.qs)}`, this.exportConfig.context);
let globalFieldsFetchResponse = await this.stackAPIClient.globalField({ api_version: '3.2' }).query(this.qs).find();
log.debug(
`Fetched ${globalFieldsFetchResponse.items?.length || 0} global fields out of total ${
globalFieldsFetchResponse.count
}`,
this.exportConfig.context,
);
if (Array.isArray(globalFieldsFetchResponse.items) && globalFieldsFetchResponse.items.length > 0) {
log.debug(`Processing ${globalFieldsFetchResponse.items.length} global fields`, this.exportConfig.context);
this.sanitizeAttribs(globalFieldsFetchResponse.items);
skip += this.globalFieldsConfig.limit || 100;
if (skip >= globalFieldsFetchResponse.count) {
log.debug('Completed fetching all global fields', this.exportConfig.context);
return;
}
log.debug(`Continuing to fetch global fields with skip: ${skip}`, this.exportConfig.context);
return await this.getGlobalFields(skip);
} else {
log.debug('No global fields found to process', this.exportConfig.context);
}
}
sanitizeAttribs(globalFields: Record<string, string>[]) {
log.debug(`Sanitizing ${globalFields.length} global fields`, this.exportConfig.context);
globalFields.forEach((globalField: Record<string, string>) => {
log.debug(`Processing global field: ${globalField.uid || 'unknown'}`, this.exportConfig.context);
for (let key in globalField) {
if (this.globalFieldsConfig.validKeys.indexOf(key) === -1) {
delete globalField[key];
}
}
this.globalFields.push(globalField);
// Track progress for each global field
this.progressManager?.tick(true, `global-field: ${globalField.uid}`);
});
log.debug(
`Sanitization complete. Total global fields processed: ${this.globalFields.length}`,
this.exportConfig.context,
);
}
}