index.mjs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* eslint-disable no-console, prefer-arrow-callback */
  2. import path from 'node:path'
  3. import { readFile, writeFile } from 'node:fs/promises'
  4. import { fileURLToPath } from 'node:url'
  5. import { getLocales, sortObjectAlphabetically } from './helpers.mjs'
  6. const root = fileURLToPath(new URL('../../', import.meta.url))
  7. const localesPath = path.join(root, 'packages', '@uppy', 'locales')
  8. const templatePath = path.join(localesPath, 'template.ts')
  9. const englishLocalePath = path.join(localesPath, 'src', 'en_US.ts')
  10. async function getLocalesAndCombinedLocale() {
  11. const locales = await getLocales(`${root}/packages/@uppy/**/lib/locale.js`)
  12. const combinedLocale = {}
  13. for (const [pluginName, locale] of Object.entries(locales)) {
  14. for (const [key, value] of Object.entries(locale.strings)) {
  15. if (key in combinedLocale && value !== combinedLocale[key]) {
  16. throw new Error(
  17. `'${key}' from ${pluginName} already exists in locale pack.`,
  18. )
  19. }
  20. combinedLocale[key] = value
  21. }
  22. }
  23. return sortObjectAlphabetically(combinedLocale)
  24. }
  25. const [combinedLocale, fileString] = await Promise.all([
  26. getLocalesAndCombinedLocale(),
  27. readFile(templatePath, 'utf-8'),
  28. ])
  29. const formattedLocale = JSON.stringify(combinedLocale, null, ' ')
  30. await Promise.all([
  31. // Populate template
  32. writeFile(
  33. englishLocalePath,
  34. fileString.replace(
  35. 'en_US.strings = {}',
  36. `en_US.strings = ${formattedLocale}`,
  37. ),
  38. ).then(() => console.log(`✅ Generated '${englishLocalePath}'`)),
  39. ])