forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-unused-loc.js
81 lines (64 loc) · 2.19 KB
/
find-unused-loc.js
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
const fs = require('fs');
const path = require('path');
const mainLocFile = './loc/en.json';
const dirsToInterate = ['components', 'screen', 'blue_modules', 'class'];
const addFiles = ['BlueComponents.js', 'App.js', 'BlueApp.js', 'Navigation.tsx'];
const allowedLocPrefixes = ['loc.lnurl_auth', 'loc.units'];
const allLocKeysHashmap = {}; // loc key -> used or not
const getAllFiles = function (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 {
arrayOfFiles.push(path.resolve(path.join(dirPath, '/', file)));
}
});
return arrayOfFiles;
};
const allDirFiles = [];
for (const dir of dirsToInterate) {
allDirFiles.push(...getAllFiles(dir));
}
for (const filename of addFiles) {
allDirFiles.push(path.resolve(filename));
}
allDirFiles.push(path.resolve('App.js'));
// got all source files
function objKeysRecursive(obj, depth = []) {
for (const k in obj) {
if (typeof obj[k] === 'object' && obj[k] !== null) {
objKeysRecursive(obj[k], depth.concat(k));
} else {
allLocKeysHashmap['loc.' + depth.join('.') + '.' + k] = false; // false means unused
}
}
}
objKeysRecursive(JSON.parse(fs.readFileSync(mainLocFile).toString('utf8')));
// got all loc keys.
// finally, iterating all source files, readign them and looking for unused loc keys
// iterating all files
for (const filepath of allDirFiles) {
const contents = fs.readFileSync(filepath);
// opened a file. iterating all loc keys
for (const key of Object.keys(allLocKeysHashmap)) {
if (contents.includes(key)) {
// opened file uses this loc key. marking it as used
allLocKeysHashmap[key] = true;
}
}
}
// done! now printing results:
let exitCode = 0;
for (const key of Object.keys(allLocKeysHashmap)) {
let allow = false;
for (const allowedKey of allowedLocPrefixes) {
if (key.startsWith(allowedKey)) allow = true;
}
if (allLocKeysHashmap[key] === false && !allow) {
console.log('Unused loc key: ' + key);
exitCode = 1;
}
}
process.exit(exitCode);