@@ -3,7 +3,7 @@ import { resolve } from 'node:path';
3
3
import type { Options } from 'prettier' ;
4
4
import { format } from 'prettier' ;
5
5
import options from '../.prettierrc.cjs' ;
6
- import type { LocaleDefinition } from '../src' ;
6
+ import type { LocaleDefinition } from '../src/definitions ' ;
7
7
import { DEFINITIONS } from '../src/definitions' ;
8
8
9
9
// Constants
@@ -94,6 +94,34 @@ function generateLocaleFile(locale: string) {
94
94
writeFileSync ( resolve ( pathLocale , locale + '.ts' ) , content ) ;
95
95
}
96
96
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
+
97
125
function generateLocalesIndexFile (
98
126
path : string ,
99
127
name : string ,
@@ -145,11 +173,12 @@ let localeIndexLocales = 'const locales: KnownLocales = {\n';
145
173
let localizationLocales = '| Locale | Name |\n| :--- | :--- |\n' ;
146
174
147
175
for ( 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 ;
153
182
154
183
localeIndexImports += `import ${ locale } from './${ locale } ';\n` ;
155
184
localeIndexType += ` | '${ locale } '\n` ;
@@ -160,7 +189,6 @@ for (const locale of locales) {
160
189
generateLocaleFile ( locale ) ;
161
190
162
191
// src/locales/<locale>/index.ts
163
- const pathModules = resolve ( pathLocales , locale ) ;
164
192
generateLocalesIndexFile (
165
193
pathModules ,
166
194
locale ,
0 commit comments