|
| 1 | +// check for unused keys in dictionary.ts and prefix them with "skip-" |
| 2 | +// to manually remove them later after double checking |
| 3 | + |
| 4 | +import { readFileSync, writeFileSync, readdirSync, statSync } from "fs"; |
| 5 | + |
| 6 | +const dictionaryPath = "./src/components/locale/dictionary.ts"; |
| 7 | +const dictionary = readFileSync(dictionaryPath, "utf-8"); |
| 8 | + |
| 9 | +const keysRegex = /\s\s"(.*)":/g; |
| 10 | +const keys = Array.from(dictionary.matchAll(keysRegex)).map((match) => match[1]); |
| 11 | +const filteredKeys = keys.filter((key) => !key.startsWith("skip-")); |
| 12 | +const keysRecord = filteredKeys.reduce<Record<string, unknown>>((acc, key) => { |
| 13 | + acc[key] = null; |
| 14 | + return acc; |
| 15 | +}, {}); |
| 16 | + |
| 17 | +const srcPath = "./src"; |
| 18 | +const exts = [".ts", ".tsx", ".js", ".jsx"]; |
| 19 | +const ignoreExts = ["dictionary.ts"]; |
| 20 | + |
| 21 | +const walk = (dir: string) => { |
| 22 | + const files = readdirSync(dir); |
| 23 | + for (const file of files) { |
| 24 | + const path = `${dir}/${file}`; |
| 25 | + if (statSync(path).isDirectory()) { |
| 26 | + walk(path); |
| 27 | + } else { |
| 28 | + if (!exts.some((ext) => path.endsWith(ext))) continue; |
| 29 | + if (ignoreExts.some((ext) => path.endsWith(ext))) continue; |
| 30 | + const content = readFileSync(path, "utf-8"); |
| 31 | + const foundKeys = []; |
| 32 | + for (const key in keysRecord) { |
| 33 | + if (content.includes(key)) { |
| 34 | + console.log(`Found key: ${key} in ${path}`); |
| 35 | + |
| 36 | + foundKeys.push(key); |
| 37 | + } |
| 38 | + } |
| 39 | + foundKeys.forEach((key) => { |
| 40 | + delete keysRecord[key]; |
| 41 | + }); |
| 42 | + } |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +walk(srcPath); |
| 47 | + |
| 48 | +const unusedKeys = Object.keys(keysRecord); |
| 49 | +console.log(`Found ${unusedKeys.length} unused keys`); |
| 50 | + |
| 51 | +// prefix unused keys with "skip-" in dictionary variable |
| 52 | +const newDictionary = dictionary.replace(keysRegex, (match, key) => { |
| 53 | + if (match.includes("skip-")) return match; |
| 54 | + |
| 55 | + if (unusedKeys.includes(key)) { |
| 56 | + return ` "skip-${key}":`; |
| 57 | + } |
| 58 | + return match; |
| 59 | +}); |
| 60 | + |
| 61 | +writeFileSync(dictionaryPath, newDictionary); |
0 commit comments