sort.js 1.1 KB

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