Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions scripts/locale/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
124 changes: 124 additions & 0 deletions scripts/locale/redundant_rm.js
Original file line number Diff line number Diff line change
@@ -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 <source> <target>");
process.exit(1);
}

const [source, target] = args;
rmPlaceholder(source, target);
Loading