Fix some bugs on initial run of i18n command

This commit is contained in:
SleepWalker
2016-09-04 11:41:33 +03:00
parent 923d7bf83b
commit 4915174d0e

View File

@ -52,8 +52,8 @@ let keysToAdd = [];
let keysToRemove = []; let keysToRemove = [];
const keysToRename = []; const keysToRename = [];
const isNotMarked = (value) => value.slice(0, 2) !== '--'; const isNotMarked = (value) => value.slice(0, 2) !== '--';
try {
const prevMessages = JSON.parse(fs.readFileSync(defaultMessagesPath, 'utf8')); const prevMessages = readJSON(defaultMessagesPath);
const prevMessagesMap = Object.entries(prevMessages).reduce((acc, [key, value]) => { const prevMessagesMap = Object.entries(prevMessages).reduce((acc, [key, value]) => {
if (acc[value]) { if (acc[value]) {
acc[value].push(key); acc[value].push(key);
@ -81,9 +81,6 @@ try {
keysToAdd.splice(keysToAdd.indexOf(toKey), 1); keysToAdd.splice(keysToAdd.indexOf(toKey), 1);
} }
}); });
} catch (err) {
console.log(chalk.yellow(`Can not read ${defaultMessagesPath}. The new file will be created.`), err);
}
if (!keysToAdd.length && !keysToRemove.length && !keysToUpdate.length && !keysToRename.length) { if (!keysToAdd.length && !keysToRemove.length && !keysToUpdate.length && !keysToRename.length) {
return console.log(chalk.green('Everything is up to date!')); return console.log(chalk.green('Everything is up to date!'));
@ -142,13 +139,7 @@ function buildLocales() {
SUPPORTED_LANGS.map((lang) => { SUPPORTED_LANGS.map((lang) => {
const destPath = `${LANG_DIR}/${lang}.json`; const destPath = `${LANG_DIR}/${lang}.json`;
const newMessages = readJSON(destPath);
let newMessages = {};
try {
newMessages = JSON.parse(fs.readFileSync(destPath, 'utf8'));
} catch (err) {
console.log(chalk.yellow(`Can not read ${destPath}. The new file will be created.`), err);
}
keysToRename.forEach(([fromKey, toKey]) => { keysToRename.forEach(([fromKey, toKey]) => {
newMessages[toKey] = newMessages[fromKey]; newMessages[toKey] = newMessages[fromKey];
@ -181,3 +172,13 @@ function buildLocales() {
fs.writeFileSync(destPath, JSON.stringify(sortedNewMessages, null, 4) + '\n'); fs.writeFileSync(destPath, JSON.stringify(sortedNewMessages, null, 4) + '\n');
}); });
} }
function readJSON(destPath) {
try {
return JSON.parse(fs.readFileSync(destPath, 'utf8'));
} catch (err) {
console.log(chalk.yellow(`Can not read ${destPath}. The new file will be created.`), `(${err.message})`);
}
return {};
}