feat: lokalise clean

This commit is contained in:
Michael Zetterberg
2025-06-10 12:44:44 +02:00
parent 4f75c4a60f
commit 2c9f3c4c5e
3 changed files with 109 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
import { stdin as input, stdout as output } from "node:process"
import * as readline from "node:readline/promises"
import { config } from "dotenv"
const rl = readline.createInterface({ input, output })
config({ path: `${process.cwd()}/.env.local` })
function diffArray(json1, json2) {
const diff = []
const keys1 = Object.keys(json1)
const keys2 = Object.keys(json2)
keys1.forEach((key) => {
if (!keys2.includes(key)) {
diff.push(key)
}
})
return diff
}
async function main() {
const answer = await rl.question(
"To make sure we use the latest data for the diff, have you run i18n:download AND i18n:extract BEFORE running this? Type yes or no "
)
if (answer !== "yes") {
console.log("")
console.warn(
"Please run i18n:download AND i18n:extract BEFORE running this."
)
rl.close()
process.exit(1)
}
rl.close()
const allLokalise = await import("./translations-all/en.json", {
with: {
type: "json",
},
})
const fromCodebase = await import("./extracted.json", {
with: {
type: "json",
},
})
const labelsToRemove = diffArray(allLokalise, fromCodebase)
const { deleteBulk } = await import("./lokalise")
await deleteBulk(labelsToRemove)
}
main()

View File

@@ -215,3 +215,54 @@ export async function download(extractPath: string, all: boolean = false) {
await performanceMetrics
}
export async function deleteBulk(keyNames: string[]) {
perf.observe({ type: "measure" })
try {
performance.mark("bulkDeleteStart")
let keysToDelete: number[] = []
let cursor: string | undefined = undefined
let hasNext = true
do {
const keys = await lokaliseApi.keys().list({
project_id: projectId,
limit: 100,
pagination: "cursor",
cursor,
})
cursor = keys.nextCursor ?? undefined
keys.items.forEach((key) => {
if (key.key_id && key.key_name.web) {
if (keyNames.includes(key.key_name.web)) {
keysToDelete.push(key.key_id)
}
}
})
if (!keys.hasNextCursor()) {
hasNext = false
}
} while (hasNext)
const response = await lokaliseApi
.keys()
.bulk_delete(keysToDelete, { project_id: projectId })
log(`Bulk delete successful, removed ${keysToDelete.length} keys`)
return response
} catch (e) {
error("Bulk delete failed", e)
} finally {
performance.mark("bulkDeleteEnd")
performance.measure(
"Bulk delete operation",
"bulkDeleteStart",
"bulkDeleteEnd"
)
}
}

View File

@@ -27,6 +27,7 @@
"i18n:pull": "yarn i18n:download && yarn i18n:compile",
"i18n:sync": "yarn i18n:push && yarn i18n:pull",
"i18n:diff": "node i18n/tooling/diff.mjs",
"i18n:clean": "jiti i18n/tooling/clean.ts",
"check-types": "tsc --noEmit"
},
"dependencies": {