Updating i18n:scrub command with better output

This commit is contained in:
Jeff Avallone 2018-02-16 17:15:45 -05:00
parent 8e8fbd3219
commit d69ab00ad5
2 changed files with 19 additions and 9 deletions

View File

@ -125,6 +125,7 @@
"babel-preset-react": "^6.24.1", "babel-preset-react": "^6.24.1",
"babel-register": "^6.26.0", "babel-register": "^6.26.0",
"cheerio": "^1.0.0-rc.2", "cheerio": "^1.0.0-rc.2",
"colors": "^1.1.2",
"copy-webpack-plugin": "^4.4.1", "copy-webpack-plugin": "^4.4.1",
"cross-env": "^5.1.3", "cross-env": "^5.1.3",
"css-loader": "^0.28.9", "css-loader": "^0.28.9",

View File

@ -2,6 +2,7 @@ const util = require('util');
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const yaml = require('js-yaml'); const yaml = require('js-yaml');
const colors = require('colors/safe');
const readdir = util.promisify(fs.readdir); const readdir = util.promisify(fs.readdir);
const readFile = util.promisify(fs.readFile); const readFile = util.promisify(fs.readFile);
@ -46,12 +47,17 @@ const saveLocales = async locales => {
loadLocales() loadLocales()
.then(async locales => { .then(async locales => {
const requiredKeys = Object.keys(locales.en.translation); const sourceLocale = locales.en.translation;
const requiredKeys = Object.keys(sourceLocale);
const languages = Object.keys(locales).filter(lang => lang !== 'en'); const languages = Object.keys(locales).filter(lang => lang !== 'en');
languages.forEach(langName => { languages.forEach(langName => {
const lang = locales[langName]; const lang = locales[langName];
const presentKeys = Object.keys(lang.translation || {}); const presentKeys = Object.keys(lang).reduce((list, nsName) => {
return list.concat(Object.keys(lang[nsName]));
}, []);
const missingKeys = requiredKeys.filter(key => !presentKeys.includes(key));
const extraKeys = presentKeys.filter(key => !requiredKeys.includes(key));
if (!lang.translation) { if (!lang.translation) {
lang.translation = {}; lang.translation = {};
@ -61,20 +67,23 @@ loadLocales()
lang.missing = {}; lang.missing = {};
} }
requiredKeys.forEach(key => { missingKeys.forEach(key => {
if (!presentKeys.includes(key)) { console.log(colors.yellow.bold('MISSING:'), `${ langName } need values for "${ colors.bold(key) }".`); //eslint-disable-line no-console
console.log(`es needs translation for "${ key }"`); // eslint-disable-line no-console lang.missing[key] = sourceLocale[key];
lang.missing[key] = locales.en.translation[key]; });
}
extraKeys.forEach(key => {
console.log(colors.yellow.bold('EXTRA:'), `${ langName } has extra key for "${ colors.bold(key) }". It should be removed.`); // eslint-disable-line no-console
}); });
}); });
await saveLocales(locales); return locales;
}) })
.then(saveLocales)
.then(() => { .then(() => {
console.log('Done updating locales'); // eslint-disable-line no-console console.log('Done updating locales'); // eslint-disable-line no-console
}) })
.catch(e => { .catch(e => {
console.log(e.toString()); // eslint-disable-line no-console console.error(colors.red.bold('FAILED:'), e); // eslint-disable-line no-console
process.exit(1); process.exit(1);
}); });