-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan_missing_keys.js
More file actions
82 lines (69 loc) · 2.4 KB
/
Copy pathscan_missing_keys.js
File metadata and controls
82 lines (69 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const fs = require('fs');
const path = require('path');
const glob = require('glob'); // You might need to install this or use recursive readdir
// Config
const SRC_DIR = './src';
const LANGUAGE_FILE = './src/contexts/LanguageContext.tsx';
function getAllFiles(dirPath, arrayOfFiles) {
const files = fs.readdirSync(dirPath);
arrayOfFiles = arrayOfFiles || [];
files.forEach(function (file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
} else {
if (file.endsWith('.tsx') || file.endsWith('.ts')) {
arrayOfFiles.push(path.join(dirPath, "/", file));
}
}
});
return arrayOfFiles;
}
// 1. Extract defined keys from LanguageContext.tsx
const langContent = fs.readFileSync(LANGUAGE_FILE, 'utf8');
// Regex to find keys in the object definitions.
// Assumes structure: 'key.name': 'Value'
const definedKeysRegex = /'([a-zA-Z0-9_.-]+)':\s*['"`]/g;
const definedKeys = new Set();
let match;
while ((match = definedKeysRegex.exec(langContent)) !== null) {
definedKeys.add(match[1]);
}
console.log(`Found ${definedKeys.size} defined keys in LanguageContext.`);
// 2. Scan usages in .tsx files
const files = getAllFiles(SRC_DIR);
const usedKeys = new Set();
const keyLocation = {}; // Map key -> file path for debugging
// Regex to find t('key')
const usageRegex = /[^a-zA-Z]t\(\s*['"]([a-zA-Z0-9_.-]+)['"]\s*[),]/g;
files.forEach(file => {
const content = fs.readFileSync(file, 'utf8');
while ((match = usageRegex.exec(content)) !== null) {
const key = match[1];
usedKeys.add(key);
if (!keyLocation[key]) {
keyLocation[key] = [];
}
if (!keyLocation[key].includes(file)) {
keyLocation[key].push(file);
}
}
});
console.log(`Found ${usedKeys.size} unique keys used in codebase.`);
// 3. Compare
const missingKeys = [];
usedKeys.forEach(key => {
if (!definedKeys.has(key)) {
missingKeys.push(key);
}
});
console.log('\n--- MISSING KEYS ---');
if (missingKeys.length === 0) {
console.log('No missing keys found! Great job.');
} else {
const output = {
total: missingKeys.length,
keys: missingKeys
};
fs.writeFileSync('missing_keys.json', JSON.stringify(output, null, 2));
console.log(`Saved ${missingKeys.length} missing keys to missing_keys.json`);
}