Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle missing or broken locales main index files #478

Merged
merged 4 commits into from
Feb 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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