2016-11-29 11:42:20 +05:30
|
|
|
/* eslint-env node */
|
2016-07-31 00:46:05 +05:30
|
|
|
/* eslint-disable no-console */
|
2016-05-09 00:58:51 +05:30
|
|
|
import fs from 'fs';
|
2019-11-27 14:33:32 +05:30
|
|
|
import { sync as globSync } from 'glob';
|
|
|
|
import { sync as mkdirpSync } from 'mkdirp';
|
2016-05-09 00:58:51 +05:30
|
|
|
import chalk from 'chalk';
|
|
|
|
import prompt from 'prompt';
|
2019-12-08 00:32:00 +05:30
|
|
|
import localesMap from 'app/i18n';
|
2019-07-01 09:09:59 +05:30
|
|
|
|
2019-12-30 13:51:28 +05:30
|
|
|
const MESSAGES_PATTERN = `${__dirname}/../../build/messages/**/*.json`;
|
2019-12-08 00:32:00 +05:30
|
|
|
const LANG_DIR = `${__dirname}/../app/i18n`;
|
2016-05-09 00:58:51 +05:30
|
|
|
const DEFAULT_LOCALE = 'en';
|
2018-03-14 17:48:58 +05:30
|
|
|
const SUPPORTED_LANGS = [DEFAULT_LOCALE, ...Object.keys(localesMap)];
|
2016-05-09 00:58:51 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Aggregates the default messages that were extracted from the app's
|
|
|
|
* React components via the React Intl Babel plugin. An error will be thrown if
|
|
|
|
* there are messages in different components that use the same `id`. The result
|
|
|
|
* is a flat collection of `id: message` pairs for the app's default locale.
|
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
let idToFileMap: { [key: string]: string[] } = {};
|
|
|
|
let duplicateIds: string[] = [];
|
2016-07-31 00:46:05 +05:30
|
|
|
const collectedMessages = globSync(MESSAGES_PATTERN)
|
2019-11-27 14:33:32 +05:30
|
|
|
.map(filename => [filename, JSON.parse(fs.readFileSync(filename, 'utf8'))])
|
|
|
|
.reduce((collection, [file, descriptors]) => {
|
|
|
|
descriptors.forEach(({ id, defaultMessage }) => {
|
|
|
|
if (collection.hasOwnProperty(id)) {
|
|
|
|
duplicateIds.push(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
collection[id] = defaultMessage;
|
|
|
|
idToFileMap[id] = (idToFileMap[id] || []).concat(file);
|
|
|
|
});
|
|
|
|
|
|
|
|
return collection;
|
|
|
|
}, {});
|
2016-05-09 00:58:51 +05:30
|
|
|
|
|
|
|
if (duplicateIds.length) {
|
2019-11-27 14:33:32 +05:30
|
|
|
console.log('\nFound duplicated ids:');
|
|
|
|
duplicateIds.forEach(id =>
|
|
|
|
console.log(`${chalk.yellow(id)}:\n - ${idToFileMap[id].join('\n - ')}\n`),
|
|
|
|
);
|
|
|
|
console.log(chalk.red('Please correct the errors above to proceed further!'));
|
2019-12-07 16:58:52 +05:30
|
|
|
|
|
|
|
process.exit(1);
|
2016-05-09 00:58:51 +05:30
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
duplicateIds = [];
|
|
|
|
idToFileMap = {};
|
2016-05-09 00:58:51 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Making a diff with the previous DEFAULT_LOCALE version
|
|
|
|
*/
|
|
|
|
const defaultMessagesPath = `${LANG_DIR}/${DEFAULT_LOCALE}.json`;
|
2019-12-07 16:58:52 +05:30
|
|
|
let keysToUpdate: string[] = [];
|
|
|
|
let keysToAdd: string[] = [];
|
|
|
|
let keysToRemove: string[] = [];
|
|
|
|
const keysToRename: Array<[string, string]> = [];
|
|
|
|
const isNotMarked = (value: string) => value.slice(0, 2) !== '--';
|
2016-07-31 00:46:05 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
const prevMessages: { [key: string]: string } = readJSON(defaultMessagesPath);
|
2019-11-27 14:33:32 +05:30
|
|
|
const prevMessagesMap = Object.entries(prevMessages).reduce(
|
|
|
|
(acc, [key, value]) => {
|
2016-09-04 14:11:33 +05:30
|
|
|
if (acc[value]) {
|
2019-11-27 14:33:32 +05:30
|
|
|
acc[value].push(key);
|
2016-09-04 14:11:33 +05:30
|
|
|
} else {
|
2019-11-27 14:33:32 +05:30
|
|
|
acc[value] = [key];
|
2016-09-04 14:11:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
2019-12-07 16:58:52 +05:30
|
|
|
{} as { [key: string]: string[] },
|
2019-11-27 14:33:32 +05:30
|
|
|
);
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
keysToAdd = Object.keys(collectedMessages).filter(key => !prevMessages[key]);
|
|
|
|
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 : [],
|
|
|
|
),
|
2019-12-07 16:58:52 +05:30
|
|
|
[] as string[],
|
2019-11-27 14:33:32 +05:30
|
|
|
);
|
2016-09-04 14:11:33 +05:30
|
|
|
|
|
|
|
// detect keys to rename, mutating keysToAdd and keysToRemove
|
2019-12-07 16:58:52 +05:30
|
|
|
[...keysToAdd].forEach(toKey => {
|
2019-11-27 14:33:32 +05:30
|
|
|
const keys = prevMessagesMap[collectedMessages[toKey]] || [];
|
2019-12-07 16:58:52 +05:30
|
|
|
const fromKey = keys.find(key => keysToRemove.indexOf(key) > -1);
|
2016-09-04 14:11:33 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (fromKey) {
|
|
|
|
keysToRename.push([fromKey, toKey]);
|
2016-09-04 14:11:33 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
keysToRemove.splice(keysToRemove.indexOf(fromKey), 1);
|
|
|
|
keysToAdd.splice(keysToAdd.indexOf(toKey), 1);
|
|
|
|
}
|
2016-09-04 14:11:33 +05:30
|
|
|
});
|
2016-05-09 00:58:51 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (
|
|
|
|
!keysToAdd.length &&
|
|
|
|
!keysToRemove.length &&
|
|
|
|
!keysToUpdate.length &&
|
|
|
|
!keysToRename.length
|
|
|
|
) {
|
|
|
|
console.log(chalk.green('Everything is up to date!'));
|
|
|
|
process.exit();
|
2016-05-09 00:58:51 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
console.log(
|
|
|
|
chalk.magenta(`The diff relative to default locale (${DEFAULT_LOCALE}) is:`),
|
|
|
|
);
|
2016-05-09 00:58:51 +05:30
|
|
|
|
|
|
|
if (keysToRemove.length) {
|
2019-11-27 14:33:32 +05:30
|
|
|
console.log('The following keys will be removed:');
|
|
|
|
console.log(
|
|
|
|
[chalk.red('\n - '), keysToRemove.join(chalk.red('\n - ')), '\n'].join(''),
|
|
|
|
);
|
2016-05-09 00:58:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (keysToAdd.length) {
|
2019-11-27 14:33:32 +05:30
|
|
|
console.log('The following keys will be added:');
|
|
|
|
console.log(
|
|
|
|
[chalk.green('\n + '), keysToAdd.join(chalk.green('\n + ')), '\n'].join(''),
|
|
|
|
);
|
2016-05-09 00:58:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (keysToUpdate.length) {
|
2019-11-27 14:33:32 +05:30
|
|
|
console.log('The following keys will be updated:');
|
|
|
|
console.log(
|
|
|
|
[
|
|
|
|
chalk.yellow('\n @ '),
|
|
|
|
keysToUpdate.join(chalk.yellow('\n @ ')),
|
|
|
|
'\n',
|
|
|
|
].join(''),
|
|
|
|
);
|
2016-07-31 00:46:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (keysToRename.length) {
|
2019-11-27 14:33:32 +05:30
|
|
|
console.log('The following keys will be renamed:\n');
|
|
|
|
console.log(
|
|
|
|
keysToRename.reduce(
|
|
|
|
(str, pair) =>
|
|
|
|
[str, pair[0], chalk.yellow(' -> '), pair[1], '\n'].join(''),
|
|
|
|
'',
|
|
|
|
),
|
|
|
|
);
|
2016-05-09 00:58:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
prompt.start();
|
2019-11-27 14:33:32 +05:30
|
|
|
prompt.get(
|
|
|
|
{
|
2016-05-09 00:58:51 +05:30
|
|
|
properties: {
|
2019-11-27 14:33:32 +05:30
|
|
|
apply: {
|
|
|
|
description: 'Apply changes? [Y/n]',
|
|
|
|
pattern: /^y|n$/i,
|
|
|
|
message: 'Please enter "y" or "n"',
|
|
|
|
default: 'y',
|
|
|
|
before: value => value.toLowerCase() === 'y',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
(err, resp) => {
|
2016-05-09 00:58:51 +05:30
|
|
|
console.log('\n');
|
|
|
|
|
|
|
|
if (err || !resp.apply) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return console.log(chalk.red('Aborted'));
|
2016-05-09 00:58:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
buildLocales();
|
|
|
|
|
|
|
|
console.log(chalk.green('All locales was successfuly built'));
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
);
|
2016-05-09 00:58:51 +05:30
|
|
|
|
|
|
|
function buildLocales() {
|
2019-11-27 14:33:32 +05:30
|
|
|
mkdirpSync(LANG_DIR);
|
|
|
|
|
|
|
|
SUPPORTED_LANGS.map(lang => {
|
|
|
|
const destPath = `${LANG_DIR}/${lang}.json`;
|
|
|
|
const newMessages = readJSON(destPath);
|
|
|
|
|
|
|
|
keysToRename.forEach(([fromKey, toKey]) => {
|
|
|
|
newMessages[toKey] = newMessages[fromKey];
|
|
|
|
delete newMessages[fromKey];
|
|
|
|
});
|
|
|
|
keysToRemove.forEach(key => {
|
|
|
|
delete newMessages[key];
|
|
|
|
});
|
|
|
|
keysToUpdate.forEach(key => {
|
|
|
|
newMessages[`--${key}`] = newMessages[key];
|
|
|
|
newMessages[key] = collectedMessages[key];
|
2016-05-09 00:58:51 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
keysToAdd.forEach(key => {
|
|
|
|
newMessages[key] = collectedMessages[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
const sortedKeys = Object.keys(newMessages).sort((key1, key2) => {
|
|
|
|
key1 = key1.replace(/^-+/, '');
|
|
|
|
key2 = key2.replace(/^-+/, '');
|
|
|
|
|
|
|
|
return key1 < key2 || !isNotMarked(key1) ? -1 : 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
const sortedNewMessages = sortedKeys.reduce((acc, key) => {
|
|
|
|
acc[key] = newMessages[key];
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
destPath,
|
|
|
|
`${JSON.stringify(sortedNewMessages, null, 4)}\n`,
|
|
|
|
);
|
|
|
|
});
|
2016-05-09 00:58:51 +05:30
|
|
|
}
|
2016-09-04 14:11:33 +05:30
|
|
|
|
|
|
|
function readJSON(destPath) {
|
2019-11-27 14:33:32 +05:30
|
|
|
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 {};
|
2016-09-04 14:11:33 +05:30
|
|
|
}
|