Skip to content

Commit 51d7dac

Browse files
committed
feat: add script to prune unused keys from dictionary and update package.json
1 parent 626e142 commit 51d7dac

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

packages/tooling/prune-dictionary.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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);

web/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"generate:robots-txt": "npx tsx src/_build/gen-robots-txt.ts",
7777
"generate:sentry-release": "tsx ../packages/tooling/sentry-release.ts web bundle",
7878
"generate:sitemap": "npx tsx src/_build/sitemap.ts",
79+
"prune:dictionary": "npx tsx ../packages/tooling/prune-dictionary.ts",
7980
"lh:collect": "npx --yes @lhci/cli collect",
8081
"lh:upload": "npx --yes @lhci/cli upload",
8182
"lint": "npm run build && npm run lint:alone",

0 commit comments

Comments
 (0)