mapcomplete/scripts/removeTranslationString.ts

54 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

2022-12-16 13:44:25 +01:00
import ScriptUtils from "./ScriptUtils"
2023-08-10 15:36:32 +02:00
import { Utils } from "../src/Utils"
import * as fs from "fs"
2022-12-16 13:44:25 +01:00
async function main(args: string[]) {
let directory = "./langs"
{
const dirs = ["layers", "themes", "shared-questions"]
for (const dir of dirs) {
const layerIndex = args.findIndex((s) => s === "--" + dir)
if (layerIndex >= 0) {
directory = "./langs/" + dir
args.splice(layerIndex, 1)
}
}
}
2022-12-16 13:44:25 +01:00
if (args.length !== 1) {
console.log(
"Usage: first argument is the fully qualified key of the string to remove. Removes translations in the core translations, unless '--layers' or '--themes' is given"
)
return
}
// Path within the JSON which will be removed - not the path in the filesystem!
const path = args[0].split(".")
2023-10-02 00:23:20 +02:00
console.log("Removing translation string ", path, "from the translations for " + directory)
const files = ScriptUtils.readDirRecSync(directory, 1).filter((f) => f.endsWith(".json"))
const removedLanguages: string[] = []
for (const file of files) {
const rawContents = fs.readFileSync(file, { encoding: "utf-8" })
const json = JSON.parse(rawContents)
Utils.WalkPath(path, json, (found) => {
removedLanguages.push(file)
2024-06-16 16:06:26 +02:00
console.log("Removing ", found)
return undefined
})
const lastChar = rawContents.endsWith("\n") ? "\n" : ""
fs.writeFileSync(file, JSON.stringify(json, null, " ") + lastChar)
}
2024-06-16 16:06:26 +02:00
if (removedLanguages.length === 0) {
console.warn("No items removed. Doublecheck the paths")
} else {
console.log("Removed items in " + removedLanguages.join(", "))
}
}
const args = [...process.argv]
args.splice(0, 2)
main(args).then((_) => {
console.log("All done!")
})