Files
freeCodeCamp/client/i18n/validate-keys.js
Oliver Eyton-Williams e118dda13a fix: order imports and remove circular dependencies (#41824)
* fix: remove circular dependency

redux depended on templates/Challenges/redux and vice versa.  This
meant that import order mattered and confusing bugs could arise.

(cherry picked from commit 7d67a4e70922bbb3051f2f9982dcc69e240d43dc)

* feat: require imports to be in alphabetical order

Import order generally does not matter, but there are edge cases
(circular  imports and css imports, for example) where changing order
changes behaviour

(cherry picked from commit b8d1393a91ec6e068caf8e8498a5c95df68c2b2c)

* chore: order imports

* fix: lift up challenge description + title comps

This brings the classic Show closer to the others as they
now all create the description and title components

* fix: remove donation-saga/index circular import

(cherry picked from commit 51a44ca668a700786d2744feffeae4fdba5fd207)

* refactor: extract action-types from settings

(cherry picked from commit 25e26124d691c84a0d0827d41dafb761c686fadd)

* fix: lint errors

* feat: prevent useless renames
2021-08-02 08:39:40 -05:00

99 lines
3.4 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const introObject = require('./locales/english/intro.json');
const linksObject = require('./locales/english/links.json');
const metaObject = require('./locales/english/meta-tags.json');
const motivationObject = require('./locales/english/motivation.json');
const translationsObject = require('./locales/english/translations.json');
const trendingObject = require('./locales/english/trending.json');
/**
* Function to flatten a nested object. Written specifically for
* our translation flow, the `namespace` value is used to create the
* property chains that are used in the i18n replacement scripts.
* @param {Object} obj
* @param {string} namespace
*/
const flattenAnObject = (obj, namespace = '') => {
const flattened = {};
Object.keys(obj).forEach(key => {
if (Array.isArray(obj[key])) {
flattened[namespace ? `${namespace}.${key}` : key] = obj[key];
} else if (typeof obj[key] === 'object') {
Object.assign(
flattened,
flattenAnObject(obj[key], namespace ? `${namespace}.${key}` : key)
);
} else {
flattened[namespace ? `${namespace}.${key}` : key] = obj[key];
}
});
return flattened;
};
const translationKeys = Object.keys(flattenAnObject(translationsObject));
const metaKeys = Object.keys(flattenAnObject(metaObject));
const motivationKeys = Object.keys(flattenAnObject(motivationObject));
const introKeys = Object.keys(flattenAnObject(introObject));
const trendingKeys = Object.keys(flattenAnObject(trendingObject));
const linksKeys = Object.keys(flattenAnObject(linksObject));
/**
* Recursively read through the directory, grabbing .js files
* in each nested subdirectory and concatenating them all in
* to one string.
* @param {String} filePath
*/
const readComponentCode = filePath => {
let code = '';
const isItFolder = fs.lstatSync(filePath).isDirectory();
if (isItFolder) {
const contents = fs.readdirSync(filePath);
contents.forEach(file => {
code += readComponentCode(path.join(filePath + '/' + file));
});
} else {
if (!filePath.endsWith('.js') || filePath.endsWith('.test.js')) {
return '';
}
code += fs.readFileSync(filePath);
}
return code;
};
const clientCodebase = readComponentCode(path.join(process.cwd() + '/src'));
const serverCodebase = readComponentCode(
path.join(process.cwd() + '/../api-server/src/server')
);
for (const key of translationKeys) {
if (!clientCodebase.includes(key) && !serverCodebase.includes(key)) {
console.warn(`The translation key '${key}' appears to be unused.`);
}
}
for (const key of motivationKeys) {
if (!clientCodebase.includes(key) && !serverCodebase.includes(key)) {
console.warn(`The motivation key '${key}' appears to be unused.`);
}
}
for (const key of metaKeys) {
if (!clientCodebase.includes(key) && !serverCodebase.includes(key)) {
console.warn(`The meta key '${key}' appears to be unused.`);
}
}
for (const key of introKeys) {
if (!clientCodebase.includes(key) && !serverCodebase.includes(key)) {
console.warn(`The intro key '${key}' appears to be unused.`);
}
}
for (const key of trendingKeys) {
if (!clientCodebase.includes(key) && !serverCodebase.includes(key)) {
console.warn(`The trending key '${key}' appears to be unused.`);
}
}
for (const key of linksKeys) {
if (!clientCodebase.includes(key) && !serverCodebase.includes(key)) {
console.warn(`The links key '${key}' appears to be unused.`);
}
}