-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocales.ts
More file actions
158 lines (140 loc) · 5.91 KB
/
locales.ts
File metadata and controls
158 lines (140 loc) · 5.91 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
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 LocaleExport extends BaseClass {
private stackAPIClient: ReturnType<ContentstackClient['stack']>;
public exportConfig: ExportConfig;
private masterLocaleConfig: { dirName: string; fileName: string; requiredKeys: string[] };
private qs: {
include_count: boolean;
asc: string;
only: {
BASE: string[];
};
skip?: number;
};
private localeConfig: {
dirName?: string;
fileName?: string;
requiredKeys?: string[];
fetchConcurrency?: number;
writeConcurrency?: number;
limit?: number;
};
private localesPath: string;
private masterLocale: Record<string, Record<string, string>>;
private locales: Record<string, Record<string, string>>;
constructor({ exportConfig, stackAPIClient }: ModuleClassParams) {
super({ exportConfig, stackAPIClient });
this.stackAPIClient = stackAPIClient;
this.localeConfig = exportConfig.modules.locales;
this.masterLocaleConfig = exportConfig.modules.masterLocale;
this.qs = {
include_count: true,
asc: 'updated_at',
only: {
BASE: this.localeConfig.requiredKeys,
},
};
this.localesPath = path.resolve(
sanitizePath(getExportBasePath(exportConfig)),
sanitizePath(this.localeConfig.dirName),
);
this.locales = {};
this.masterLocale = {};
this.exportConfig.context.module = MODULE_CONTEXTS.LOCALES;
this.currentModuleName = MODULE_NAMES[MODULE_CONTEXTS.LOCALES];
}
async start() {
try {
log.debug('Starting locales export process...', this.exportConfig.context);
// Get locales count and setup with loading spinner
const [totalCount] = await this.withLoadingSpinner('LOCALES: Analyzing locales...', async () => {
await fsUtil.makeDirectory(this.localesPath);
log.debug(`Locales path: ${this.localesPath}`, this.exportConfig.context);
const countResponse = await this.stackAPIClient
.locale()
.query({ ...this.qs, include_count: true, limit: 1 })
.find();
return [countResponse.count || 0];
});
// Create simple progress manager with total count
const progress = this.createSimpleProgress(this.currentModuleName, totalCount);
// Fetch locales
progress.updateStatus('Fetching locale definitions...');
await this.getLocales();
log.debug(
`Retrieved ${Object.keys(this.locales || {}).length} locales and ${
Object.keys(this.masterLocale || {}).length
} master locales`,
this.exportConfig.context,
);
const localesFilePath = path.join(this.localesPath, this.localeConfig.fileName);
const masterLocaleFilePath = path.join(this.localesPath, this.masterLocaleConfig.fileName);
log.debug(`Writing locales to: ${localesFilePath}`, this.exportConfig.context);
fsUtil.writeFile(localesFilePath, this.locales);
log.debug(`Writing master locale to: ${masterLocaleFilePath}`, this.exportConfig.context);
fsUtil.writeFile(masterLocaleFilePath, this.masterLocale);
this.completeProgressWithMessage();
} catch (error) {
handleAndLogError(error, { ...this.exportConfig.context });
this.completeProgress(false, error?.message || 'Locales export failed');
throw error;
}
}
async getLocales(skip: number = 0): Promise<any> {
if (skip) {
this.qs.skip = skip;
log.debug(`Fetching locales with skip: ${skip}`, this.exportConfig.context);
}
log.debug(`Query parameters: ${JSON.stringify(this.qs)}`, this.exportConfig.context);
let localesFetchResponse = await this.stackAPIClient.locale().query(this.qs).find();
log.debug(
`Fetched ${localesFetchResponse.items?.length || 0} locales out of total ${localesFetchResponse.count}`,
this.exportConfig.context,
);
if (Array.isArray(localesFetchResponse.items) && localesFetchResponse.items.length > 0) {
log.debug(`Processing ${localesFetchResponse.items.length} locales`, this.exportConfig.context);
this.sanitizeAttribs(localesFetchResponse.items);
skip += this.localeConfig.limit || 100;
if (skip > localesFetchResponse.count) {
log.debug('Completed fetching all locales', this.exportConfig.context);
return;
}
log.debug(`Continuing to fetch locales with skip: ${skip}`, this.exportConfig.context);
return await this.getLocales(skip);
} else {
log.debug('No locales found to process', this.exportConfig.context);
}
}
sanitizeAttribs(locales: Record<string, string>[]) {
log.debug(`Sanitizing ${locales.length} locales`, this.exportConfig.context);
locales.forEach((locale: Record<string, string>) => {
for (let key in locale) {
if (this.localeConfig.requiredKeys.indexOf(key) === -1) {
delete locale[key];
}
}
let uid = locale.uid;
if (this.exportConfig?.master_locale?.code === locale?.code) {
log.debug(`Adding locale ${locale.uid} to master locale`, this.exportConfig.context);
this.masterLocale[uid] = locale;
// Track progress for master locale
this.progressManager?.tick(true, `master-locale: ${uid}`);
} else {
log.debug(`Adding locale ${locale.uid} to regular locales`, this.exportConfig.context);
this.locales[uid] = locale;
// Track progress for regular locale
this.progressManager?.tick(true, `locale: ${uid}`);
}
});
log.debug(
`Sanitization complete. Master locales: ${Object.keys(this.masterLocale || {}).length}, Regular locales: ${
Object.keys(this.locales || {}).length
}`,
this.exportConfig.context,
);
}
}