-
-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto generate/update locale files (#252)
- Loading branch information
Showing
62 changed files
with
406 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { readdirSync, readFileSync, writeFileSync } from 'node:fs'; | ||
import { resolve } from 'node:path'; | ||
import type { Options } from 'prettier'; | ||
import { format } from 'prettier'; | ||
import options from '../.prettierrc.cjs'; | ||
import type { LocaleDefinition } from '../src'; | ||
|
||
const pathRoot = resolve(__dirname, '..'); | ||
const pathLocale = resolve(pathRoot, 'src', 'locale'); | ||
const pathLocales = resolve(pathRoot, 'src', 'locales'); | ||
const pathLocalesIndex = resolve(pathLocales, 'index.ts'); | ||
const pathDocsApiLocalization = resolve( | ||
pathRoot, | ||
'docs', | ||
'api', | ||
'localization.md' | ||
); | ||
|
||
const scriptCommand = 'pnpm run generate:locales'; | ||
const prettierTsOptions: Options = { ...options, parser: 'typescript' }; | ||
const prettierMdOptions: Options = { ...options, parser: 'markdown' }; | ||
|
||
const locales = readdirSync(pathLocales); | ||
locales.splice(locales.indexOf('index.ts'), 1); | ||
|
||
let localeIndexImports = "import type { LocaleDefinition } from '..';\n"; | ||
let localeIndexType = 'export type KnownLocale =\n'; | ||
let localeIndexLocales = 'const locales: KnownLocales = {\n'; | ||
|
||
let localizationLocales = '| Locale | Name |\n| :--- | :--- |\n'; | ||
|
||
const autoGeneratedCommentHeader = `/* | ||
* This file is automatically generated. | ||
* Run '${scriptCommand}' to update. | ||
*/`; | ||
|
||
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; | ||
|
||
localeIndexImports += `import ${locale} from './${locale}';\n`; | ||
localeIndexType += ` | '${locale}'\n`; | ||
localeIndexLocales += ` ${locale},\n`; | ||
localizationLocales += `| ${locale} | ${localeTitle} |\n`; | ||
|
||
// src/locale/<locale>.ts | ||
let content = ` | ||
${autoGeneratedCommentHeader} | ||
import { Faker } from '..'; | ||
import ${locale} from '../locales/${locale}'; | ||
${locale !== 'en' ? "import en from '../locales/en';" : ''} | ||
const faker = new Faker({ | ||
locale: '${locale}', | ||
localeFallback: 'en', | ||
locales: { | ||
${locale}, | ||
${locale !== 'en' ? 'en,' : ''} | ||
}, | ||
}); | ||
export = faker; | ||
`; | ||
|
||
content = format(content, prettierTsOptions); | ||
writeFileSync(resolve(pathLocale, locale + '.ts'), content); | ||
} | ||
|
||
// src/locales/index.ts | ||
|
||
let indexContent = ` | ||
${autoGeneratedCommentHeader} | ||
${localeIndexImports} | ||
${localeIndexType}; | ||
export type KnownLocales = Record<KnownLocale, LocaleDefinition>; | ||
${localeIndexLocales}}; | ||
export default locales; | ||
`; | ||
|
||
indexContent = format(indexContent, prettierTsOptions); | ||
|
||
writeFileSync(pathLocalesIndex, indexContent); | ||
|
||
// docs/api/localization.md | ||
|
||
localizationLocales = format(localizationLocales, prettierMdOptions); | ||
|
||
let localizationContent = readFileSync(pathDocsApiLocalization, 'utf-8'); | ||
localizationContent = localizationContent.replace( | ||
/(^<!-- LOCALES-AUTO-GENERATED-START -->$).*(^<!-- LOCALES-AUTO-GENERATED-END -->$)/gms, | ||
`$1\n\n<!-- Run '${scriptCommand}' to update. -->\n\n${localizationLocales}\n$2` | ||
); | ||
writeFileSync(pathDocsApiLocalization, localizationContent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.