overleaf/services/web/scripts/translations/sort.js
Liangjun Song 8293771f58 Merge pull request #21290 from overleaf/ls-scripts-to-esm-translations
Migrate scripts/translation to esm

GitOrigin-RevId: 475ec949f0ba238791df91de109169584e68c701
2024-10-25 08:06:12 +00:00

41 lines
1.1 KiB
JavaScript

import fs from 'fs'
import Path from 'path'
import { fileURLToPath } from 'url'
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const LOCALES = Path.join(__dirname, '../../locales')
const CHECK = process.argv.includes('--check')
async function main() {
for (const locale of await fs.promises.readdir(LOCALES)) {
if (locale === 'README.md') continue
const path = Path.join(LOCALES, locale)
const input = await fs.promises.readFile(path, 'utf-8')
const parsed = JSON.parse(input)
const sorted = JSON.stringify(parsed, Object.keys(parsed).sort(), 2) + '\n'
if (input === sorted) {
continue
}
if (CHECK) {
console.warn('---')
console.warn(
locale,
'is not sorted. Try running:\n\n',
' web$ make sort_locales',
'\n'
)
console.warn('---')
throw new Error(locale + ' is not sorted')
}
console.log('Storing sorted version of', locale)
await fs.promises.writeFile(path, sorted)
}
}
try {
await main()
} catch (error) {
console.error(error)
process.exit(1)
}