BlueWallet/scripts/find-unused-loc.js

82 lines
2.2 KiB
JavaScript
Raw Normal View History

const fs = require('fs');
const path = require('path');
const mainLocFile = './loc/en.json';
2024-05-05 22:33:06 +02:00
const dirsToInterate = ['components', 'screen', 'blue_modules', 'class', 'hooks', 'helpers', 'navigation'];
2024-05-14 19:17:03 +02:00
const addFiles = ['BlueComponents.js', 'App.tsx', 'navigation/index.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));
}
2024-05-14 19:17:03 +02:00
allDirFiles.push(path.resolve('App.tsx'));
// 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);