about summary refs log tree commit diff
path: root/config/webpack/translationRunner.js
diff options
context:
space:
mode:
Diffstat (limited to 'config/webpack/translationRunner.js')
-rw-r--r--config/webpack/translationRunner.js138
1 files changed, 60 insertions, 78 deletions
diff --git a/config/webpack/translationRunner.js b/config/webpack/translationRunner.js
index 8145c605f..79bb6980f 100644
--- a/config/webpack/translationRunner.js
+++ b/config/webpack/translationRunner.js
@@ -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({