sort.js 987 B

123456789101112131415161718192021222324252627282930313233343536
  1. const fs = require('fs')
  2. const Path = require('path')
  3. const LOCALES = Path.join(__dirname, '../../locales')
  4. const CHECK = process.argv.includes('--check')
  5. async function main() {
  6. for (const locale of await fs.promises.readdir(LOCALES)) {
  7. if (locale === 'README.md') continue
  8. const path = Path.join(LOCALES, locale)
  9. const input = await fs.promises.readFile(path, 'utf-8')
  10. const parsed = JSON.parse(input)
  11. const sorted = JSON.stringify(parsed, Object.keys(parsed).sort(), 2) + '\n'
  12. if (input === sorted) {
  13. continue
  14. }
  15. if (CHECK) {
  16. console.warn('---')
  17. console.warn(
  18. locale,
  19. 'is not sorted. Try running:\n\n',
  20. ' web$ make sort_locales',
  21. '\n'
  22. )
  23. console.warn('---')
  24. throw new Error(locale + ' is not sorted')
  25. }
  26. console.log('Storing sorted version of', locale)
  27. await fs.promises.writeFile(path, sorted)
  28. }
  29. }
  30. main().catch(error => {
  31. console.error(error)
  32. process.exit(1)
  33. })