Created
May 4, 2021 14:00
-
-
Save lucasdu4rte/cd654103f6886a75a37f067a030205d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// requiring path and fs modules | |
const path = require('path') | |
const fs = require('fs') | |
const dirTree = require('directory-tree') | |
const LOCALE_PATH = 'public/locales' | |
const SRC_PATH = 'src/' | |
const directoryLocalesPath = path.join(__dirname, LOCALE_PATH) | |
const directorySrcPath = path.join(__dirname, SRC_PATH) | |
const localesTree = dirTree(directoryLocalesPath) | |
const getAllFiles = function (dirPath, arrayOfFiles = []) { | |
const files = fs.readdirSync(dirPath, { withFileTypes: true }) | |
files.forEach(function (file) { | |
if (fs.statSync(`${dirPath}/${file.name}`).isDirectory()) { | |
// eslint-disable-next-line no-param-reassign | |
arrayOfFiles = getAllFiles(`${dirPath}/${file.name}`, arrayOfFiles) | |
} else if (file.name.includes('.jsx')) { | |
arrayOfFiles.push(path.join(dirPath, '/', file.name)) | |
} | |
}) | |
return arrayOfFiles | |
} | |
const files = getAllFiles(directorySrcPath) | |
let countFiles = 0 | |
function checkFile(filePath) { | |
const fileData = fs.readFileSync(filePath) | |
const fileContent = fileData.toString().replace(/(\r\n|\n|\r)/gm, '') | |
// console.log(`fileContent`, fileContent) | |
// const matches = fileContent.match(/\{t\{(.*?)\}/g) | |
// const matches = false | |
// console.log(`matches`, matches) | |
// const matches = fileContent.match(/t\((.*?)\)/) | |
// const reg2 = new RegExp(/{t\((.*?)\)}/) | |
const reg = new RegExp(/t\(([^}]*)\)/) | |
const matches = fileContent.match(reg) | |
if (matches) { | |
const termComplete = matches[1] | |
const [file, term] = termComplete | |
.replace('(', '') | |
.replace(')', '') | |
.replace("'", '') | |
.replace("'", '') | |
.split(':') | |
return localesTree.children.map((locale) => { | |
return locale.children.map((childItem) => { | |
const [localeFileName] = childItem.name.split('.') | |
if (childItem.type === 'file' && localeFileName === file) { | |
countFiles += 1 | |
const fileDataLocale = fs.readFileSync(childItem.path) | |
const langContent = fileDataLocale.toString() | |
const langJson = JSON.parse(langContent) | |
// console.log(locale.name) | |
// console.log('filename: ', childItem.name) | |
// console.log('term: ', term) | |
// console.log(`has term: `, !!langJson[term]) | |
// console.log('===============') | |
if (!langJson[term]) { | |
return `${locale.name}: ${termComplete}` | |
} | |
} | |
}) | |
}) | |
} | |
return false | |
} | |
console.log(countFiles) | |
console.log(files.length) | |
const checkedFiles = files | |
.map((file) => { | |
return checkFile(file) | |
}) | |
.flat(Infinity) | |
.filter(Boolean) | |
.filter((arr) => arr.length) | |
console.log(`checkedFiles`, checkedFiles) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment