lint-locales.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env node
  2. const cp = require('child_process');
  3. const { promisify } = require('util');
  4. const pkg = require('../package.json');
  5. const conf = require('../server/config');
  6. const exec = promisify(cp.exec);
  7. const cmd = `compare-locales l10n.toml . ${getLocales()} --data=json`;
  8. console.log(cmd);
  9. exec(cmd)
  10. .then(({ stdout }) => JSON.parse(stdout))
  11. .then(({ details }) => filterErrors(details))
  12. .then(results => {
  13. if (results.length) {
  14. results.forEach(({ locale, data }) => {
  15. console.log(locale);
  16. data.forEach(msg => console.log(`- ${msg}`));
  17. console.log('');
  18. });
  19. process.exit(2);
  20. }
  21. })
  22. .catch(err => {
  23. console.error(err);
  24. process.exit(1);
  25. });
  26. function filterErrors(details) {
  27. return Object.keys(details)
  28. .sort()
  29. .map(locale => {
  30. const data = details[locale]
  31. .filter(item => Object.prototype.hasOwnProperty.call(item, 'error'))
  32. .map(({ error }) => error);
  33. return { locale, data };
  34. })
  35. .filter(({ data }) => data.length);
  36. }
  37. function getLocales() {
  38. // If we're in a "production" env (or passed the `--production` flag), only
  39. // check the locales from the package.json file's `availableLanguages` array.
  40. if (conf.env === 'production' || process.argv.includes('--production')) {
  41. return pkg.availableLanguages.sort().join(' ');
  42. }
  43. // Lint all the locales.
  44. return '`ls public/locales`';
  45. }