-
-
Notifications
You must be signed in to change notification settings - Fork 919
/
generate-locales.ts
471 lines (396 loc) · 13.6 KB
/
generate-locales.ts
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/usr/bin/env node
/**
* This file contains a script that can be used to update the following files:
*
* - `src/locale/<locale>.ts`
* - `src/locales/<locale>/index.ts`
* - `src/locales/<locale>/<module...>/index.ts`
* - `src/docs/guide/localization.md`
*
* If you wish to edit all/specific locale data files you can do so using the
* `updateLocaleFileHook()` method.
* Please remember to not commit your temporary update code.
*
* Run this script using `pnpm run generate:locales`
*/
import { constants } from 'node:fs';
import { access, readFile, readdir, stat, writeFile } from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { LocaleDefinition, MetadataDefinition } from '../src/definitions';
import { keys } from '../src/internal/keys';
import { formatMarkdown, formatTypescript } from './apidocs/utils/format';
// Constants
const pathRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const pathLocale = resolve(pathRoot, 'src', 'locale');
const pathLocales = resolve(pathRoot, 'src', 'locales');
const pathLocaleIndex = resolve(pathLocale, 'index.ts');
const pathLocalesIndex = resolve(pathLocales, 'index.ts');
const pathDocsGuideLocalization = resolve(
pathRoot,
'docs',
'guide',
'localization.md'
);
// Workaround for nameOf<T>
type PascalCase<TName extends string> =
TName extends `${infer Prefix}_${infer Remainder}`
? `${Capitalize<Prefix>}${PascalCase<Remainder>}`
: Capitalize<TName>;
type DefinitionType = {
[key in keyof LocaleDefinition]-?: PascalCase<`${key}Definition`>;
};
/**
* The types of the definitions.
*/
const definitionsTypes: DefinitionType = {
airline: 'AirlineDefinition',
animal: 'AnimalDefinition',
color: 'ColorDefinition',
commerce: 'CommerceDefinition',
company: 'CompanyDefinition',
database: 'DatabaseDefinition',
date: 'DateDefinition',
finance: 'FinanceDefinition',
food: 'FoodDefinition',
hacker: 'HackerDefinition',
internet: 'InternetDefinition',
location: 'LocationDefinition',
lorem: 'LoremDefinition',
metadata: 'MetadataDefinition',
music: 'MusicDefinition',
person: 'PersonDefinition',
phone_number: 'PhoneNumberDefinition',
science: 'ScienceDefinition',
system: 'SystemDefinition',
vehicle: 'VehicleDefinition',
word: 'WordDefinition',
};
const scriptCommand = 'pnpm run generate:locales';
const autoGeneratedCommentHeader = `/*
* This file is automatically generated.
* Run '${scriptCommand}' to update.
*/`;
// Helper functions
function removeIndexTs(files: string[]): string[] {
const index = files.indexOf('index.ts');
if (index !== -1) {
files.splice(index, 1);
}
return files;
}
function removeTsSuffix(files: string[]): string[] {
return files.map((file) => file.replace('.ts', ''));
}
function escapeImport(parent: string, module: string): string {
if (['name', 'type', 'switch', parent].includes(module)) {
return `${module}_`;
}
return module;
}
function escapeField(parent: string, module: string): string {
if (['name', 'type', 'switch', parent].includes(module)) {
return `${module}: ${module}_`;
}
return module;
}
async function generateLocaleFile(locale: string): Promise<void> {
const parts = locale.split('_');
const locales = [locale];
for (let i = parts.length - 1; i > 0; i--) {
const fallback = parts.slice(0, i).join('_');
try {
await access(resolve(pathLocales, fallback), constants.R_OK);
locales.push(fallback);
} catch {
// file is missing
}
}
// TODO @Shinigami92 2023-03-07: Remove 'en' fallback in a separate PR
if (locales.at(-1) !== 'en' && locale !== 'base') {
locales.push('en');
}
if (locales.at(-1) !== 'base') {
locales.push('base');
}
let content = `
${autoGeneratedCommentHeader}
import { Faker } from '../faker';
${locales
.map((imp) => `import ${imp} from '../locales/${imp}';`)
.join('\n')}
export const faker = new Faker({
locale: ${
locales.length === 1 ? locales[0] : `[${locales.join(', ')}]`
},
});
`;
content = await formatTypescript(content);
return writeFile(resolve(pathLocale, `${locale}.ts`), content);
}
async function generateLocalesIndexFile(
path: string,
name: string,
type: string,
depth: number
): Promise<void> {
let modules = await readdir(path);
modules = modules.filter((file) => !file.startsWith('.'));
modules = removeIndexTs(modules);
modules = removeTsSuffix(modules);
modules.sort();
const content = [autoGeneratedCommentHeader];
let fieldType = '';
if (type !== 'any') {
fieldType = `: ${type}`;
content.push(
`import type { ${type.replace(/\[.*/, '')} } from '..${'/..'.repeat(
depth
)}';`
);
}
content.push(
...modules.map(
(module) => `import ${escapeImport(name, module)} from './${module}';`
),
'',
`const ${name}${fieldType} = {
${modules.map((module) => `${escapeField(name, module)},`).join('\n')}
};`,
'',
`export default ${name};`
);
return writeFile(
resolve(path, 'index.ts'),
await formatTypescript(content.join('\n'))
);
}
async function generateRecursiveModuleIndexes(
path: string,
name: string,
definition: string,
depth: number
): Promise<unknown> {
await generateLocalesIndexFile(path, name, definition, depth);
const promises: Array<Promise<unknown>> = [];
let submodules = await readdir(path);
submodules = removeIndexTs(submodules);
for (const submodule of submodules) {
const pathModule = resolve(path, submodule);
await updateLocaleFile(pathModule);
// Only process sub folders recursively
const moduleStat = await stat(pathModule);
if (moduleStat.isDirectory()) {
let moduleDefinition =
definition === 'any' ? 'any' : `${definition}['${submodule}']`;
// Overwrite types of src/locales/<locale>/<module>/index.ts for known definition types
if (depth === 1) {
moduleDefinition = definitionsTypes[submodule] ?? 'any';
}
// Recursive
promises.push(
generateRecursiveModuleIndexes(
pathModule,
submodule,
moduleDefinition,
depth + 1
)
);
}
}
return Promise.all(promises);
}
/**
* Intermediate helper function to allow selectively updating locale data files.
* Use the `updateLocaleFileHook()` method to temporarily add your custom per file processing/update logic.
*
* @param filePath The full file path to the file.
*/
async function updateLocaleFile(filePath: string): Promise<void> {
const fileStat = await stat(filePath);
if (fileStat.isFile()) {
const [locale, moduleKey, entryKey] = filePath
.substring(pathLocales.length + 1, filePath.length - 3)
.split(/[\\/]/);
return updateLocaleFileHook(filePath, locale, moduleKey, entryKey);
}
}
/**
* Use this hook method to selectively update locale data files (not for index.ts files).
* This method is intended to be temporarily overwritten for one-time updates.
*
* @param filePath The full file path to the file.
* @param locale The locale for that file.
* @param definitionKey The definition key of the current file (ex. 'location').
* @param entryName The entry key of the current file (ex. 'state'). Is `undefined` if `definitionKey` is `'metadata'`.
*/
async function updateLocaleFileHook(
filePath: string,
locale: string,
definitionKey: string,
entryName: string | undefined
): Promise<void> {
// this needs to stay so all arguments are "used"
if (filePath === 'never') {
console.log(`${filePath} <-> ${locale} @ ${definitionKey} -> ${entryName}`);
}
return normalizeLocaleFile(filePath, definitionKey);
}
/**
* Normalizes the data of a locale file based on a set of rules.
* Those include:
* - filter the entry list for duplicates
* - limiting the maximum entries of a file to 1000
* - sorting the entries alphabetically
*
* This function mutates the file by reading and writing to it!
*
* @param filePath The full file path to the file.
* @param definitionKey The definition key of the current file (ex. 'location').
*/
async function normalizeLocaleFile(filePath: string, definitionKey: string) {
function normalizeDataRecursive<T>(localeData: T): T {
if (typeof localeData !== 'object' || localeData === null) {
// we can only traverse object-like structs
return localeData;
}
if (Array.isArray(localeData)) {
return (
[...new Set(localeData)]
// limit entries to 1k
.slice(0, 1000)
// sort entries alphabetically
.sort() as T
);
}
const result = {} as T;
for (const key of keys(localeData)) {
result[key] = normalizeDataRecursive(localeData[key]);
}
return result;
}
const legacyDefinitions = ['app', 'cell_phone', 'team'];
const definitionsToSkip = [
'date',
'finance',
'internet',
'location',
'lorem',
'metadata',
'person',
'phone_number',
'system',
'word',
...legacyDefinitions,
];
if (definitionsToSkip.includes(definitionKey)) {
return;
}
console.log(`Running data normalization for:`, filePath);
const fileContent = await readFile(filePath, { encoding: 'utf8' });
const searchString = 'export default ';
const compareIndex = fileContent.indexOf(searchString) + searchString.length;
const compareString = fileContent.substring(compareIndex);
const isDynamicFile = compareString.startsWith('mergeArrays');
const isNonApplicable = compareString.startsWith('null');
const isFrozenData = compareString.startsWith('Object.freeze');
if (isDynamicFile || isNonApplicable || isFrozenData) {
return;
}
const validEntryListStartCharacters = ['[', '{'];
const staticFileOpenSyntax = validEntryListStartCharacters.find(
(validStart) => compareString.startsWith(validStart)
);
if (staticFileOpenSyntax === undefined) {
console.log('Found an unhandled dynamic file:', filePath);
return;
}
const fileContentPreData = fileContent.substring(0, compareIndex);
const fileImport = await import(`file:${filePath}`);
const oldData = fileImport.default;
const localeData = normalizeDataRecursive(oldData);
// We reattach the content before the actual data implementation to keep stuff like comments.
// In the long term we should probably define a whether we want those in the files at all.
const newDataJson = JSON.stringify(localeData);
const newContent = fileContentPreData + newDataJson;
// Exit early if unchanged for performance reasons
if (JSON.stringify(oldData) === newDataJson) {
return;
}
return writeFile(filePath, await formatTypescript(newContent));
}
// Start of actual logic
const locales = await readdir(pathLocales);
removeIndexTs(locales);
let localeIndexImports = '';
let localeIndexExportsIndividual = '';
let localeIndexExportsGrouped = '';
let localesIndexImports = '';
let localizationLocales = '| Locale | Name | Faker |\n| :--- | :--- | :--- |\n';
const promises: Array<Promise<unknown>> = [];
for (const locale of locales) {
const pathModules = resolve(pathLocales, locale);
const pathMetadata = resolve(pathModules, 'metadata.ts');
let localeTitle = 'No title found';
try {
const metadataImport = await import(`file:${pathMetadata}`);
const metadata: MetadataDefinition = metadataImport.default;
const { title } = metadata;
if (!title) {
throw new Error(`No title property found on ${JSON.stringify(metadata)}`);
}
localeTitle = title;
} catch (error) {
console.error(
`Failed to load ${pathMetadata}. Please make sure the file exists and exports a MetadataDefinition.`
);
console.error(error);
}
const localizedFaker = `faker${locale.replace(/^([a-z]+)/, (part) =>
part.toUpperCase()
)}`;
localeIndexImports += `import { faker as ${localizedFaker} } from './${locale}';\n`;
localeIndexExportsIndividual += ` ${localizedFaker},\n`;
localeIndexExportsGrouped += ` ${locale}: ${localizedFaker},\n`;
localesIndexImports += `import { default as ${locale} } from './${locale}';\n`;
localizationLocales += `| \`${locale}\` | ${localeTitle} | \`${localizedFaker}\` |\n`;
promises.push(
// src/locale/<locale>.ts
// eslint-disable-next-line unicorn/prefer-top-level-await -- Disabled for performance
generateLocaleFile(locale),
// src/locales/**/index.ts
// eslint-disable-next-line unicorn/prefer-top-level-await -- Disabled for performance
generateRecursiveModuleIndexes(pathModules, locale, 'LocaleDefinition', 1)
);
}
await Promise.all(promises);
// src/locale/index.ts
let localeIndexContent = `
${autoGeneratedCommentHeader}
${localeIndexImports}
export {
${localeIndexExportsIndividual}
};
export const allFakers = {
${localeIndexExportsGrouped}
} as const;
`;
localeIndexContent = await formatTypescript(localeIndexContent);
await writeFile(pathLocaleIndex, localeIndexContent);
// src/locales/index.ts
let localesIndexContent = `
${autoGeneratedCommentHeader}
${localesIndexImports}
export { ${locales.join(',')} };
export const allLocales = { ${locales.join(',')} };
`;
localesIndexContent = await formatTypescript(localesIndexContent);
await writeFile(pathLocalesIndex, localesIndexContent);
// docs/guide/localization.md
localizationLocales = await formatMarkdown(localizationLocales);
let localizationContent = await readFile(pathDocsGuideLocalization, 'utf8');
localizationContent = localizationContent.replaceAll(
/(^<!-- LOCALES-AUTO-GENERATED-START -->$).*(^<!-- LOCALES-AUTO-GENERATED-END -->$)/gms,
`$1\n\n<!-- Run '${scriptCommand}' to update. -->\n\n${localizationLocales}\n$2`
);
await writeFile(pathDocsGuideLocalization, localizationContent);