diff options
| author | ST-DDT <[email protected]> | 2023-03-07 10:09:29 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-03-07 09:09:29 +0000 |
| commit | 9a35dc92260585683132172f10bcdec701ed661a (patch) | |
| tree | 35a5711ba5a5fbc18a8f62515d1ae8b25af77023 /scripts | |
| parent | a4d5203511e6ea7e26bc8c75baf91ee44387cac0 (diff) | |
| download | faker-9a35dc92260585683132172f10bcdec701ed661a.tar.xz faker-9a35dc92260585683132172f10bcdec701ed661a.zip | |
refactor!: remove dynamic locale switching support (#1735)
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/apidoc/typedoc.ts | 4 | ||||
| -rw-r--r-- | scripts/bundle.ts | 6 | ||||
| -rw-r--r-- | scripts/generateLocales.ts | 84 |
3 files changed, 66 insertions, 28 deletions
diff --git a/scripts/apidoc/typedoc.ts b/scripts/apidoc/typedoc.ts index 43b2ac88..b408b7ce 100644 --- a/scripts/apidoc/typedoc.ts +++ b/scripts/apidoc/typedoc.ts @@ -94,7 +94,9 @@ export function selectApiModules( export function selectApiMethods( module: DeclarationReflection ): DeclarationReflection[] { - return module.getChildrenByKind(ReflectionKind.Method); + return module + .getChildrenByKind(ReflectionKind.Method) + .filter((method) => !method.flags.isPrivate); } /** diff --git a/scripts/bundle.ts b/scripts/bundle.ts index e08b7de4..9f8038cb 100644 --- a/scripts/bundle.ts +++ b/scripts/bundle.ts @@ -1,7 +1,7 @@ import { buildSync } from 'esbuild'; import { sync as globSync } from 'glob'; import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; -import locales from '../src/locales'; +import { allLocales } from '../src'; console.log('Building dist for node (cjs)...'); @@ -14,7 +14,7 @@ if (existsSync(localeDir)) { } mkdirSync(localeDir); -for (const locale of Object.keys(locales)) { +for (const locale of Object.keys(allLocales)) { writeFileSync( `${localeDir}/${locale}.js`, `module.exports = require('../dist/cjs/locale/${locale}');\n`, @@ -43,7 +43,7 @@ console.log('Building dist for node type=module (esm)...'); buildSync({ entryPoints: [ './src/index.ts', - ...Object.keys(locales).map((locale) => `./src/locale/${locale}.ts`), + ...Object.keys(allLocales).map((locale) => `./src/locale/${locale}.ts`), ], outdir: './dist/esm', bundle: true, diff --git a/scripts/generateLocales.ts b/scripts/generateLocales.ts index 57181589..d8fa1be1 100644 --- a/scripts/generateLocales.ts +++ b/scripts/generateLocales.ts @@ -12,7 +12,13 @@ * * Run this script using `pnpm run generate:locales` */ -import { lstatSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'; +import { + existsSync, + lstatSync, + readdirSync, + readFileSync, + writeFileSync, +} from 'node:fs'; import { resolve } from 'node:path'; import type { Options } from 'prettier'; import { format } from 'prettier'; @@ -24,6 +30,7 @@ import type { Definitions, LocaleDefinition } from '../src/definitions'; const pathRoot = resolve(__dirname, '..'); const pathLocale = resolve(pathRoot, 'src', 'locale'); const pathLocales = resolve(pathRoot, 'src', 'locales'); +const pathLocaleIndex = resolve(pathLocale, 'index.ts'); const pathLocalesIndex = resolve(pathLocales, 'index.ts'); const pathDocsGuideLocalization = resolve( pathRoot, @@ -108,19 +115,31 @@ function escapeField(parent: string, module: string): string { } function generateLocaleFile(locale: string): void { + const parts = locale.split('_'); + const locales = [locale]; + + for (let i = parts.length - 1; i > 0; i--) { + const fallback = parts.slice(0, i).join('_'); + if (existsSync(resolve(pathLocales, fallback))) { + locales.push(fallback); + } + } + + if (locales[locales.length - 1] !== 'en') { + locales.push('en'); + } + let content = ` ${autoGeneratedCommentHeader} import { Faker } from '../faker'; - import ${locale} from '../locales/${locale}'; - ${locale !== 'en' ? "import en from '../locales/en';" : ''} + ${locales + .map((imp) => `import ${imp} from '../locales/${imp}';`) + .join('\n')} export const faker = new Faker({ - locale: '${locale}', - localeFallback: 'en', - locales: { - ${locale}, - ${locale !== 'en' ? 'en,' : ''} + locale: ${ + locales.length === 1 ? locales[0] : `[${locales.join(', ')}]` }, }); `; @@ -275,11 +294,12 @@ function updateLocaleFileHook( const locales = readdirSync(pathLocales); removeIndexTs(locales); -let localeIndexImports = "import type { LocaleDefinition } from '..';\n"; -let localeIndexType = 'export type KnownLocale =\n'; -let localeIndexLocales = 'const locales: KnownLocales = {\n'; +let localeIndexImports = ''; +let localeIndexExportsIndividual = ''; +let localeIndexExportsGrouped = ''; +let localesIndexExports = ''; -let localizationLocales = '| Locale | Name |\n| :--- | :--- |\n'; +let localizationLocales = '| Locale | Name | Faker |\n| :--- | :--- | :--- |\n'; for (const locale of locales) { const pathModules = resolve(pathLocales, locale); @@ -288,10 +308,15 @@ for (const locale of locales) { // We use a fallback here to at least generate a working file. const localeTitle = localeDef?.title ?? `TODO: Insert Title for ${locale}`; - localeIndexImports += `import ${locale} from './${locale}';\n`; - localeIndexType += ` | '${locale}'\n`; - localeIndexLocales += ` ${locale},\n`; - localizationLocales += `| ${locale} | ${localeTitle} |\n`; + const localizedFaker = `faker${locale.replace(/^([a-z]+)/, (part) => + part.toUpperCase() + )}`; + + localeIndexImports += `import { faker as ${localizedFaker} } from './${locale}';\n`; + localeIndexExportsIndividual += ` ${localizedFaker},\n`; + localeIndexExportsGrouped += ` ${locale}: ${localizedFaker},\n`; + localesIndexExports += `export { default as ${locale} } from './${locale}';\n`; + localizationLocales += `| \`${locale}\` | ${localeTitle} | \`${localizedFaker}\` |\n`; // src/locale/<locale>.ts generateLocaleFile(locale); @@ -306,24 +331,35 @@ for (const locale of locales) { ); } -// src/locales/index.ts +// src/locale/index.ts -let indexContent = ` +let localeIndexContent = ` ${autoGeneratedCommentHeader} ${localeIndexImports} - ${localeIndexType}; + export { + ${localeIndexExportsIndividual} + }; + + export const allFakers = { + ${localeIndexExportsGrouped} + } as const; + `; + +localeIndexContent = format(localeIndexContent, prettierTsOptions); +writeFileSync(pathLocaleIndex, localeIndexContent); - export type KnownLocales = Record<KnownLocale, LocaleDefinition>; +// src/locales/index.ts - ${localeIndexLocales}}; +let localesIndexContent = ` + ${autoGeneratedCommentHeader} - export default locales; + ${localesIndexExports} `; -indexContent = format(indexContent, prettierTsOptions); -writeFileSync(pathLocalesIndex, indexContent); +localesIndexContent = format(localesIndexContent, prettierTsOptions); +writeFileSync(pathLocalesIndex, localesIndexContent); // docs/guide/localization.md |
