sort.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. await sortFile(
  9. Path.join(__dirname, '../../frontend/extracted-translations.json')
  10. )
  11. for (const locale of await fs.promises.readdir(LOCALES)) {
  12. if (locale === 'README.md') continue
  13. const path = Path.join(LOCALES, locale)
  14. await sortFile(path)
  15. }
  16. }
  17. async function sortFile(path) {
  18. const name = Path.basename(path)
  19. const input = await fs.promises.readFile(path, 'utf-8')
  20. const parsed = JSON.parse(input)
  21. const sorted = JSON.stringify(parsed, Object.keys(parsed).sort(), 2) + '\n'
  22. if (input === sorted) {
  23. return
  24. }
  25. if (CHECK) {
  26. console.warn('---')
  27. console.warn(
  28. name,
  29. 'is not sorted. Try running:\n\n',
  30. ' web$ make sort_locales',
  31. '\n'
  32. )
  33. console.warn('---')
  34. throw new Error(name + ' is not sorted')
  35. }
  36. console.log('Storing sorted version of', name)
  37. await fs.promises.writeFile(path, sorted)
  38. }
  39. try {
  40. await main()
  41. } catch (error) {
  42. console.error(error)
  43. process.exit(1)
  44. }