Skip to content

feat(types): generate all locales index files with non-any types #494

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

Merged
merged 4 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
109 changes: 66 additions & 43 deletions scripts/generateLocales.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { lstatSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { resolve } from 'node:path';
import type { Options } from 'prettier';
import { format } from 'prettier';
Expand Down Expand Up @@ -44,20 +44,16 @@ function removeTsSuffix(files: string[]): string[] {
}

function escapeImport(module: string): string {
if (module === 'name') {
return 'name_';
} else if (module === 'type') {
return 'type_';
if (module === 'name' || module === 'type' || module === 'switch') {
return `${module}_`;
} else {
return module;
}
}

function escapeField(module: string): string {
if (module === 'name') {
return 'name: name_';
} else if (module === 'type') {
return 'type: type_';
if (module === 'name' || module === 'type' || module === 'switch') {
return `${module}: ${module}_`;
} else {
return module;
}
Expand All @@ -70,7 +66,7 @@ function containsAll(checked?: string[], expected?: string[]): boolean {
return expected.every((c) => checked.includes(c));
}

function generateLocaleFile(locale: string) {
function generateLocaleFile(locale: string): void {
let content = `
${autoGeneratedCommentHeader}

Expand Down Expand Up @@ -129,7 +125,7 @@ function generateLocalesIndexFile(
depth: number,
extra: string = '',
expected?: string[]
) {
): void {
let modules = readdirSync(path);
modules = removeIndexTs(modules);
modules = removeTsSuffix(modules);
Expand All @@ -141,14 +137,19 @@ function generateLocalesIndexFile(
let asType = '';
if (!containsAll(expected, modules)) {
asType = ` as ${type}`;
} else {
} else if (type !== 'any') {
fieldType = `: ${type}`;
}
let content = `${autoGeneratedCommentHeader}
import type { ${importType} } from '..${'/..'.repeat(depth)}';
${modules
.map((module) => `import ${escapeImport(module)} from './${module}';`)
.join('\n')}
let content = `${autoGeneratedCommentHeader}\n`;
if (type !== 'any') {
content += ` import type { ${importType.replace(
/\[.*/,
''
)} } from '..${'/..'.repeat(depth)}';\n`;
}
content += ` ${modules
.map((module) => `import ${escapeImport(module)} from './${module}';`)
.join('\n')}

const ${name}${fieldType} = {
${extra}
Expand All @@ -161,6 +162,51 @@ function generateLocalesIndexFile(
writeFileSync(resolve(path, 'index.ts'), content);
}

function generateRecursiveModuleIndexes(
path: string,
name: string,
definition: string,
depth: number,
extra?: string,
moduleFiles?: string[]
): void {
generateLocalesIndexFile(path, name, definition, depth, extra, moduleFiles);

let submodules = readdirSync(path);
submodules = removeIndexTs(submodules);
for (const submodule of submodules) {
const pathModule = resolve(path, submodule);
// Only process sub folders recursively
if (lstatSync(pathModule).isDirectory()) {
let moduleDefinition =
definition === 'any' ? 'any' : `${definition}['${submodule}']`;
let moduleFiles: string[] = undefined;

// Overwrite types of src/locales/<locale>/<module>/index.ts for known DEFINITIONS
if (depth === 1) {
moduleFiles = DEFINITIONS[submodule];
if (typeof moduleFiles === 'undefined') {
moduleDefinition = 'any';
} else {
moduleDefinition = `${submodule.replace(/(^|_)([a-z])/g, (s) =>
s.replace('_', '').toUpperCase()
)}Definitions`;
}
}

// Recursive
generateRecursiveModuleIndexes(
pathModule,
submodule,
moduleDefinition,
depth + 1,
undefined,
moduleFiles
);
}
}
}

// Start of actual logic

const locales = readdirSync(pathLocales);
Expand Down Expand Up @@ -188,38 +234,15 @@ for (const locale of locales) {
// src/locale/<locale>.ts
generateLocaleFile(locale);

// src/locales/<locale>/index.ts
generateLocalesIndexFile(
// src/locales/**/index.ts
generateRecursiveModuleIndexes(
pathModules,
locale,
'LocaleDefinition',
1,
`title: '${localeTitle}',` +
(localeSeparator ? `\nseparator: '${localeSeparator}',` : ''),
undefined
(localeSeparator ? `\nseparator: '${localeSeparator}',` : '')
);

let modules = readdirSync(pathModules);
modules = removeIndexTs(modules);
modules = removeTsSuffix(modules);
for (const module of modules) {
// src/locales/<locale>/<module>/index.ts
const pathModule = resolve(pathModules, module);
const moduleFiles: string[] = DEFINITIONS[module];
if (typeof moduleFiles === 'undefined') {
continue;
}
generateLocalesIndexFile(
pathModule,
module,
`${module.replace(/(^|_)([a-z])/g, (s) =>
s.replace('_', '').toUpperCase()
)}Definitions`,
2,
'',
moduleFiles
);
}
}

// src/locales/index.ts
Expand Down
6 changes: 5 additions & 1 deletion src/locales/af_ZA/cell_phone/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import formats from './formats';

const cell_phone: any = {
const cell_phone = {
formats,
};

Expand Down
6 changes: 5 additions & 1 deletion src/locales/ar/cell_phone/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import formats from './formats';

const cell_phone: any = {
const cell_phone = {
formats,
};

Expand Down
10 changes: 7 additions & 3 deletions src/locales/ar/team/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import creature from './creature';
import name from './name';
import name_ from './name';

const team: any = {
const team = {
creature,
name,
name: name_,
};

export default team;
6 changes: 5 additions & 1 deletion src/locales/de/cell_phone/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import formats from './formats';

const cell_phone: any = {
const cell_phone = {
formats,
};

Expand Down
6 changes: 5 additions & 1 deletion src/locales/de_AT/cell_phone/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import formats from './formats';

const cell_phone: any = {
const cell_phone = {
formats,
};

Expand Down
10 changes: 7 additions & 3 deletions src/locales/el/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import author from './author';
import name from './name';
import name_ from './name';
import version from './version';

const app: any = {
const app = {
author,
name,
name: name_,
version,
};

Expand Down
6 changes: 5 additions & 1 deletion src/locales/el/business/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import credit_card_expiry_dates from './credit_card_expiry_dates';
import credit_card_numbers from './credit_card_numbers';
import credit_card_types from './credit_card_types';

const business: any = {
const business = {
credit_card_expiry_dates,
credit_card_numbers,
credit_card_types,
Expand Down
6 changes: 5 additions & 1 deletion src/locales/el/cell_phone/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import formats from './formats';

const cell_phone: any = {
const cell_phone = {
formats,
};

Expand Down
6 changes: 5 additions & 1 deletion src/locales/el/finance/credit_card/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { FinanceDefinitions } from '../../../../definitions';
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import type { FinanceDefinitions } from '../../../..';
import american_express from './american_express';
import discover from './discover';
import maestro from './maestro';
Expand Down
10 changes: 7 additions & 3 deletions src/locales/el/team/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import creature from './creature';
import name from './name';
import name_ from './name';

const team: any = {
const team = {
creature,
name,
name: name_,
};

export default team;
10 changes: 7 additions & 3 deletions src/locales/en/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import author from './author';
import name from './name';
import name_ from './name';
import version from './version';

const app: any = {
const app = {
author,
name,
name: name_,
version,
};

Expand Down
6 changes: 5 additions & 1 deletion src/locales/en/business/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import credit_card_expiry_dates from './credit_card_expiry_dates';
import credit_card_numbers from './credit_card_numbers';
import credit_card_types from './credit_card_types';

const business: any = {
const business = {
credit_card_expiry_dates,
credit_card_numbers,
credit_card_types,
Expand Down
6 changes: 5 additions & 1 deletion src/locales/en/cell_phone/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import formats from './formats';

const cell_phone: any = {
const cell_phone = {
formats,
};

Expand Down
6 changes: 5 additions & 1 deletion src/locales/en/finance/credit_card/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { FinanceDefinitions } from '../../../../definitions';
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import type { FinanceDefinitions } from '../../../..';
import american_express from './american_express';
import diners_club from './diners_club';
import discover from './discover';
Expand Down
10 changes: 7 additions & 3 deletions src/locales/en/team/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import creature from './creature';
import name from './name';
import name_ from './name';

const team: any = {
const team = {
creature,
name,
name: name_,
};

export default team;
6 changes: 5 additions & 1 deletion src/locales/en_GB/cell_phone/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import formats from './formats';

const cell_phone: any = {
const cell_phone = {
formats,
};

Expand Down
6 changes: 5 additions & 1 deletion src/locales/en_IE/cell_phone/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/
import formats from './formats';

const cell_phone: any = {
const cell_phone = {
formats,
};

Expand Down
Loading