| 6 | const languagesDir = path.join(import.meta.dirname, "../languages"); |
| 7 | |
| 8 | export async function sortLanguageFiles() { |
| 9 | try { |
| 10 | const languageFiles = await fs.readdir(languagesDir); |
| 11 | await Promise.all( |
| 12 | languageFiles.map(async (file) => { |
| 13 | if (!file.endsWith(".json")) return; |
| 14 | console.log(`Sorting ${file}...`); |
| 15 | const filePath = path.join(languagesDir, file); |
| 16 | const content = await fs.readFile(filePath, "utf8"); |
| 17 | const jsonContent = JSON.parse(content); |
| 18 | const keysCount = Object.keys(jsonContent).length; |
| 19 | const sortedContent = Object.keys(jsonContent) |
| 20 | .sort() |
| 21 | .reduce((obj, key) => { |
| 22 | obj[key] = jsonContent[key]; |
| 23 | return obj; |
| 24 | }, {}); |
| 25 | if (keysCount !== Object.keys(sortedContent).length) { |
| 26 | throw new Error(`Error: ${file} keys count is not equal`); |
| 27 | } |
| 28 | return await fs.writeFile(filePath, JSON.stringify(sortedContent, null, 2)); |
| 29 | }) |
| 30 | ); |
| 31 | console.log("All language files sorted successfully"); |
| 32 | } catch (err) { |
| 33 | console.error("Error sorting language files:", err); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | sortLanguageFiles(); |