diff --git a/scripts/locale/index.js b/scripts/locale/index.js index 45255d00d..a2bbc7424 100644 --- a/scripts/locale/index.js +++ b/scripts/locale/index.js @@ -4,7 +4,7 @@ const subCommand = process.argv[2]; if (!subCommand) { console.error( - "Please specify a subcommand for locale (diff, rm, gen_placeholder)" + "Please specify a subcommand for locale (diff, rm, gen_placeholder, redundant_rm)" ); process.exit(1); } @@ -15,7 +15,9 @@ try { } catch (error) { if (error.code === "MODULE_NOT_FOUND") { console.error(`Unknown subcommand: ${subCommand}`); - console.error("Available subcommands: diff, rm, gen_placeholder"); + console.error( + "Available subcommands: diff, rm, gen_placeholder, redundant_rm" + ); } else { console.error(error); } diff --git a/scripts/locale/redundant_rm.js b/scripts/locale/redundant_rm.js new file mode 100644 index 000000000..0c4e72236 --- /dev/null +++ b/scripts/locale/redundant_rm.js @@ -0,0 +1,124 @@ +const fs = require("fs"); +const path = require("path"); + +// Recursively remove keys from target that don't exist in source +function removeExtraKeys(source, target) { + // If target is not an object, return it as-is + if (typeof target !== "object" || target === null || Array.isArray(target)) { + return target; + } + + const result = {}; + + // Only keep keys that exist in source + for (const key in target) { + if (key in source) { + // If both are objects (not arrays), recursively process them + if ( + typeof source[key] === "object" && + source[key] !== null && + !Array.isArray(source[key]) && + typeof target[key] === "object" && + target[key] !== null && + !Array.isArray(target[key]) + ) { + result[key] = removeExtraKeys(source[key], target[key]); + } else { + // Keep the target value as-is + result[key] = target[key]; + } + } + // If key doesn't exist in source, it will be omitted (deleted) + } + + return result; +} + +function rmPlaceholder(source, target) { + // Define paths for source and target locale files + const sourceFile = path.join( + __dirname, + "..", + "..", + "src", + "locales", + `${source}.json` + ); + const targetFile = path.join( + __dirname, + "..", + "..", + "src", + "locales", + `${target}.json` + ); + + // Read the source file + fs.readFile(sourceFile, "utf8", (readErr, sourceData) => { + if (readErr) { + console.error(`Error reading file ${sourceFile}:`, readErr); + process.exit(1); + } + + let sourceLocaleData; + try { + sourceLocaleData = JSON.parse(sourceData); + } catch (parseErr) { + console.error(`Error parsing JSON from ${sourceFile}:`, parseErr); + process.exit(1); + } + + // Check if target file exists + fs.access(targetFile, fs.constants.F_OK, (err) => { + if (err) { + console.error(`Target file does not exist: ${targetFile}`); + process.exit(1); + } + + // Read the target file + fs.readFile(targetFile, "utf8", (readErr, targetData) => { + if (readErr) { + console.error(`Error reading file ${targetFile}:`, readErr); + process.exit(1); + } + + let targetLocaleData; + try { + targetLocaleData = JSON.parse(targetData); + } catch (parseErr) { + console.error(`Error parsing JSON from ${targetFile}:`, parseErr); + process.exit(1); + } + + // Remove keys from target that don't exist in source + const cleanedData = removeExtraKeys(sourceLocaleData, targetLocaleData); + + // Write the cleaned JSON to the target file + fs.writeFile( + targetFile, + JSON.stringify(cleanedData, null, 2), + "utf8", + (writeErr) => { + if (writeErr) { + console.error(`Error writing file ${targetFile}:`, writeErr); + process.exit(1); + } + console.log( + `Extra keys removed from ${target}.json (keys not in ${source}.json)` + ); + } + ); + }); + }); + }); +} + +// Parse command-line arguments (skip node and the script path) +const args = process.argv.slice(3); +if (args.length < 2) { + console.error("Usage: npm run locale rm_placeholder "); + process.exit(1); +} + +const [source, target] = args; +rmPlaceholder(source, target);