crowdin-generate.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import fs from 'fs'
  2. import path from 'path'
  3. import chalk from 'chalk'
  4. import consola from 'consola'
  5. import { docRoot, errorAndExit } from '@element-plus/build-utils'
  6. // NB: this file is only for generating files that enables developers to develop the website.
  7. const componentLocaleRoot = path.resolve(docRoot, '.vitepress/crowdin')
  8. const exists = 'File already exists'
  9. async function main() {
  10. const localeOutput = path.resolve(docRoot, '.vitepress/i18n')
  11. if (fs.existsSync(localeOutput)) {
  12. throw new Error(exists)
  13. }
  14. consola.trace(chalk.cyan('Starting for build doc for developing'))
  15. // all language should be identical since it is mirrored from crowdin.
  16. const dirs = await fs.promises.readdir(componentLocaleRoot, {
  17. withFileTypes: true,
  18. })
  19. const languages = dirs.map((dir) => dir.name)
  20. const langWithoutEn = languages.filter((l) => l !== 'en-US')
  21. await fs.promises.mkdir(localeOutput)
  22. // build lang.json for telling `header>language-select` how many languages are there
  23. await fs.promises.writeFile(
  24. path.resolve(localeOutput, 'lang.json'),
  25. JSON.stringify(languages),
  26. 'utf-8'
  27. )
  28. // loop through en-US
  29. const enUS = path.resolve(componentLocaleRoot, 'en-US')
  30. // we do not include en-US since we are currently using it as template
  31. const languagePaths = langWithoutEn.map((l) => {
  32. return {
  33. name: l,
  34. pathname: path.resolve(componentLocaleRoot, l),
  35. }
  36. })
  37. consola.debug(languagePaths)
  38. await traverseDir(enUS, languagePaths, localeOutput)
  39. }
  40. async function traverseDir(
  41. dir: string,
  42. paths: { name: string; pathname: string }[],
  43. targetPath: string
  44. ) {
  45. const contents = await fs.promises.readdir(dir, { withFileTypes: true })
  46. await Promise.all(
  47. contents.map(async (c) => {
  48. if (c.isDirectory()) {
  49. await fs.promises.mkdir(path.resolve(targetPath, c.name), {
  50. recursive: true,
  51. })
  52. return traverseDir(
  53. path.resolve(dir, c.name),
  54. paths.map((p) => {
  55. return {
  56. ...p,
  57. pathname: path.resolve(p.pathname, c.name),
  58. }
  59. }),
  60. path.resolve(targetPath, c.name)
  61. )
  62. } else if (c.isFile()) {
  63. // eslint-disable-next-line @typescript-eslint/no-var-requires
  64. const content = require(path.resolve(dir, c.name))
  65. const contentToWrite = {
  66. 'en-US': content,
  67. }
  68. await Promise.all(
  69. paths.map(async (p) => {
  70. // eslint-disable-next-line @typescript-eslint/no-var-requires
  71. const content = require(path.resolve(p.pathname, c.name))
  72. contentToWrite[p.name] = content
  73. })
  74. )
  75. return fs.promises.writeFile(
  76. path.resolve(targetPath, c.name),
  77. JSON.stringify(contentToWrite, null, 2),
  78. {
  79. encoding: 'utf-8',
  80. }
  81. )
  82. }
  83. })
  84. )
  85. }
  86. main()
  87. .then(() => {
  88. consola.success(chalk.green('Locale for website development generated'))
  89. })
  90. .catch((err) => {
  91. if (err.message === exists) {
  92. // do nothing
  93. } else {
  94. errorAndExit(err)
  95. }
  96. })