Refactor translationRunner.js (#3604)
- Use yargs instead of minimist - Simplify validators - Fix typo (RFC5626 -> RFC5646)
This commit is contained in:
parent
d8ae3efec3
commit
ad4a28f4f6
3 changed files with 188 additions and 82 deletions
|
@ -1,112 +1,94 @@
|
|||
/*eslint no-console: "off"*/
|
||||
const manageTranslations = require('react-intl-translations-manager').default;
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { default: manageTranslations } = require('react-intl-translations-manager');
|
||||
|
||||
const testRFC5626 = function (reRFC5646) {
|
||||
return function (language) {
|
||||
if (!language.match(reRFC5646)) {
|
||||
throw new Error('Not RFC5626 name');
|
||||
}
|
||||
};
|
||||
const RFC5646_REGEXP = /^[a-z]{2,3}(?:|[A-Z]+)$/;
|
||||
|
||||
const rootDirectory = path.resolve(__dirname, '..', '..');
|
||||
const translationsDirectory = path.resolve(rootDirectory, 'app', 'javascript', 'mastodon', 'locales');
|
||||
const messagesDirectory = path.resolve(rootDirectory, 'build', 'messages');
|
||||
const availableLanguages = fs.readdirSync(translationsDirectory).reduce((languages, filename) => {
|
||||
const basename = path.basename(filename, '.json');
|
||||
if (RFC5646_REGEXP.test(basename)) {
|
||||
languages.push(basename);
|
||||
}
|
||||
return languages;
|
||||
}, []);
|
||||
|
||||
const testRFC5646 = language => {
|
||||
if (!RFC5646_REGEXP.test(language)) {
|
||||
throw new Error('Not RFC5646 name');
|
||||
}
|
||||
};
|
||||
|
||||
const testAvailability = function (availableLanguages) {
|
||||
return function (language) {
|
||||
if ((argv.force !== true) && availableLanguages.indexOf(language) < 0) {
|
||||
throw new Error('Not an available language');
|
||||
}
|
||||
};
|
||||
const testAvailability = language => {
|
||||
if (!availableLanguages.includes(language)) {
|
||||
throw new Error('Not an available language');
|
||||
}
|
||||
};
|
||||
|
||||
const validateLanguages = function (languages, validators) {
|
||||
let invalidLanguages = languages.reduce((acc, language) => {
|
||||
const validateLanguages = (languages, validators) => {
|
||||
const invalidLanguages = languages.reduce((acc, language) => {
|
||||
try {
|
||||
for (let validator of validators) {
|
||||
validator(language);
|
||||
}
|
||||
validators.forEach(validator => validator(language));
|
||||
} catch (error) {
|
||||
acc.push({
|
||||
language,
|
||||
error,
|
||||
});
|
||||
acc.push({ language, error });
|
||||
}
|
||||
return acc;
|
||||
}, []);
|
||||
|
||||
if (invalidLanguages.length > 0) {
|
||||
console.log('\nError: Specified invalid LANGUAGES:');
|
||||
for (let { language, error } of invalidLanguages) {
|
||||
console.error(`* ${language}: ${error}`);
|
||||
}
|
||||
console.log('\nUse yarn "manage:translations -- --help" for usage information\n');
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(`
|
||||
Error: Specified invalid LANGUAGES:
|
||||
${invalidLanguages.map(({ language, error }) => `* ${language}: ${error.message}`).join('\n')}
|
||||
|
||||
Use yarn "manage:translations -- --help" for usage information
|
||||
`);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
const printHelpMessages = function () {
|
||||
console.log(
|
||||
`Usage: yarn manage:translations -- [OPTIONS] [LANGUAGES]
|
||||
const { argv } = require('yargs')
|
||||
.usage(`Usage: yarn manage:translations -- [OPTIONS] [LANGUAGES]
|
||||
|
||||
Manage javascript translation files in mastodon. Generates and update
|
||||
translations in translationsDirectory: ${translationsDirectory}
|
||||
|
||||
OPTIONS
|
||||
-h,--help show this message
|
||||
-f,--force force using the provided languages. create files if not exists.
|
||||
default: false
|
||||
Manage JavaScript translation files in Mastodon. Generates and update translations in translationsDirectory: ${translationsDirectory}
|
||||
|
||||
LANGUAGES
|
||||
The RFC5646 language tag for the language you want to test or fix. If you want
|
||||
to input multiple languages, separate them with space.
|
||||
The RFC5646 language tag for the language you want to test or fix. If you want to input multiple languages, separate them with space.
|
||||
|
||||
Available languages:
|
||||
${availableLanguages}
|
||||
`);
|
||||
};
|
||||
|
||||
// parse arguments
|
||||
const argv = require('minimist')(process.argv.slice(2), {
|
||||
'boolean': [
|
||||
'force',
|
||||
'help',
|
||||
],
|
||||
'alias': {
|
||||
'f': 'force',
|
||||
'h': 'help',
|
||||
},
|
||||
});
|
||||
const translationsDirectory = 'app/javascript/mastodon/locales';
|
||||
const messagesDirectory = 'build/messages';
|
||||
const localeFn = /^([a-z]{2,3}(|\-[A-Z]+))\.json$/;
|
||||
const reRFC5646 = /^[a-z]{2,3}(|\-[A-Z]+)$/;
|
||||
const availableLanguages = fs.readdirSync(`${process.cwd()}/${translationsDirectory}`).reduce((acc, fn) => {
|
||||
if (fn.match(localeFn)) {
|
||||
acc.push(fn.replace(localeFn, '$1'));
|
||||
}
|
||||
return acc;
|
||||
}, []);
|
||||
|
||||
// print help message
|
||||
if (argv.help) {
|
||||
printHelpMessages();
|
||||
process.exit(0);
|
||||
}
|
||||
${availableLanguages.join(', ')}
|
||||
`)
|
||||
.help('h', 'show this message')
|
||||
.alias('h', 'help')
|
||||
.options({
|
||||
f: {
|
||||
alias: 'force',
|
||||
default: false,
|
||||
describe: 'force using the provided languages. create files if not exists.',
|
||||
type: 'boolean',
|
||||
},
|
||||
});
|
||||
|
||||
// check if message directory exists
|
||||
if (!fs.existsSync(`${process.cwd()}/${messagesDirectory}`)) {
|
||||
console.error(`\nError: messageDirectory not exists\n(${process.cwd()}/${messagesDirectory})\n`);
|
||||
console.error('Try to run "yarn build:development" first');
|
||||
if (!fs.existsSync(messagesDirectory)) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(`
|
||||
Error: messagesDirectory not exists
|
||||
(${messagesDirectory})
|
||||
Try to run "yarn build:development" first`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// determine the languages list
|
||||
const languages = (argv._.length === 0) ? availableLanguages : argv._;
|
||||
const languages = (argv._.length > 0) ? argv._ : availableLanguages;
|
||||
|
||||
// validate languages
|
||||
validateLanguages(languages, [
|
||||
testRFC5626(reRFC5646),
|
||||
testAvailability(availableLanguages),
|
||||
]);
|
||||
testRFC5646,
|
||||
!argv.force && testAvailability,
|
||||
].filter(Boolean));
|
||||
|
||||
// manage translations
|
||||
manageTranslations({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue