@@ -23,7 +23,7 @@ import { resolve } from 'node:path';
2323import type { Options } from 'prettier' ;
2424import { format } from 'prettier' ;
2525import options from '../.prettierrc.cjs' ;
26- import type { Definitions , LocaleDefinition } from '../src/definitions' ;
26+ import type { LocaleDefinition , MetadataDefinitions } from '../src/definitions' ;
2727
2828// Constants
2929
@@ -45,7 +45,7 @@ type PascalCase<S extends string> = S extends `${infer P1}_${infer P2}`
4545 : Capitalize < S > ;
4646
4747type DefinitionsType = {
48- [ key in keyof Definitions ] : PascalCase < `${key } Definitions`> ;
48+ [ key in keyof LocaleDefinition ] -? : PascalCase < `${key } Definitions`> ;
4949} ;
5050
5151/**
@@ -64,6 +64,7 @@ const definitionsTypes: DefinitionsType = {
6464 internet : 'InternetDefinitions' ,
6565 location : 'LocationDefinitions' ,
6666 lorem : 'LoremDefinitions' ,
67+ metadata : 'MetadataDefinitions' ,
6768 music : 'MusicDefinitions' ,
6869 person : 'PersonDefinitions' ,
6970 phone_number : 'PhoneNumberDefinitions' ,
@@ -153,43 +154,11 @@ function generateLocaleFile(locale: string): void {
153154 writeFileSync ( resolve ( pathLocale , `${ locale } .ts` ) , content ) ;
154155}
155156
156- function tryLoadLocalesMainIndexFile (
157- pathModules : string
158- ) : LocaleDefinition | undefined {
159- let localeDef : LocaleDefinition | undefined ;
160- // This call might fail, if the module setup is broken.
161- // Unfortunately, we try to fix it with this script
162- // Thats why have a fallback logic here, we only need the title anyway
163- try {
164- // eslint-disable-next-line @typescript-eslint/no-var-requires
165- localeDef = require ( pathModules ) . default ;
166- } catch ( e ) {
167- try {
168- console . log (
169- `Failed to load ${ pathModules } . Attempting manual parse instead...`
170- ) ;
171- const localeIndex = readFileSync (
172- resolve ( pathModules , 'index.ts' ) ,
173- 'utf-8'
174- ) ;
175- const title = localeIndex . match ( / t i t l e : ' ( .* ) ' , / ) ?. [ 1 ] ;
176- if ( title ) {
177- localeDef = { title } ;
178- }
179- } catch {
180- console . error ( `Failed to load ${ pathModules } or manually parse it.` , e ) ;
181- }
182- }
183-
184- return localeDef ;
185- }
186-
187157function generateLocalesIndexFile (
188158 path : string ,
189159 name : string ,
190160 type : string ,
191- depth : number ,
192- extra : string = ''
161+ depth : number
193162) : void {
194163 let modules = readdirSync ( path ) ;
195164 modules = removeIndexTs ( modules ) ;
@@ -214,7 +183,6 @@ function generateLocalesIndexFile(
214183 ) ;
215184
216185 content . push ( `\nconst ${ name } ${ fieldType } = {
217- ${ extra }
218186 ${ modules . map ( ( module ) => `${ escapeField ( name , module ) } ,` ) . join ( '\n' ) }
219187 };\n` ) ;
220188
@@ -230,10 +198,9 @@ function generateRecursiveModuleIndexes(
230198 path : string ,
231199 name : string ,
232200 definition : string ,
233- depth : number ,
234- extra ?: string
201+ depth : number
235202) : void {
236- generateLocalesIndexFile ( path , name , definition , depth , extra ) ;
203+ generateLocalesIndexFile ( path , name , definition , depth ) ;
237204
238205 let submodules = readdirSync ( path ) ;
239206 submodules = removeIndexTs ( submodules ) ;
@@ -255,8 +222,7 @@ function generateRecursiveModuleIndexes(
255222 pathModule ,
256223 submodule ,
257224 moduleDefinition ,
258- depth + 1 ,
259- undefined
225+ depth + 1
260226 ) ;
261227 }
262228 }
@@ -311,10 +277,21 @@ let localizationLocales = '| Locale | Name | Faker |\n| :--- | :--- | :--- |\n';
311277
312278for ( const locale of locales ) {
313279 const pathModules = resolve ( pathLocales , locale ) ;
314-
315- const localeDef = tryLoadLocalesMainIndexFile ( pathModules ) ;
316- // We use a fallback here to at least generate a working file.
317- const localeTitle = localeDef ?. title ?? `TODO: Insert Title for ${ locale } ` ;
280+ const pathMetadata = resolve ( pathModules , 'metadata.ts' ) ;
281+ let localeTitle = 'No title found' ;
282+ try {
283+ // eslint-disable-next-line @typescript-eslint/no-var-requires
284+ const metadata : MetadataDefinitions = require ( pathMetadata ) . default ;
285+ localeTitle = metadata . title ;
286+ if ( ! localeTitle ) {
287+ throw new Error ( `No title property found on ${ JSON . stringify ( metadata ) } ` ) ;
288+ }
289+ } catch ( e ) {
290+ console . error (
291+ `Failed to load ${ pathMetadata } . Please make sure the file exists and exports MetadataDefinitions.`
292+ ) ;
293+ console . error ( e ) ;
294+ }
318295
319296 const localizedFaker = `faker${ locale . replace ( / ^ ( [ a - z ] + ) / , ( part ) =>
320297 part . toUpperCase ( )
@@ -330,13 +307,7 @@ for (const locale of locales) {
330307 generateLocaleFile ( locale ) ;
331308
332309 // src/locales/**/index.ts
333- generateRecursiveModuleIndexes (
334- pathModules ,
335- locale ,
336- 'LocaleDefinition' ,
337- 1 ,
338- `title: '${ localeTitle } ',`
339- ) ;
310+ generateRecursiveModuleIndexes ( pathModules , locale , 'LocaleDefinition' , 1 ) ;
340311}
341312
342313// src/locale/index.ts
0 commit comments