From afd10884d1ab4db4b53f73336ae4b51a45026fd2 Mon Sep 17 00:00:00 2001 From: Overtorment Date: Fri, 30 Sep 2022 20:15:10 +0100 Subject: [PATCH] REF: script to detect unused locale keys --- package.json | 2 +- scripts/find-unused-loc.js | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 scripts/find-unused-loc.js diff --git a/package.json b/package.json index 6be3d88d2..567d5eba5 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "e2e:release-build": "detox build -c android.release", "e2e:release-test": "detox test -c android.release --record-videos all --record-logs all --take-screenshots all --headless -d 200000", "tslint": "tsc", - "lint": "eslint --ext .js,.ts,.tsx '*.@(js|ts|tsx)' screen 'blue_modules/*.@(js|ts|tsx)' class models loc tests components && npm run tslint", + "lint": "eslint --ext .js,.ts,.tsx '*.@(js|ts|tsx)' screen 'blue_modules/*.@(js|ts|tsx)' class models loc tests components && npm run tslint && node scripts/find-unused-loc.js", "lint:fix": "npm run lint -- --fix", "lint:quickfix": "git status --porcelain | grep -v '\\.json' | grep -E '\\.js|\\.ts' --color=never | awk '{print $2}' | xargs eslint --fix; exit 0", "unit": "jest -b -i tests/unit/*", diff --git a/scripts/find-unused-loc.js b/scripts/find-unused-loc.js new file mode 100644 index 000000000..76bb0cc14 --- /dev/null +++ b/scripts/find-unused-loc.js @@ -0,0 +1,72 @@ +const fs = require('fs'); +const path = require("path") + +const mainLocFile = './loc/en.json'; +const dirsToInterate = ['components', 'screen', 'blue_modules']; + +let 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)); +} + +// 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)) { + if (allLocKeysHashmap[key] === false) { + console.log('Unused loc key: ' + key); + exitCode = 1; + } +} + +process.exit(exitCode);