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

refactor(locale)!: move title to metadata #1978

Merged
merged 17 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
65 changes: 17 additions & 48 deletions scripts/generateLocales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { resolve } from 'node:path';
import type { Options } from 'prettier';
import { format } from 'prettier';
import options from '../.prettierrc.cjs';
import type { Definitions, LocaleDefinition } from '../src/definitions';
import type { Definitions } from '../src/definitions';

// Constants

Expand Down Expand Up @@ -64,6 +64,7 @@ const definitionsTypes: DefinitionsType = {
internet: 'InternetDefinitions',
location: 'LocationDefinitions',
lorem: 'LoremDefinitions',
metadata: 'MetadataDefinitions',
music: 'MusicDefinitions',
person: 'PersonDefinitions',
phone_number: 'PhoneNumberDefinitions',
Expand Down Expand Up @@ -148,40 +149,11 @@ function generateLocaleFile(locale: string): void {
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 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],
};
} catch {
console.error(`Failed to load ${pathModules} or manually parse it.`, e);
}
}

return localeDef;
}

function generateLocalesIndexFile(
path: string,
name: string,
type: string,
depth: number,
extra: string = ''
depth: number
): void {
let modules = readdirSync(path);
modules = removeIndexTs(modules);
Expand All @@ -206,7 +178,6 @@ function generateLocalesIndexFile(
);

content.push(`\nconst ${name}${fieldType} = {
${extra}
${modules.map((module) => `${escapeField(name, module)},`).join('\n')}
};\n`);

Expand All @@ -222,10 +193,9 @@ function generateRecursiveModuleIndexes(
path: string,
name: string,
definition: string,
depth: number,
extra?: string
depth: number
): void {
generateLocalesIndexFile(path, name, definition, depth, extra);
generateLocalesIndexFile(path, name, definition, depth);

let submodules = readdirSync(path);
submodules = removeIndexTs(submodules);
Expand All @@ -247,8 +217,7 @@ function generateRecursiveModuleIndexes(
pathModule,
submodule,
moduleDefinition,
depth + 1,
undefined
depth + 1
);
}
}
Expand Down Expand Up @@ -303,10 +272,16 @@ let localizationLocales = '| Locale | Name | Faker |\n| :--- | :--- | :--- |\n';

for (const locale of locales) {
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 pathTitle = resolve(pathModules, 'metadata', 'title.ts');
let localeTitle = 'No title found';
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
localeTitle = require(pathTitle).default as string;
} catch (e) {
console.log(
`Failed to load ${pathTitle}. Please make sure the file exists and exports a string.`
);
}

