-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlocales.ts
More file actions
355 lines (305 loc) · 13.7 KB
/
locales.ts
File metadata and controls
355 lines (305 loc) · 13.7 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
/* eslint-disable no-prototype-builtins */
/*!
* Contentstack Import
* Copyright (c) 2024 Contentstack LLC
* MIT Licensed
*/
import * as path from 'path';
import { values, isEmpty, filter, pick, keys } from 'lodash';
import { cliux, sanitizePath, log, handleAndLogError } from '@contentstack/cli-utilities';
import { fsUtil, formatError, fileHelper } from '../../utils';
import { ImportConfig, ModuleClassParams } from '../../types';
import BaseClass from './base-class';
export default class ImportLocales extends BaseClass {
private langMapperPath: string;
private langFolderPath: string;
private langFailsPath: string;
private langSuccessPath: string;
private langUidMapperPath: string;
private languages: Record<string, unknown>[];
private config: ImportConfig;
private stackAPIClient: any;
private failedLocales: Record<string, unknown>[];
private createdLocales: Record<string, unknown>[];
private langUidMapper: any;
private localeConfig: any;
public client: any;
private reqConcurrency: number;
private masterLanguage: {
code: string;
};
private masterLanguageConfig: {
dirName: string;
fileName: string;
requiredKeys: string[];
};
private sourceMasterLanguage: Record<string, any>;
constructor({ importConfig, stackAPIClient }: ModuleClassParams) {
super({ importConfig, stackAPIClient });
this.config = importConfig;
this.config.context.module = 'locales';
this.currentModuleName = 'Locales';
this.localeConfig = importConfig.modules.locales;
this.masterLanguage = importConfig.masterLocale;
this.masterLanguageConfig = importConfig.modules.masterLocale;
this.stackAPIClient = stackAPIClient;
this.languages = [];
this.langUidMapper = {};
this.createdLocales = [];
this.failedLocales = [];
this.reqConcurrency = this.localeConfig.writeConcurrency || this.config.writeConcurrency;
this.langMapperPath = path.resolve(sanitizePath(this.config.data), 'mapper', 'languages');
this.langFolderPath = path.resolve(sanitizePath(this.config.data), sanitizePath(this.localeConfig.dirName));
this.langFailsPath = path.resolve(sanitizePath(this.config.data), 'mapper', 'languages', 'fails.json');
this.langSuccessPath = path.resolve(sanitizePath(this.config.data), 'mapper', 'languages', 'success.json');
this.langUidMapperPath = path.resolve(sanitizePath(this.config.data), 'mapper', 'languages', 'uid-mapper.json');
}
async start(): Promise<void> {
try {
log.debug('Starting locales import process...', this.config.context);
const [localesCount] = await this.analyzeLocales();
if (localesCount === 0) {
log.info('No languages found to import', this.config.context);
return;
}
const progress = this.setupLocalesProgress(localesCount);
this.prepareLocalesMapper();
await this.processMasterLocale(progress);
await this.processLocaleCreation(progress);
await this.processLocaleUpdate(progress);
log.debug('Writing failed locales to file', this.config.context);
fsUtil.writeFile(this.langFailsPath, this.failedLocales);
log.debug(`Written ${this.failedLocales.length} failed locales to file`, this.config.context);
this.completeProgress(true);
log.success('Languages have been imported successfully!', this.config.context);
} catch (error) {
this.completeProgress(false, error?.message || 'Locales import failed');
handleAndLogError(error, { ...this.config.context });
}
}
async checkAndUpdateMasterLocale(): Promise<void> {
log.debug('Checking and updating master locale', this.config.context);
const sourceMasterLangDetails = this.getSourceMasterLangDetails();
if (!sourceMasterLangDetails) return;
if (this.masterLanguage?.code !== sourceMasterLangDetails?.code) {
this.logCodeMismatch(sourceMasterLangDetails.code);
return;
}
log.debug(`Master locale code matches: ${this.masterLanguage.code}`, this.config.context);
const masterLangDetails = await this.fetchTargetMasterLocale();
if (!masterLangDetails) return;
if (
masterLangDetails?.name?.toString().toUpperCase() === sourceMasterLangDetails['name']?.toString().toUpperCase()
) {
this.tickProgress(true, `${masterLangDetails.name} (no update needed)`);
log.debug('Master language names match, no update required', this.config.context);
return;
}
await this.handleNameMismatch(sourceMasterLangDetails, masterLangDetails);
}
async createLocales(): Promise<any> {
const languagesToCreate = filter(
values(this.languages),
(lang) => lang.code !== this.masterLanguage.code,
) as Record<string, any>[];
log.debug(`Creating ${languagesToCreate.length} locales (excluding master locale)`, this.config.context);
const onSuccess = ({ response = {}, apiData: { uid, code } = undefined }: any) => {
this.langUidMapper[uid] = response.uid;
this.createdLocales.push(pick(response, [...this.localeConfig.requiredKeys]));
this.progressManager?.tick(true, `locale: ${code}`, null, 'Create');
log.info(`Created locale: '${code}'`, this.config.context);
log.debug(`Locale UID mapping: ${uid} → ${response.uid}`, this.config.context);
fsUtil.writeFile(this.langUidMapperPath, this.langUidMapper);
};
const onReject = ({ error, apiData: { uid, code } = undefined }: any) => {
this.progressManager?.tick(false, `locale: ${code}`, error?.message || 'Failed to create locale', 'Create');
if (error?.errorCode === 247) {
log.info(formatError(error), this.config.context);
} else {
log.error(`Language '${code}' failed to import`, this.config.context);
handleAndLogError(error, { ...this.config.context, code });
}
this.failedLocales.push({ uid, code });
};
return await this.makeConcurrentCall({
processName: 'Import locales',
apiContent: languagesToCreate,
apiParams: {
reject: onReject.bind(this),
resolve: onSuccess.bind(this),
entity: 'create-locale',
includeParamOnCompletion: true,
},
concurrencyLimit: this.reqConcurrency,
});
}
async updateLocales(): Promise<any> {
log.debug(`Updating ${values(this.languages).length} locales`, this.config.context);
const onSuccess = ({ response = {}, apiData: { uid, code } = undefined }: any) => {
log.info(`Updated locale: '${code}'`, this.config.context);
log.debug(`Locale update completed for: ${code}`, this.config.context);
this.progressManager?.tick(true, `locale: ${code}`, null, 'Update');
fsUtil.writeFile(this.langSuccessPath, this.createdLocales);
};
const onReject = ({ error, apiData: { uid, code } = undefined }: any) => {
this.progressManager?.tick(false, `locale: ${code}`, 'Failed to update locale', 'Update');
log.error(`Language '${code}' failed to update`, this.config.context);
handleAndLogError(error, { ...this.config.context, code });
fsUtil.writeFile(this.langFailsPath, this.failedLocales);
};
return await this.makeConcurrentCall({
processName: 'Locale Update locales',
apiContent: values(this.languages),
apiParams: {
reject: onReject.bind(this),
resolve: onSuccess.bind(this),
entity: 'update-locale',
includeParamOnCompletion: true,
},
concurrencyLimit: this.reqConcurrency,
});
}
private async analyzeLocales(): Promise<[number]> {
return this.withLoadingSpinner('LOCALES: Analyzing import data...', async () => {
log.debug('Loading locales from file', this.config.context);
this.languages = fsUtil.readFile(path.join(this.langFolderPath, this.localeConfig.fileName)) as Record<
string,
unknown
>[];
if (!this.languages || isEmpty(this.languages)) {
log.info('No languages found to import', this.config.context);
return [0];
}
this.sourceMasterLanguage = fsUtil.readFile(
path.join(this.langFolderPath, this.masterLanguageConfig.fileName),
) as Record<string, any>;
log.debug('Loaded source master language configuration', this.config.context);
const localesCount = keys(this.languages || {})?.length;
log.debug(`Found ${localesCount} languages to import`, this.config.context);
return [localesCount];
});
}
private setupLocalesProgress(localesCount: number) {
const progress = this.createNestedProgress(this.currentModuleName);
progress.addProcess('Master Locale ', 1);
if (localesCount > 0) {
progress.addProcess('Create', localesCount);
progress.addProcess('Update', localesCount);
}
return progress;
}
private async prepareLocalesMapper(): Promise<void> {
log.debug('Creating languages mapper directory', this.config.context);
fileHelper.makeDirectory(this.langMapperPath);
log.debug('Created languages mapper directory', this.config.context);
if (fileHelper.fileExistsSync(this.langUidMapperPath)) {
this.langUidMapper = fsUtil.readFile(this.langUidMapperPath) || {};
const langUidCount = Object.keys(this.langUidMapper).length;
log.debug(`Loaded existing language UID data: ${langUidCount} items`, this.config.context);
} else {
log.debug('No existing language UID mappings found', this.config.context);
}
}
private async processMasterLocale(progress: any): Promise<void> {
progress.startProcess('Master Locale ').updateStatus('Checking master locale...', 'Master Locale ');
log.debug('Checking and updating master locale', this.config.context);
try {
await this.checkAndUpdateMasterLocale();
progress.completeProcess('Master Locale ', true);
} catch (error) {
progress.completeProcess('Master Locale ', false);
//NOTE:- Continue locale creation in case of master locale error
handleAndLogError(error, { ...this.config.context });
}
}
private async processLocaleCreation(progress: any): Promise<void> {
progress.startProcess('Create').updateStatus('Creating locales...', 'Create');
log.debug('Creating locales', this.config.context);
try {
await this.createLocales();
progress.completeProcess('Create', true);
} catch (error) {
progress.completeProcess('Create', false);
throw error;
}
}
private async processLocaleUpdate(progress: any): Promise<void> {
progress.startProcess('Update').updateStatus('Updating locales...', 'Update');
log.debug('Updating locales', this.config.context);
try {
await this.updateLocales();
progress.completeProcess('Update', true);
} catch (error) {
progress.completeProcess('Update', false);
throw error;
}
}
private getSourceMasterLangDetails(): Record<string, any> | null {
const details = this.sourceMasterLanguage && Object.values(this.sourceMasterLanguage);
const lang = details?.[0];
if (!lang) {
log.info('No source master language details found', this.config.context);
return null;
}
return lang as Record<string, any>;
}
private async fetchTargetMasterLocale(): Promise<Record<string, any> | null> {
try {
log.debug('Fetching current master language details from stack', this.config.context);
return await this.stackAPIClient.locale(this.masterLanguage.code).fetch();
} catch (error) {
log.debug('Error fetching master language details', this.config.context);
handleAndLogError(error, { ...this.config.context });
return null;
}
}
private logCodeMismatch(sourceCode: string): void {
const targetCode = this.masterLanguage?.code;
const message = `master locale: codes differ (${sourceCode} vs ${targetCode})`;
this.tickProgress(true, message);
log.debug(
`Master Locale language codes do not match. Source: ${sourceCode}, Target: ${targetCode}`,
this.config.context,
);
}
private async handleNameMismatch(source: Record<string, any>, target: Record<string, any>): Promise<void> {
log.debug('Master Locale language name differs between source and destination', this.config.context);
log.debug(`Current: ${target.name}, Source: ${source.name}`, this.config.context);
cliux.print('WARNING!!! The master language name for the source and destination is different.', {
color: 'yellow',
});
cliux.print('WARNING!!! The master language name for the source and destination is different.', {
color: 'yellow',
});
cliux.print(`Old Master Locale language name: ${target.name}`, { color: 'red' });
cliux.print(`New Master Locale language name: ${source.name}`, { color: 'green' });
const langUpdateConfirmation: boolean = await cliux.inquire({
type: 'confirm',
message: 'Are you sure you want to update name of master language?',
name: 'confirmation',
});
if (!langUpdateConfirmation) {
this.tickProgress(true, `${target.name} (skipped update)`);
log.info('Master Locale language update cancelled by user', this.config.context);
return;
}
log.debug('User confirmed master language update', this.config.context);
try {
const updatePayload = { ...source, uid: target.uid };
const langUpdateRequest = this.stackAPIClient.locale(source.code);
langUpdateRequest.name = source.name;
await langUpdateRequest.update(updatePayload);
this.tickProgress(true, `${source.name} (updated)`);
log.success(
`Successfully updated master language name from '${target.name}' to '${source.name}'`,
this.config.context,
);
} catch (error) {
this.tickProgress(false, source.name, error?.message || 'Failed to update master locale');
throw error;
}
}
private tickProgress(success: boolean, message: string, error?: string): void {
this.progressManager?.tick(success, `master locale: ${message}`, error || null, 'Master Locale ');
}
}