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 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
75 changes: 23 additions & 52 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 { LocaleDefinition, MetadataDefinitions } from '../src/definitions';

// Constants

Expand All @@ -45,7 +45,7 @@ type PascalCase<S extends string> = S extends `${infer P1}_${infer P2}`
: Capitalize<S>;

type DefinitionsType = {
[key in keyof Definitions]: PascalCase<`${key}Definitions`>;
[key in keyof LocaleDefinition]-?: PascalCase<`${key}Definitions`>;
};

/**
Expand All @@ -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 @@ -153,43 +154,11 @@ function generateLocaleFile(locale: string): void {
writeFileSync(resolve(pathLocale, `${locale}.ts`), content);
}

function tryLoadLocalesMainIndexFile(
pathModules: string
): LocaleDefinition | undefined {
let localeDef: LocaleDefinition | undefined;
// 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'
);
const title = localeIndex.match(/title: '(.*)',/)?.[1];
if (title) {
localeDef = { title };
}
} 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 @@ -214,7 +183,6 @@ function generateLocalesIndexFile(
);

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

Expand All @@ -230,10 +198,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 @@ -255,8 +222,7 @@ function generateRecursiveModuleIndexes(
pathModule,
submodule,
moduleDefinition,
depth + 1,
undefined
depth + 1
);
}
}
Expand Down Expand Up @@ -311,10 +277,21 @@ 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 pathMetadata = resolve(pathModules, 'metadata.ts');
let localeTitle = 'No title found';
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const metadata: MetadataDefinitions = require(pathMetadata).default;
localeTitle = metadata.title;
if (!localeTitle) {
throw new Error(`No title property found on ${JSON.stringify(metadata)}`);
}
} catch (e) {
console.error(
`Failed to load ${pathMetadata}. Please make sure the file exists and exports MetadataDefinitions.`
);
console.error(e);
}

const localizedFaker = `faker${locale.replace(/^([a-z]+)/, (part) =>
part.toUpperCase()
Expand All @@ -330,13 +307,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
58 changes: 26 additions & 32 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 @@ -18,44 +19,37 @@ import type { SystemDefinitions } from './system';
import type { VehicleDefinitions } from './vehicle';
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>;

/**
* The definitions as used by the Faker modules.
* Wrapper type for all definition categories that will make all properties optional and allow extra properties.
*/
export interface Definitions {
airline: AirlineDefinitions;
animal: AnimalDefinitions;
color: ColorDefinitions;
commerce: CommerceDefinitions;
company: CompanyDefinitions;
database: DatabaseDefinitions;
date: DateDefinitions;
finance: FinanceDefinitions;
hacker: HackerDefinitions;
internet: InternetDefinitions;
location: LocationDefinitions;
lorem: LoremDefinitions;
music: MusicDefinitions;
person: PersonDefinitions;
phone_number: PhoneNumberDefinitions;
science: ScienceDefinitions;
system: SystemDefinitions;
vehicle: VehicleDefinitions;
word: WordDefinitions;
}
export type LocaleEntry<T extends Record<string, unknown>> = Partial<T> &
// Unsupported & custom entries
Record<string, unknown>;

/**
* The definitions as used by the translations/locales.
* This is basically the same as Definitions with the exception,
* 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;
airline?: AirlineDefinitions;
animal?: AnimalDefinitions;
color?: ColorDefinitions;
commerce?: CommerceDefinitions;
company?: CompanyDefinitions;
database?: DatabaseDefinitions;
date?: DateDefinitions;
finance?: FinanceDefinitions;
hacker?: HackerDefinitions;
internet?: InternetDefinitions;
location?: LocationDefinitions;
lorem?: LoremDefinitions;
music?: MusicDefinitions;
person?: PersonDefinitions;
phone_number?: PhoneNumberDefinitions;
science?: ScienceDefinitions;
system?: SystemDefinitions;
vehicle?: VehicleDefinitions;
word?: WordDefinitions;
} & Record<string, Record<string, unknown>>;
3 changes: 2 additions & 1 deletion src/definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ export type {
export type { CompanyDefinitions } from './company';
export type { DatabaseDefinitions } from './database';
export type { DateDefinitions, DateEntryDefinition } from './date';
export type { Definitions, LocaleDefinition } from './definitions';
export type { LocaleDefinition, LocaleEntry } from './definitions';
export type { FinanceDefinitions } from './finance';
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;
} & Record<string, unknown>;
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ export type {
HackerDefinitions,
InternetDefinitions,
LocaleDefinition,
LocaleEntry,
/** @deprecated Use LocationDefinitions instead */
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
7 changes: 7 additions & 0 deletions src/locales/af_ZA/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { MetadataDefinitions } from '../..';

const metadata: MetadataDefinitions = {
title: 'Afrikaans',
};

export default metadata;
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
7 changes: 7 additions & 0 deletions src/locales/ar/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { MetadataDefinitions } from '../..';

const metadata: MetadataDefinitions = {
title: 'Arabic',
};

export default metadata;
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
7 changes: 7 additions & 0 deletions src/locales/az/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { MetadataDefinitions } from '../..';

const metadata: MetadataDefinitions = {
title: 'Azerbaijani',
};

export default metadata;
3 changes: 2 additions & 1 deletion src/locales/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import database from './database';
import hacker from './hacker';
import internet from './internet';
import location from './location';
import metadata from './metadata';
import system from './system';

const base: LocaleDefinition = {
title: 'Base',
color,
database,
hacker,
internet,
location,
metadata,
system,
};

Expand Down
7 changes: 7 additions & 0 deletions src/locales/base/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { MetadataDefinitions } from '../..';

const metadata: MetadataDefinitions = {
title: 'Base',
};

export default metadata;
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
7 changes: 7 additions & 0 deletions src/locales/cz/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { MetadataDefinitions } from '../..';

const metadata: MetadataDefinitions = {
title: 'Czech',
};

export default metadata;
Loading