@@ -17,8 +17,7 @@ import { resolve } from 'node:path';
1717import type { Options } from 'prettier' ;
1818import { format } from 'prettier' ;
1919import options from '../.prettierrc.cjs' ;
20- import type { LocaleDefinition } from '../src/definitions' ;
21- import { DEFINITIONS } from '../src/definitions' ;
20+ import type { Definitions , LocaleDefinition } from '../src/definitions' ;
2221
2322// Constants
2423
@@ -33,6 +32,37 @@ const pathDocsApiLocalization = resolve(
3332 'localization.md'
3433) ;
3534
35+ // Workaround for nameOf<T>
36+ type PascalCase < S extends string > = S extends `${infer P1 } _${infer P2 } `
37+ ? `${Capitalize < P1 > } ${PascalCase < P2 > } `
38+ : Capitalize < S > ;
39+
40+ type DefinitionsType = {
41+ [ key in keyof Definitions ] : PascalCase < `${key } Definitions`> ;
42+ } ;
43+
44+ /**
45+ * The types of the definitions.
46+ */
47+ const definitionsTypes : DefinitionsType = {
48+ address : 'AddressDefinitions' ,
49+ animal : 'AnimalDefinitions' ,
50+ commerce : 'CommerceDefinitions' ,
51+ company : 'CompanyDefinitions' ,
52+ database : 'DatabaseDefinitions' ,
53+ date : 'DateDefinitions' ,
54+ finance : 'FinanceDefinitions' ,
55+ hacker : 'HackerDefinitions' ,
56+ internet : 'InternetDefinitions' ,
57+ lorem : 'LoremDefinitions' ,
58+ music : 'MusicDefinitions' ,
59+ name : 'NameDefinitions' ,
60+ phone_number : 'PhoneNumberDefinitions' ,
61+ system : 'SystemDefinitions' ,
62+ vehicle : 'VehicleDefinitions' ,
63+ word : 'WordDefinitions' ,
64+ } ;
65+
3666const prettierTsOptions : Options = { ...options , parser : 'typescript' } ;
3767const prettierMdOptions : Options = { ...options , parser : 'markdown' } ;
3868
@@ -73,13 +103,6 @@ function escapeField(module: string): string {
73103 }
74104}
75105
76- function containsAll ( checked ?: string [ ] , expected ?: string [ ] ) : boolean {
77- if ( expected == null || checked == null ) {
78- return true ;
79- }
80- return expected . every ( ( c ) => checked . includes ( c ) ) ;
81- }
82-
83106function generateLocaleFile ( locale : string ) : void {
84107 let content = `
85108 ${ autoGeneratedCommentHeader }
@@ -135,54 +158,47 @@ function generateLocalesIndexFile(
135158 name : string ,
136159 type : string ,
137160 depth : number ,
138- extra : string = '' ,
139- expected ?: string [ ]
161+ extra : string = ''
140162) : void {
141163 let modules = readdirSync ( path ) ;
142164 modules = removeIndexTs ( modules ) ;
143165 modules = removeTsSuffix ( modules ) ;
144- const importType = type ;
145- if ( ! containsAll ( modules , expected ) ) {
146- type = `Partial<${ type } >` ;
147- }
166+
167+ const content = [ autoGeneratedCommentHeader ] ;
148168 let fieldType = '' ;
149- let asType = '' ;
150- if ( ! containsAll ( expected , modules ) ) {
151- asType = ` as ${ type } ` ;
152- } else if ( type !== 'any' ) {
153- fieldType = `: ${ type } ` ;
154- }
155- let content = `${ autoGeneratedCommentHeader } \n` ;
156169 if ( type !== 'any' ) {
157- content += ` import type { ${ importType . replace (
158- / \[ .* / ,
159- ''
160- ) } } from '..${ '/..' . repeat ( depth ) } ';\n`;
170+ fieldType = `: ${ type } ` ;
171+ content . push (
172+ `import type { ${ type . replace ( / \[ .* / , '' ) } } from '..${ '/..' . repeat (
173+ depth
174+ ) } ';\n`
175+ ) ;
161176 }
162- content += ` ${ modules
163- . map ( ( module ) => `import ${ escapeImport ( module ) } from './${ module } ';` )
164- . join ( '\n' ) }
177+ content . push (
178+ ... modules . map ( ( m ) => `import ${ escapeImport ( m ) } from './${ m } ';` )
179+ ) ;
165180
166- const ${ name } ${ fieldType } = {
181+ content . push ( `\nconst ${ name } ${ fieldType } = {
167182 ${ extra }
168183 ${ modules . map ( ( module ) => `${ escapeField ( module ) } ,` ) . join ( '\n' ) }
169- }${ asType } ;
184+ };\n` ) ;
170185
171- export default ${ name } ;
172- ` ;
173- content = format ( content , prettierTsOptions ) ;
174- writeFileSync ( resolve ( path , 'index.ts' ) , content ) ;
186+ content . push ( `export default ${ name } ;` ) ;
187+
188+ writeFileSync (
189+ resolve ( path , 'index.ts' ) ,
190+ format ( content . join ( '\n' ) , prettierTsOptions )
191+ ) ;
175192}
176193
177194function generateRecursiveModuleIndexes (
178195 path : string ,
179196 name : string ,
180197 definition : string ,
181198 depth : number ,
182- extra ?: string ,
183- moduleFiles ?: string [ ]
199+ extra ?: string
184200) : void {
185- generateLocalesIndexFile ( path , name , definition , depth , extra , moduleFiles ) ;
201+ generateLocalesIndexFile ( path , name , definition , depth , extra ) ;
186202
187203 let submodules = readdirSync ( path ) ;
188204 submodules = removeIndexTs ( submodules ) ;
@@ -193,18 +209,10 @@ function generateRecursiveModuleIndexes(
193209 if ( lstatSync ( pathModule ) . isDirectory ( ) ) {
194210 let moduleDefinition =
195211 definition === 'any' ? 'any' : `${ definition } ['${ submodule } ']` ;
196- let moduleFiles : string [ ] ;
197212
198- // Overwrite types of src/locales/<locale>/<module>/index.ts for known DEFINITIONS
213+ // Overwrite types of src/locales/<locale>/<module>/index.ts for known definition types
199214 if ( depth === 1 ) {
200- moduleFiles = DEFINITIONS [ submodule ] ;
201- if ( moduleFiles == null ) {
202- moduleDefinition = 'any' ;
203- } else {
204- moduleDefinition = `${ submodule . replace ( / ( ^ | _ ) ( [ a - z ] ) / g, ( s ) =>
205- s . replace ( '_' , '' ) . toUpperCase ( )
206- ) } Definitions`;
207- }
215+ moduleDefinition = definitionsTypes [ submodule ] ?? 'any' ;
208216 }
209217
210218 // Recursive
@@ -213,8 +221,7 @@ function generateRecursiveModuleIndexes(
213221 submodule ,
214222 moduleDefinition ,
215223 depth + 1 ,
216- undefined ,
217- moduleFiles
224+ undefined
218225 ) ;
219226 }
220227 }
0 commit comments