From d69ab00ad5648bde06c26788c3ec78520bc9dbf7 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Fri, 16 Feb 2018 17:15:45 -0500 Subject: [PATCH] Updating i18n:scrub command with better output --- package.json | 1 + script/i18n-scrub.js | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index fb47568..a7490c0 100644 --- a/package.json +++ b/package.json @@ -125,6 +125,7 @@ "babel-preset-react": "^6.24.1", "babel-register": "^6.26.0", "cheerio": "^1.0.0-rc.2", + "colors": "^1.1.2", "copy-webpack-plugin": "^4.4.1", "cross-env": "^5.1.3", "css-loader": "^0.28.9", diff --git a/script/i18n-scrub.js b/script/i18n-scrub.js index ad1e8eb..441bceb 100644 --- a/script/i18n-scrub.js +++ b/script/i18n-scrub.js @@ -2,6 +2,7 @@ const util = require('util'); const path = require('path'); const fs = require('fs'); const yaml = require('js-yaml'); +const colors = require('colors/safe'); const readdir = util.promisify(fs.readdir); const readFile = util.promisify(fs.readFile); @@ -46,12 +47,17 @@ const saveLocales = async locales => { loadLocales() .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'); languages.forEach(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) { lang.translation = {}; @@ -61,20 +67,23 @@ loadLocales() lang.missing = {}; } - requiredKeys.forEach(key => { - if (!presentKeys.includes(key)) { - console.log(`es needs translation for "${ key }"`); // eslint-disable-line no-console - lang.missing[key] = locales.en.translation[key]; - } + missingKeys.forEach(key => { + console.log(colors.yellow.bold('MISSING:'), `${ langName } need values for "${ colors.bold(key) }".`); //eslint-disable-line no-console + lang.missing[key] = sourceLocale[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(() => { console.log('Done updating locales'); // eslint-disable-line no-console }) .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); });