get-prod-locales.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env node
  2. const cp = require('child_process');
  3. const { promisify } = require('util');
  4. const fs = require('fs');
  5. const pkg = require('../package.json');
  6. const availableLanguages = pkg.availableLanguages.sort();
  7. const exec = promisify(cp.exec);
  8. const arrayDiff = (current, package) =>
  9. current.filter(locale => !package.includes(locale));
  10. const cmd = 'compare-locales l10n.toml . `ls public/locales` --data=json';
  11. exec(cmd)
  12. .then(({ stdout }) => JSON.parse(stdout))
  13. .then(({ summary }) => {
  14. const locales = Object.keys(summary)
  15. .filter(locale => {
  16. const loc = summary[locale];
  17. const hasMissing = Object.prototype.hasOwnProperty.call(loc, 'missing');
  18. const hasErrors = Object.prototype.hasOwnProperty.call(loc, 'errors');
  19. return !hasMissing && !hasErrors;
  20. })
  21. .sort();
  22. if (locales.join(',') !== availableLanguages.join(',')) {
  23. const missingLanguages = arrayDiff(locales, availableLanguages);
  24. console.log('current 100%:', JSON.stringify(locales));
  25. console.log('package.json:', JSON.stringify(availableLanguages));
  26. console.log('missing prod:', JSON.stringify(missingLanguages));
  27. if (process.argv.includes('--write')) {
  28. const pkgPath = require.resolve('../package.json');
  29. pkg.availableLanguages = locales;
  30. const str = JSON.stringify(pkg, null, 2) + '\n';
  31. console.log('Updating /package.json availableLanguages');
  32. fs.writeFileSync(pkgPath, str, 'utf-8');
  33. }
  34. } else {
  35. console.log('Production locales are up to date!');
  36. }
  37. })
  38. .catch(err => {
  39. console.error(err);
  40. process.exit(1);
  41. });