Skip to content

Commit

Permalink
Merge branch 'main' into test-rewrite-internet
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Feb 14, 2022
2 parents ccc3a97 + ff97431 commit 3438839
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions scripts/generateLocales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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`;
Expand All @@ -160,7 +189,6 @@ for (const locale of locales) {
generateLocaleFile(locale);

// src/locales/<locale>/index.ts
const pathModules = resolve(pathLocales, locale);
generateLocalesIndexFile(
pathModules,
locale,
Expand Down

0 comments on commit 3438839

Please sign in to comment.