From 59357357bbd919c48800091d08932a900d53e3e3 Mon Sep 17 00:00:00 2001 From: alexsparkes Date: Sat, 16 Mar 2024 21:48:42 +0000 Subject: [PATCH] feat: try catch for updatetranslations --- scripts/updatetranslations.js | 101 ++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 46 deletions(-) diff --git a/scripts/updatetranslations.js b/scripts/updatetranslations.js index 8127bcea..e0317404 100644 --- a/scripts/updatetranslations.js +++ b/scripts/updatetranslations.js @@ -1,13 +1,7 @@ -// tl;dr this function merges the translation file with the english file in order to add untranslated strings const fs = require('fs'); +const path = require('path'); const merge = require('@eartharoid/deep-merge'); -/** - * It recursively compares the keys of two JSON objects and removes the keys from the first object that - * are not present in the second object - * @param json1 - The JSON object that you want to remove keys from. - * @param json2 - The JSON object that you want to compare against. - */ const compareAndRemoveKeys = (json1, json2) => { for (let key in json1) { if (json2.hasOwnProperty(key)) { @@ -20,48 +14,63 @@ const compareAndRemoveKeys = (json1, json2) => { } }; -fs.readdirSync('../src/i18n/locales').forEach((file) => { - if (file === 'en_GB.json') { - return; +try { + const localesDir = path.join(__dirname, '../src/i18n/locales'); + const achievementsDir = path.join(localesDir, 'achievements'); + + // Check if the locales directory exists, if not, create it + if (!fs.existsSync(localesDir)) { + fs.mkdirSync(localesDir, { recursive: true }); } - const en = require('../src/i18n/locales/en_GB.json'); - const newdata = merge(en, require('../src/i18n/locales/' + file)); - - // remove strings not in english file - compareAndRemoveKeys(newdata, en); - - // write new file - fs.writeFileSync('../src/i18n/locales/' + file, JSON.stringify(newdata, null, 2)); - - // add new line - fs.appendFileSync('../src/i18n/locales/' + file, '\n'); -}); - -// do the same with achievements -fs.readdirSync('../src/i18n/locales/achievements').forEach((file) => { - if (file === 'en_GB.json') { - return; + // Check if the achievements directory exists, if not, create it + if (!fs.existsSync(achievementsDir)) { + fs.mkdirSync(achievementsDir, { recursive: true }); } - const en = require('../src/i18n/locales/achievements/en_GB.json'); - const newdata = merge(en, require('../src/i18n/locales/achievements/' + file)); - - // remove strings not in english file - compareAndRemoveKeys(newdata, en); - - // write new file - fs.writeFileSync('../src/i18n/locales/achievements/' + file, JSON.stringify(newdata, null, 2)); - - // add new line - fs.appendFileSync('../src/i18n/locales/achievements/' + file, '\n'); - - // if missing translations from locales/ add them to achievements/ - const locales = fs.readdirSync('../src/i18n/locales'); - locales.forEach((locale) => { - if (!fs.existsSync('../src/i18n/locales/achievements/' + locale)) { - fs.writeFileSync('../src/i18n/locales/achievements/' + locale, JSON.stringify(en, null, 2)); - fs.appendFileSync('../src/i18n/locales/achievements/' + locale, '\n'); + fs.readdirSync(localesDir).forEach((file) => { + if (file === 'en_GB.json') { + return; } + + const en = require(path.join(localesDir, 'en_GB.json')); + const newdata = merge(en, require(path.join(localesDir, file))); + + compareAndRemoveKeys(newdata, en); + + fs.writeFileSync(path.join(localesDir, file), JSON.stringify(newdata, null, 2)); + + fs.appendFileSync(path.join(localesDir, file), '\n'); }); -}); + + fs.readdirSync(achievementsDir).forEach((file) => { + if (file === 'en_GB.json') { + return; + } + + const enGBFilePath = path.join(achievementsDir, 'en_GB.json'); + if (!fs.existsSync(enGBFilePath)) { + console.error(`File 'en_GB.json' does not exist in the directory '${achievementsDir}'`); + return; + } + + const en = require(enGBFilePath); + const newdata = merge(en, require(path.join(achievementsDir, file))); + + compareAndRemoveKeys(newdata, en); + + fs.writeFileSync(path.join(achievementsDir, file), JSON.stringify(newdata, null, 2)); + + fs.appendFileSync(path.join(achievementsDir, file), '\n'); + + const locales = fs.readdirSync(localesDir); + locales.forEach((locale) => { + if (!fs.existsSync(path.join(achievementsDir, locale))) { + fs.writeFileSync(path.join(achievementsDir, locale), JSON.stringify(en, null, 2)); + fs.appendFileSync(path.join(achievementsDir, locale), '\n'); + } + }); + }); +} catch (error) { + console.error(`An error occurred: ${error.message}`); +}