@@ -23,7 +23,7 @@ import { resolve } from 'node:path';
23
23
import type { Options } from 'prettier' ;
24
24
import { format } from 'prettier' ;
25
25
import options from '../.prettierrc.cjs' ;
26
- import type { Definitions , LocaleDefinition } from '../src/definitions' ;
26
+ import type { LocaleDefinition , MetadataDefinitions } from '../src/definitions' ;
27
27
28
28
// Constants
29
29
@@ -45,7 +45,7 @@ type PascalCase<S extends string> = S extends `${infer P1}_${infer P2}`
45
45
: Capitalize < S > ;
46
46
47
47
type DefinitionsType = {
48
- [ key in keyof Definitions ] : PascalCase < `${key } Definitions`> ;
48
+ [ key in keyof LocaleDefinition ] -? : PascalCase < `${key } Definitions`> ;
49
49
} ;
50
50
51
51
/**
@@ -64,6 +64,7 @@ const definitionsTypes: DefinitionsType = {
64
64
internet : 'InternetDefinitions' ,
65
65
location : 'LocationDefinitions' ,
66
66
lorem : 'LoremDefinitions' ,
67
+ metadata : 'MetadataDefinitions' ,
67
68
music : 'MusicDefinitions' ,
68
69
person : 'PersonDefinitions' ,
69
70
phone_number : 'PhoneNumberDefinitions' ,
@@ -153,43 +154,11 @@ function generateLocaleFile(locale: string): void {
153
154
writeFileSync ( resolve ( pathLocale , `${ locale } .ts` ) , content ) ;
154
155
}
155
156
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
-
187
157
function generateLocalesIndexFile (
188
158
path : string ,
189
159
name : string ,
190
160
type : string ,
191
- depth : number ,
192
- extra : string = ''
161
+ depth : number
193
162
) : void {
194
163
let modules = readdirSync ( path ) ;
195
164
modules = removeIndexTs ( modules ) ;
@@ -214,7 +183,6 @@ function generateLocalesIndexFile(
214
183
) ;
215
184
216
185
content . push ( `\nconst ${ name } ${ fieldType } = {
217
- ${ extra }
218
186
${ modules . map ( ( module ) => `${ escapeField ( name , module ) } ,` ) . join ( '\n' ) }
219
187
};\n` ) ;
220
188
@@ -230,10 +198,9 @@ function generateRecursiveModuleIndexes(
230
198
path : string ,
231
199
name : string ,
232
200
definition : string ,
233
- depth : number ,
234
- extra ?: string
201
+ depth : number
235
202
) : void {
236
- generateLocalesIndexFile ( path , name , definition , depth , extra ) ;
203
+ generateLocalesIndexFile ( path , name , definition , depth ) ;
237
204
238
205
let submodules = readdirSync ( path ) ;
239
206
submodules = removeIndexTs ( submodules ) ;
@@ -255,8 +222,7 @@ function generateRecursiveModuleIndexes(
255
222
pathModule ,
256
223
submodule ,
257
224
moduleDefinition ,
258
- depth + 1 ,
259
- undefined
225
+ depth + 1
260
226
) ;
261
227
}
262
228
}
@@ -311,10 +277,21 @@ let localizationLocales = '| Locale | Name | Faker |\n| :--- | :--- | :--- |\n';
311
277
312
278
for ( const locale of locales ) {
313
279
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
+ }
318
295
319
296
const localizedFaker = `faker${ locale . replace ( / ^ ( [ a - z ] + ) / , ( part ) =>
320
297
part . toUpperCase ( )
@@ -330,13 +307,7 @@ for (const locale of locales) {
330
307
generateLocaleFile ( locale ) ;
331
308
332
309
// src/locales/**/index.ts
333
- generateRecursiveModuleIndexes (
334
- pathModules ,
335
- locale ,
336
- 'LocaleDefinition' ,
337
- 1 ,
338
- `title: '${ localeTitle } ',`
339
- ) ;
310
+ generateRecursiveModuleIndexes ( pathModules , locale , 'LocaleDefinition' , 1 ) ;
340
311
}
341
312
342
313
// src/locale/index.ts
0 commit comments