From 2c9f3c4c5e0c96232a5303b008e4db191208a1fe Mon Sep 17 00:00:00 2001 From: Michael Zetterberg Date: Tue, 10 Jun 2025 12:44:44 +0200 Subject: [PATCH] feat: lokalise clean --- apps/scandic-web/i18n/tooling/clean.mjs | 57 +++++++++++++++++++++++ apps/scandic-web/i18n/tooling/lokalise.ts | 51 ++++++++++++++++++++ apps/scandic-web/package.json | 1 + 3 files changed, 109 insertions(+) create mode 100644 apps/scandic-web/i18n/tooling/clean.mjs diff --git a/apps/scandic-web/i18n/tooling/clean.mjs b/apps/scandic-web/i18n/tooling/clean.mjs new file mode 100644 index 000000000..e63525425 --- /dev/null +++ b/apps/scandic-web/i18n/tooling/clean.mjs @@ -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() diff --git a/apps/scandic-web/i18n/tooling/lokalise.ts b/apps/scandic-web/i18n/tooling/lokalise.ts index 6fae35a15..454a193ef 100644 --- a/apps/scandic-web/i18n/tooling/lokalise.ts +++ b/apps/scandic-web/i18n/tooling/lokalise.ts @@ -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" + ) + } +} diff --git a/apps/scandic-web/package.json b/apps/scandic-web/package.json index 46c879909..9c3240617 100644 --- a/apps/scandic-web/package.json +++ b/apps/scandic-web/package.json @@ -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": {