mirror of
https://github.com/elyby/accounts-frontend.git
synced 2025-01-27 22:14:36 +05:30
Fix some bugs on initial run of i18n command
This commit is contained in:
parent
923d7bf83b
commit
4915174d0e
@ -52,38 +52,35 @@ 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 prevMessagesMap = Object.entries(prevMessages).reduce((acc, [key, value]) => {
|
|
||||||
if (acc[value]) {
|
|
||||||
acc[value].push(key);
|
|
||||||
} else {
|
|
||||||
acc[value] = [key];
|
|
||||||
}
|
|
||||||
|
|
||||||
return acc;
|
const prevMessages = readJSON(defaultMessagesPath);
|
||||||
}, {});
|
const prevMessagesMap = Object.entries(prevMessages).reduce((acc, [key, value]) => {
|
||||||
keysToAdd = Object.keys(collectedMessages).filter((key) => !prevMessages[key]);
|
if (acc[value]) {
|
||||||
keysToRemove = Object.keys(prevMessages).filter((key) => !collectedMessages[key]).filter(isNotMarked);
|
acc[value].push(key);
|
||||||
keysToUpdate = Object.entries(prevMessages).reduce((acc, [key, message]) =>
|
} else {
|
||||||
acc.concat(collectedMessages[key] && collectedMessages[key] !== message ? key : [])
|
acc[value] = [key];
|
||||||
, []);
|
}
|
||||||
|
|
||||||
// detect keys to rename, mutating keysToAdd and keysToRemove
|
return acc;
|
||||||
[].concat(keysToAdd).forEach((toKey) => {
|
}, {});
|
||||||
const keys = prevMessagesMap[collectedMessages[toKey]] || [];
|
keysToAdd = Object.keys(collectedMessages).filter((key) => !prevMessages[key]);
|
||||||
const fromKey = keys.find((fromKey) => keysToRemove.indexOf(fromKey) > -1);
|
keysToRemove = Object.keys(prevMessages).filter((key) => !collectedMessages[key]).filter(isNotMarked);
|
||||||
|
keysToUpdate = Object.entries(prevMessages).reduce((acc, [key, message]) =>
|
||||||
|
acc.concat(collectedMessages[key] && collectedMessages[key] !== message ? key : [])
|
||||||
|
, []);
|
||||||
|
|
||||||
if (fromKey) {
|
// detect keys to rename, mutating keysToAdd and keysToRemove
|
||||||
keysToRename.push([fromKey, toKey]);
|
[].concat(keysToAdd).forEach((toKey) => {
|
||||||
|
const keys = prevMessagesMap[collectedMessages[toKey]] || [];
|
||||||
|
const fromKey = keys.find((fromKey) => keysToRemove.indexOf(fromKey) > -1);
|
||||||
|
|
||||||
keysToRemove.splice(keysToRemove.indexOf(fromKey), 1);
|
if (fromKey) {
|
||||||
keysToAdd.splice(keysToAdd.indexOf(toKey), 1);
|
keysToRename.push([fromKey, toKey]);
|
||||||
}
|
|
||||||
});
|
keysToRemove.splice(keysToRemove.indexOf(fromKey), 1);
|
||||||
} catch (err) {
|
keysToAdd.splice(keysToAdd.indexOf(toKey), 1);
|
||||||
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 {};
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user