diff options
| author | ST-DDT <[email protected]> | 2022-05-05 00:09:25 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-05-05 00:09:25 +0200 |
| commit | 984fbb445ff3be3658535bf98916ce5f38943fbf (patch) | |
| tree | afab5f5137ee3069690944d11285d6d5a4e2fe35 /scripts | |
| parent | f1dba1bb0b90dc5e1e3ee096f937a1d4df6acf03 (diff) | |
| download | faker-984fbb445ff3be3658535bf98916ce5f38943fbf.tar.xz faker-984fbb445ff3be3658535bf98916ce5f38943fbf.zip | |
fix(generate:locale): make the definition types extendible (#915)
Co-authored-by: Piotr Kuczynski <[email protected]>
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/generateLocales.ts | 107 |
1 files changed, 57 insertions, 50 deletions
diff --git a/scripts/generateLocales.ts b/scripts/generateLocales.ts index fc2c7ebc..7e2ca837 100644 --- a/scripts/generateLocales.ts +++ b/scripts/generateLocales.ts @@ -17,8 +17,7 @@ import { resolve } from 'node:path'; import type { Options } from 'prettier'; import { format } from 'prettier'; import options from '../.prettierrc.cjs'; -import type { LocaleDefinition } from '../src/definitions'; -import { DEFINITIONS } from '../src/definitions'; +import type { Definitions, LocaleDefinition } from '../src/definitions'; // Constants @@ -33,6 +32,37 @@ const pathDocsApiLocalization = resolve( 'localization.md' ); +// Workaround for nameOf<T> +type PascalCase<S extends string> = S extends `${infer P1}_${infer P2}` + ? `${Capitalize<P1>}${PascalCase<P2>}` + : Capitalize<S>; + +type DefinitionsType = { + [key in keyof Definitions]: PascalCase<`${key}Definitions`>; +}; + +/** + * The types of the definitions. + */ +const definitionsTypes: DefinitionsType = { + address: 'AddressDefinitions', + animal: 'AnimalDefinitions', + commerce: 'CommerceDefinitions', + company: 'CompanyDefinitions', + database: 'DatabaseDefinitions', + date: 'DateDefinitions', + finance: 'FinanceDefinitions', + hacker: 'HackerDefinitions', + internet: 'InternetDefinitions', + lorem: 'LoremDefinitions', + music: 'MusicDefinitions', + name: 'NameDefinitions', + phone_number: 'PhoneNumberDefinitions', + system: 'SystemDefinitions', + vehicle: 'VehicleDefinitions', + word: 'WordDefinitions', +}; + const prettierTsOptions: Options = { ...options, parser: 'typescript' }; const prettierMdOptions: Options = { ...options, parser: 'markdown' }; @@ -73,13 +103,6 @@ function escapeField(module: string): string { } } -function containsAll(checked?: string[], expected?: string[]): boolean { - if (expected == null || checked == null) { - return true; - } - return expected.every((c) => checked.includes(c)); -} - function generateLocaleFile(locale: string): void { let content = ` ${autoGeneratedCommentHeader} @@ -135,43 +158,37 @@ function generateLocalesIndexFile( name: string, type: string, depth: number, - extra: string = '', - expected?: string[] + extra: string = '' ): void { let modules = readdirSync(path); modules = removeIndexTs(modules); modules = removeTsSuffix(modules); - const importType = type; - if (!containsAll(modules, expected)) { - type = `Partial<${type}>`; - } + + const content = [autoGeneratedCommentHeader]; let fieldType = ''; - let asType = ''; - if (!containsAll(expected, modules)) { - asType = ` as ${type}`; - } else if (type !== 'any') { - fieldType = `: ${type}`; - } - let content = `${autoGeneratedCommentHeader}\n`; if (type !== 'any') { - content += ` import type { ${importType.replace( - /\[.*/, - '' - )} } from '..${'/..'.repeat(depth)}';\n`; + fieldType = `: ${type}`; + content.push( + `import type { ${type.replace(/\[.*/, '')} } from '..${'/..'.repeat( + depth + )}';\n` + ); } - content += ` ${modules - .map((module) => `import ${escapeImport(module)} from './${module}';`) - .join('\n')} + content.push( + ...modules.map((m) => `import ${escapeImport(m)} from './${m}';`) + ); - const ${name}${fieldType} = { + content.push(`\nconst ${name}${fieldType} = { ${extra} ${modules.map((module) => `${escapeField(module)},`).join('\n')} - }${asType}; + };\n`); - export default ${name}; - `; - content = format(content, prettierTsOptions); - writeFileSync(resolve(path, 'index.ts'), content); + content.push(`export default ${name};`); + + writeFileSync( + resolve(path, 'index.ts'), + format(content.join('\n'), prettierTsOptions) + ); } function generateRecursiveModuleIndexes( @@ -179,10 +196,9 @@ function generateRecursiveModuleIndexes( name: string, definition: string, depth: number, - extra?: string, - moduleFiles?: string[] + extra?: string ): void { - generateLocalesIndexFile(path, name, definition, depth, extra, moduleFiles); + generateLocalesIndexFile(path, name, definition, depth, extra); let submodules = readdirSync(path); submodules = removeIndexTs(submodules); @@ -193,18 +209,10 @@ function generateRecursiveModuleIndexes( if (lstatSync(pathModule).isDirectory()) { let moduleDefinition = definition === 'any' ? 'any' : `${definition}['${submodule}']`; - let moduleFiles: string[]; - // Overwrite types of src/locales/<locale>/<module>/index.ts for known DEFINITIONS + // Overwrite types of src/locales/<locale>/<module>/index.ts for known definition types if (depth === 1) { - moduleFiles = DEFINITIONS[submodule]; - if (moduleFiles == null) { - moduleDefinition = 'any'; - } else { - moduleDefinition = `${submodule.replace(/(^|_)([a-z])/g, (s) => - s.replace('_', '').toUpperCase() - )}Definitions`; - } + moduleDefinition = definitionsTypes[submodule] ?? 'any'; } // Recursive @@ -213,8 +221,7 @@ function generateRecursiveModuleIndexes( submodule, moduleDefinition, depth + 1, - undefined, - moduleFiles + undefined ); } } |
