Skip to content

Commit ff97431

Browse files
authored
fix: handle missing or broken locales main index files (#478)
1 parent 829f7ff commit ff97431

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

scripts/generateLocales.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { resolve } from 'node:path';
33
import type { Options } from 'prettier';
44
import { format } from 'prettier';
55
import options from '../.prettierrc.cjs';
6-
import type { LocaleDefinition } from '../src';
6+
import type { LocaleDefinition } from '../src/definitions';
77
import { 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(/title: '(.*)',/)[1],
116+
separator: localeIndex.match(/separator: '(.*)',/)?.[1],
117+
};
118+
} catch {
119+
console.error(`Failed to load ${pathModules} or manually parse it.`, e);
120+
}
121+
}
122+
return localeDef;
123+
}
124+
97125
function generateLocalesIndexFile(
98126
path: string,
99127
name: string,
@@ -145,11 +173,12 @@ let localeIndexLocales = 'const locales: KnownLocales = {\n';
145173
let localizationLocales = '| Locale | Name |\n| :--- | :--- |\n';
146174

147175
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;
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

Comments
 (0)