@@ -3,7 +3,7 @@ import { resolve } from 'node:path';
33import type { Options } from 'prettier' ;
44import { format } from 'prettier' ;
55import options from '../.prettierrc.cjs' ;
6- import type { LocaleDefinition } from '../src' ;
6+ import type { LocaleDefinition } from '../src/definitions ' ;
77import { DEFINITIONS } from '../src/definitions' ;
88
99// Constants
@@ -94,6 +94,34 @@ function generateLocaleFile(locale: string) {
9494 writeFileSync ( resolve ( pathLocale , locale + '.ts' ) , content ) ;
9595}
9696
97+ function tryLoadLocalesMainIndexFile ( pathModules : string ) : LocaleDefinition {
98+ let localeDef : LocaleDefinition ;
99+ // This call might fail, if the module setup is broken.
100+ // Unfortunately, we try to fix it with this script
101+ // Thats why have a fallback logic here, we only need the title and separator anyway
102+ try {
103+ // eslint-disable-next-line @typescript-eslint/no-var-requires
104+ localeDef = require ( pathModules ) . default ;
105+ } catch ( e ) {
106+ try {
107+ console . log (
108+ `Failed to load ${ pathModules } . Attempting manual parse instead...`
109+ ) ;
110+ const localeIndex = readFileSync (
111+ resolve ( pathModules , 'index.ts' ) ,
112+ 'utf-8'
113+ ) ;
114+ localeDef = {
115+ title : localeIndex . match ( / t i t l e : ' ( .* ) ' , / ) [ 1 ] ,
116+ separator : localeIndex . match ( / s e p a r a t o r : ' ( .* ) ' , / ) ?. [ 1 ] ,
117+ } ;
118+ } catch {
119+ console . error ( `Failed to load ${ pathModules } or manually parse it.` , e ) ;
120+ }
121+ }
122+ return localeDef ;
123+ }
124+
97125function generateLocalesIndexFile (
98126 path : string ,
99127 name : string ,
@@ -145,11 +173,12 @@ let localeIndexLocales = 'const locales: KnownLocales = {\n';
145173let localizationLocales = '| Locale | Name |\n| :--- | :--- |\n' ;
146174
147175for ( const locale of locales ) {
148- // eslint-disable-next-line @typescript-eslint/no-var-requires
149- const localeDef : LocaleDefinition = require ( '../src/locales/' +
150- locale ) . default ;
151- const localeTitle = localeDef . title ;
152- const localeSeparator = localeDef . separator ;
176+ const pathModules = resolve ( pathLocales , locale ) ;
177+
178+ const localeDef = tryLoadLocalesMainIndexFile ( pathModules ) ;
179+ // We use a fallback here to at least generate a working file.
180+ const localeTitle = localeDef ?. title ?? `TODO: Insert Title for ${ locale } ` ;
181+ const localeSeparator = localeDef ?. separator ;
153182
154183 localeIndexImports += `import ${ locale } from './${ locale } ';\n` ;
155184 localeIndexType += ` | '${ locale } '\n` ;
@@ -160,7 +189,6 @@ for (const locale of locales) {
160189 generateLocaleFile ( locale ) ;
161190
162191 // src/locales/<locale>/index.ts
163- const pathModules = resolve ( pathLocales , locale ) ;
164192 generateLocalesIndexFile (
165193 pathModules ,
166194 locale ,
0 commit comments