build-locales-files.mjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { existsSync, writeFileSync } from 'node:fs';
  2. import { Glob } from 'bun';
  3. import _ from 'lodash';
  4. async function getPathsFromGlobs({ patterns, onlyFiles = true }) {
  5. const filePaths = [];
  6. for (const pattern of patterns) {
  7. const glob = new Glob(pattern);
  8. for await (const filePath of glob.scan({ onlyFiles, cwd: '.' })) {
  9. filePaths.push(filePath);
  10. }
  11. }
  12. return { filePaths };
  13. }
  14. function getLocaleKey({ filePath }) {
  15. const fileName = filePath.split('/').pop();
  16. return fileName.replace(/\.yml$/, '');
  17. }
  18. async function createMissingLocaleFile({ localeKey }) {
  19. const fileName = `${localeKey}.yml`;
  20. const { filePaths: localesDirs } = await getPathsFromGlobs({
  21. patterns: [
  22. 'locales',
  23. 'src/tools/*/locales',
  24. ],
  25. onlyFiles: false,
  26. });
  27. for (const localesDir of localesDirs) {
  28. const filePath = `${localesDir}/${fileName}`;
  29. if (existsSync(filePath)) {
  30. console.log(`Locale file already exists: ${filePath}`);
  31. continue;
  32. }
  33. console.log(`Creating missing locale file: ${filePath}`);
  34. writeFileSync(filePath, '', 'utf8');
  35. }
  36. }
  37. const { filePaths } = await getPathsFromGlobs({
  38. patterns: [
  39. 'locales/*.yml',
  40. 'src/tools/*/locales/*.yml',
  41. ],
  42. });
  43. await Promise.all(
  44. _.chain(filePaths)
  45. .map(filePath => getLocaleKey({ filePath }))
  46. .uniq()
  47. .map(localeKey => createMissingLocaleFile({ localeKey }))
  48. .value(),
  49. );