const localizedFaker = `faker${locale.replace(/^([a-z]+)/, (part) =>
part.toUpperCase()
Expand All @@ -322,13 +297,7 @@ for (const locale of locales) {
generateLocaleFile(locale);

// src/locales/**/index.ts
generateRecursiveModuleIndexes(
pathModules,
locale,
'LocaleDefinition',
1,
`title: '${localeTitle}',`
);
generateRecursiveModuleIndexes(pathModules, locale, 'LocaleDefinition', 1);
}

// src/locale/index.ts
Expand Down
13 changes: 6 additions & 7 deletions src/definitions/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { HackerDefinitions } from './hacker';
import type { InternetDefinitions } from './internet';
import type { LocationDefinitions } from './location';
import type { LoremDefinitions } from './lorem';
import type { MetadataDefinitions } from './metadata';
import type { MusicDefinitions } from './music';
import type { PersonDefinitions } from './person';
import type { PhoneNumberDefinitions } from './phone_number';
Expand All @@ -20,8 +21,7 @@ import type { WordDefinitions } from './word';

export type LocaleEntry<T> = Partial<T> &
// Unsupported & custom modules
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Record<string, any>;
Record<string, unknown>;

/**
* The definitions as used by the Faker modules.
Expand All @@ -39,6 +39,7 @@ export interface Definitions {
internet: InternetDefinitions;
location: LocationDefinitions;
lorem: LoremDefinitions;
metadata: MetadataDefinitions;
music: MusicDefinitions;
person: PersonDefinitions;
phone_number: PhoneNumberDefinitions;
Expand All @@ -54,8 +55,6 @@ export interface Definitions {
* that most properties are optional and extra properties are allowed.
*/
export type LocaleDefinition = {
/**
* The English name of the language (and the specific country, if defined).
*/
title: string;
} & LocaleEntry<Definitions>;
metadata: MetadataDefinitions;
} & Partial<Definitions> &
Record<string, Record<string, unknown>>;
1 change: 1 addition & 0 deletions src/definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type { HackerDefinitions } from './hacker';
export type { InternetDefinitions } from './internet';
export type { LocationDefinitions } from './location';
export type { LoremDefinitions } from './lorem';
export type { MetadataDefinitions } from './metadata';
export type { MusicDefinitions } from './music';
export type { PersonDefinitions, PersonTitleDefinitions } from './person';
export type { PhoneNumberDefinitions } from './phone_number';
Expand Down
6 changes: 6 additions & 0 deletions src/definitions/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type MetadataDefinitions = {
/**
* The English name of the language (and the specific country, if defined).
*/
title: string;
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type {
LocationDefinitions as AddressDefinitions,
LocationDefinitions,
LoremDefinitions,
MetadataDefinitions,
MusicDefinitions,
/** @deprecated Use PersonDefinitions instead */
PersonDefinitions as NameDefinitions,
Expand Down
3 changes: 2 additions & 1 deletion src/locales/af_ZA/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import cell_phone from './cell_phone';
import company from './company';
import internet from './internet';
import location from './location';
import metadata from './metadata';
import person from './person';
import phone_number from './phone_number';

const af_ZA: LocaleDefinition = {
title: 'Afrikaans',
cell_phone,
company,
internet,
location,
metadata,
person,
phone_number,
};
Expand Down
12 changes: 12 additions & 0 deletions src/locales/af_ZA/metadata/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import type { MetadataDefinitions } from '../../..';
import title from './title';

const metadata: MetadataDefinitions = {
title,
};

export default metadata;
1 change: 1 addition & 0 deletions src/locales/af_ZA/metadata/title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'Afrikaans';
3 changes: 2 additions & 1 deletion src/locales/ar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import color from './color';
import commerce from './commerce';
import date from './date';
import location from './location';
import metadata from './metadata';
import person from './person';
import phone_number from './phone_number';
import team from './team';
import vehicle from './vehicle';

const ar: LocaleDefinition = {
title: 'Arabic',
cell_phone,
color,
commerce,
date,
location,
metadata,
person,
phone_number,
team,
Expand Down
12 changes: 12 additions & 0 deletions src/locales/ar/metadata/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import type { MetadataDefinitions } from '../../..';
import title from './title';

const metadata: MetadataDefinitions = {
title,
};

export default metadata;
1 change: 1 addition & 0 deletions src/locales/ar/metadata/title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'Arabic';
3 changes: 2 additions & 1 deletion src/locales/az/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import company from './company';
import date from './date';
import internet from './internet';
import location from './location';
import metadata from './metadata';
import person from './person';
import phone_number from './phone_number';

const az: LocaleDefinition = {
title: 'Azerbaijani',
color,
commerce,
company,
date,
internet,
location,
metadata,
person,
phone_number,
};
Expand Down
12 changes: 12 additions & 0 deletions src/locales/az/metadata/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import type { MetadataDefinitions } from '../../..';
import title from './title';

const metadata: MetadataDefinitions = {
title,
};

export default metadata;
1 change: 1 addition & 0 deletions src/locales/az/metadata/title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'Azerbaijani';
3 changes: 2 additions & 1 deletion src/locales/cz/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import date from './date';
import internet from './internet';
import location from './location';
import lorem from './lorem';
import metadata from './metadata';
import person from './person';
import phone_number from './phone_number';

const cz: LocaleDefinition = {
title: 'Czech',
company,
date,
internet,
location,
lorem,
metadata,
person,
phone_number,
};
Expand Down
12 changes: 12 additions & 0 deletions src/locales/cz/metadata/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import type { MetadataDefinitions } from '../../..';
import title from './title';

const metadata: MetadataDefinitions = {
title,
};

export default metadata;
1 change: 1 addition & 0 deletions src/locales/cz/metadata/title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'Czech';
3 changes: 2 additions & 1 deletion src/locales/de/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import date from './date';
import internet from './internet';
import location from './location';
import lorem from './lorem';
import metadata from './metadata';
import person from './person';
import phone_number from './phone_number';
import word from './word';

const de: LocaleDefinition = {
title: 'German',
cell_phone,
company,
date,
internet,
location,
lorem,
metadata,
person,
phone_number,
word,
Expand Down
12 changes: 12 additions & 0 deletions src/locales/de/metadata/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import type { MetadataDefinitions } from '../../..';
import title from './title';

const metadata: MetadataDefinitions = {
title,
};

export default metadata;
1 change: 1 addition & 0 deletions src/locales/de/metadata/title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'German';
3 changes: 2 additions & 1 deletion src/locales/de_AT/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import cell_phone from './cell_phone';
import company from './company';
import internet from './internet';
import location from './location';
import metadata from './metadata';
import person from './person';
import phone_number from './phone_number';
import word from './word';

const de_AT: LocaleDefinition = {
title: 'German (Austria)',
cell_phone,
company,
internet,
location,
metadata,
person,
phone_number,
word,
Expand Down
12 changes: 12 additions & 0 deletions src/locales/de_AT/metadata/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import type { MetadataDefinitions } from '../../..';
import title from './title';

const metadata: MetadataDefinitions = {
title,
};

export default metadata;
1 change: 1 addition & 0 deletions src/locales/de_AT/metadata/title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'German (Austria)';
Loading