aboutsummaryrefslogtreecommitdiff
path: root/scripts/generateLocales.ts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/generateLocales.ts')
-rw-r--r--scripts/generateLocales.ts42
1 files changed, 35 insertions, 7 deletions
diff --git a/scripts/generateLocales.ts b/scripts/generateLocales.ts
index 4419e523..eab91b43 100644
--- a/scripts/generateLocales.ts
+++ b/scripts/generateLocales.ts
@@ -3,7 +3,7 @@ import { resolve } from 'node:path';
import type { Options } from 'prettier';
import { format } from 'prettier';
import options from '../.prettierrc.cjs';
-import type { LocaleDefinition } from '../src';
+import type { LocaleDefinition } from '../src/definitions';
import { DEFINITIONS } from '../src/definitions';
// Constants
@@ -94,6 +94,34 @@ function generateLocaleFile(locale: string) {
writeFileSync(resolve(pathLocale, locale + '.ts'), content);
}
+function tryLoadLocalesMainIndexFile(pathModules: string): LocaleDefinition {
+ let localeDef: LocaleDefinition;
+ // This call might fail, if the module setup is broken.
+ // Unfortunately, we try to fix it with this script
+ // Thats why have a fallback logic here, we only need the title and separator anyway
+ try {
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
+ localeDef = require(pathModules).default;
+ } catch (e) {
+ try {
+ console.log(
+ `Failed to load ${pathModules}. Attempting manual parse instead...`
+ );
+ const localeIndex = readFileSync(
+ resolve(pathModules, 'index.ts'),
+ 'utf-8'
+ );
+ localeDef = {
+ title: localeIndex.match(/title: '(.*)',/)[1],
+ separator: localeIndex.match(/separator: '(.*)',/)?.[1],
+ };
+ } catch {
+ console.error(`Failed to load ${pathModules} or manually parse it.`, e);
+ }
+ }
+ return localeDef;
+}
+
function generateLocalesIndexFile(
path: string,
name: string,
@@ -145,11 +173,12 @@ let localeIndexLocales = 'const locales: KnownLocales = {\n';
let localizationLocales = '| Locale | Name |\n| :--- | :--- |\n';
for (const locale of locales) {
- // eslint-disable-next-line @typescript-eslint/no-var-requires
- const localeDef: LocaleDefinition = require('../src/locales/' +
- locale).default;
- const localeTitle = localeDef.title;
- const localeSeparator = localeDef.separator;
+ const pathModules = resolve(pathLocales, locale);
+
+ const localeDef = tryLoadLocalesMainIndexFile(pathModules);
+ // We use a fallback here to at least generate a working file.
+ const localeTitle = localeDef?.title ?? `TODO: Insert Title for ${locale}`;
+ const localeSeparator = localeDef?.separator;
localeIndexImports += `import ${locale} from './${locale}';\n`;
localeIndexType += ` | '${locale}'\n`;
@@ -160,7 +189,6 @@ for (const locale of locales) {
generateLocaleFile(locale);
// src/locales/<locale>/index.ts
- const pathModules = resolve(pathLocales, locale);
generateLocalesIndexFile(
pathModules,
locale,