-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathverify_nls_keys.js
More file actions
82 lines (70 loc) · 3.32 KB
/
verify_nls_keys.js
File metadata and controls
82 lines (70 loc) · 3.32 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 packageJsonPath = path.join(__dirname, 'package.json');
const packageNlsPath = path.join(__dirname, 'package.nls.json');
try {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
const packageNls = JSON.parse(fs.readFileSync(packageNlsPath, 'utf8'));
// 1. Extract used keys from package.json
const usedKeys = new Set();
function scanForKeys(obj) {
if (typeof obj === 'string') {
const match = obj.match(/^%([^%]+)%$/);
if (match) {
usedKeys.add(match[1]);
}
} else if (typeof obj === 'object' && obj !== null) {
for (const key in obj) {
scanForKeys(obj[key]);
}
}
}
scanForKeys(packageJson);
console.log(`Found ${usedKeys.size} used keys in package.json`);
// 2. Check for unused keys in package.nls.json
const definedKeys = Object.keys(packageNls);
const unusedKeys = definedKeys.filter(key => !usedKeys.has(key));
console.log('--- Unused keys in package.nls.json (not referenced in package.json) ---');
if (unusedKeys.length > 0) {
unusedKeys.forEach(key => console.log(key));
} else {
console.log('None (All keys are used)');
}
// 3. Check other package.nls.*.json files
const dir = __dirname;
const files = fs.readdirSync(dir).filter(f => f.startsWith('package.nls.') && f.endsWith('.json') && f !== 'package.nls.json');
console.log('\n--- Checking other package.nls.*.json files ---');
files.forEach(file => {
const filePath = path.join(dir, file);
const content = JSON.parse(fs.readFileSync(filePath, 'utf8'));
const keys = Object.keys(content);
// Keys in this file but not in main nls (extras)
const extraKeys = keys.filter(k => !definedKeys.includes(k));
// Keys in main nls but not in this file (missing)
const missingKeys = definedKeys.filter(k => !keys.includes(k));
// Unused keys (from step 2) that are present in this file
const unusedInFile = keys.filter(k => unusedKeys.includes(k));
if (extraKeys.length > 0 || missingKeys.length > 0 || unusedInFile.length > 0) {
console.log(`\nResults for ${file}:`);
if (extraKeys.length > 0) {
console.log(` Extra keys (not in package.nls.json) [Count: ${extraKeys.length}]:`);
// Limit output
extraKeys.slice(0, 5).forEach(k => console.log(` ${k}`));
if (extraKeys.length > 5) console.log(` ... and ${extraKeys.length - 5} more`);
}
if (missingKeys.length > 0) {
console.log(` Missing keys (present in package.nls.json) [Count: ${missingKeys.length}]:`);
missingKeys.slice(0, 5).forEach(k => console.log(` ${k}`));
if (missingKeys.length > 5) console.log(` ... and ${missingKeys.length - 5} more`);
}
if (unusedInFile.length > 0) {
console.log(` Unused keys (present here but not used) [Count: ${unusedInFile.length}]:`);
unusedInFile.forEach(k => console.log(` ${k}`));
}
} else {
console.log(`${file}: OK`);
}
});
} catch (err) {
console.error('Error:', err.message);
}