From ad4a28f4f689cc9a37a5c2d5dd8c012e964903b7 Mon Sep 17 00:00:00 2001 From: Yamagishi Kazutoshi Date: Tue, 6 Jun 2017 20:31:57 +0900 Subject: [PATCH] Refactor translationRunner.js (#3604) - Use yargs instead of minimist - Simplify validators - Fix typo (RFC5626 -> RFC5646) --- config/webpack/translationRunner.js | 138 ++++++++++++---------------- package.json | 4 +- yarn.lock | 128 +++++++++++++++++++++++++- 3 files changed, 188 insertions(+), 82 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({ diff --git a/package.json b/package.json index 63231a75d..866fbb946 100644 --- a/package.json +++ b/package.json @@ -121,12 +121,12 @@ "eslint-plugin-jsx-a11y": "^4.0.0", "eslint-plugin-react": "^6.10.3", "jsdom": "^10.1.0", - "minimist": "^1.2.0", "mocha": "^3.4.1", "react-intl-translations-manager": "^5.0.0", "react-test-renderer": "^15.5.4", "sinon": "^2.3.2", - "webpack-dev-server": "^2.4.5" + "webpack-dev-server": "^2.4.5", + "yargs": "^8.0.1" }, "optionalDependencies": { "fsevents": "*" diff --git a/yarn.lock b/yarn.lock index 203f80a66..cd4ffd1fc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1497,6 +1497,10 @@ camelcase@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-3.0.0.tgz#32fc4b9fcdaf845fcdf7e73bb97cac2261f0ab0a" +camelcase@^4.1.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd" + caniuse-api@^1.5.2: version "1.6.1" resolved "https://registry.yarnpkg.com/caniuse-api/-/caniuse-api-1.6.1.tgz#b534e7c734c4f81ec5fbe8aca2ad24354b962c6c" @@ -1894,6 +1898,13 @@ cross-spawn@^3.0.0: lru-cache "^4.0.1" which "^1.2.9" +cross-spawn@^4.0.0: + version "4.0.2" + resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-4.0.2.tgz#7b9247621c23adfdd3856004a823cbe397424d41" + dependencies: + lru-cache "^4.0.1" + which "^1.2.9" + cryptiles@2.x.x: version "2.0.5" resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8" @@ -2611,6 +2622,18 @@ evp_bytestokey@^1.0.0: dependencies: create-hash "^1.1.1" +execa@^0.5.0: + version "0.5.1" + resolved "https://registry.yarnpkg.com/execa/-/execa-0.5.1.tgz#de3fb85cb8d6e91c85bcbceb164581785cb57b36" + dependencies: + cross-spawn "^4.0.0" + get-stream "^2.2.0" + is-stream "^1.1.0" + npm-run-path "^2.0.0" + p-finally "^1.0.0" + signal-exit "^3.0.0" + strip-eof "^1.0.0" + exenv@1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/exenv/-/exenv-1.2.0.tgz#3835f127abf075bfe082d0aed4484057c78e3c89" @@ -2792,7 +2815,7 @@ find-up@^1.0.0: path-exists "^2.0.0" pinkie-promise "^2.0.0" -find-up@^2.1.0: +find-up@^2.0.0, find-up@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/find-up/-/find-up-2.1.0.tgz#45d1b7e506c717ddd482775a2b77920a3c0c57a7" dependencies: @@ -2991,6 +3014,13 @@ get-stdin@^4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-4.0.1.tgz#b968c6b0a04384324902e8bf1a5df32579a450fe" +get-stream@^2.2.0: + version "2.3.1" + resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-2.3.1.tgz#5f38f93f346009666ee0150a054167f91bdd95de" + dependencies: + object-assign "^4.0.1" + pinkie-promise "^2.0.0" + getpass@^0.1.1: version "0.1.7" resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.7.tgz#5eff8e3e684d569ae4cb2b1282604e8ba62149fa" @@ -3592,7 +3622,7 @@ is-resolvable@^1.0.0: dependencies: tryit "^1.0.1" -is-stream@^1.0.1: +is-stream@^1.0.1, is-stream@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44" @@ -3823,6 +3853,15 @@ load-json-file@^1.0.0: pinkie-promise "^2.0.0" strip-bom "^2.0.0" +load-json-file@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-2.0.0.tgz#7947e42149af80d696cbf797bcaabcfe1fe29ca8" + dependencies: + graceful-fs "^4.1.2" + parse-json "^2.2.0" + pify "^2.0.0" + strip-bom "^3.0.0" + loader-runner@^2.3.0: version "2.3.0" resolved "https://registry.yarnpkg.com/loader-runner/-/loader-runner-2.3.0.tgz#f482aea82d543e07921700d5a46ef26fdac6b8a2" @@ -4067,6 +4106,12 @@ media-typer@0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" +mem@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/mem/-/mem-1.1.0.tgz#5edd52b485ca1d900fe64895505399a0dfa45f76" + dependencies: + mimic-fn "^1.0.0" + memory-fs@^0.4.0, memory-fs@~0.4.1: version "0.4.1" resolved "https://registry.yarnpkg.com/memory-fs/-/memory-fs-0.4.1.tgz#3a9a20b8462523e447cfbc7e8bb80ed667bfc552" @@ -4136,6 +4181,10 @@ mime@1.3.4, mime@1.3.x, mime@^1.3.4: version "1.3.4" resolved "https://registry.yarnpkg.com/mime/-/mime-1.3.4.tgz#115f9e3b6b3daf2959983cb38f149a2d40eb5d53" +mimic-fn@^1.0.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.1.0.tgz#e667783d92e89dbd342818b5230b9d62a672ad18" + minimalistic-assert@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/minimalistic-assert/-/minimalistic-assert-1.0.0.tgz#702be2dda6b37f4836bcb3f5db56641b64a1d3d3" @@ -4373,6 +4422,12 @@ normalize-url@^1.4.0: query-string "^4.1.0" sort-keys "^1.0.0" +npm-run-path@^2.0.0: + version "2.0.2" + resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-2.0.2.tgz#35a9232dfa35d7067b4cb2ddf2357b1871536c5f" + dependencies: + path-key "^2.0.0" + "npmlog@0 || 1 || 2 || 3 || 4", npmlog@^4.0.0, npmlog@^4.0.2, npmlog@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/npmlog/-/npmlog-4.1.0.tgz#dc59bee85f64f00ed424efb2af0783df25d1c0b5" @@ -4526,6 +4581,14 @@ os-locale@^1.4.0: dependencies: lcid "^1.0.0" +os-locale@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/os-locale/-/os-locale-2.0.0.tgz#15918ded510522b81ee7ae5a309d54f639fc39a4" + dependencies: + execa "^0.5.0" + lcid "^1.0.0" + mem "^1.1.0" + os-tmpdir@^1.0.0, os-tmpdir@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274" @@ -4545,6 +4608,10 @@ output-file-sync@^1.1.0: mkdirp "^0.5.1" object-assign "^4.1.0" +p-finally@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae" + p-limit@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-1.1.0.tgz#b07ff2d9a5d88bec806035895a2bab66a27988bc" @@ -4622,6 +4689,10 @@ path-is-inside@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/path-is-inside/-/path-is-inside-1.0.2.tgz#365417dede44430d1c11af61027facf074bdfc53" +path-key@^2.0.0: + version "2.0.1" + resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40" + path-parse@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.5.tgz#3c1adf871ea9cd6c9431b6ea2bd74a0ff055c4c1" @@ -4644,6 +4715,12 @@ path-type@^1.0.0: pify "^2.0.0" pinkie-promise "^2.0.0" +path-type@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/path-type/-/path-type-2.0.0.tgz#f012ccb8415b7096fc2daa1054c3d72389594c73" + dependencies: + pify "^2.0.0" + pathval@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0" @@ -5630,6 +5707,13 @@ read-pkg-up@^1.0.1: find-up "^1.0.0" read-pkg "^1.0.0" +read-pkg-up@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-2.0.0.tgz#6b72a8048984e0c41e79510fd5e9fa99b3b549be" + dependencies: + find-up "^2.0.0" + read-pkg "^2.0.0" + read-pkg@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-1.1.0.tgz#f5ffaa5ecd29cb31c0474bca7d756b6bb29e3f28" @@ -5638,6 +5722,14 @@ read-pkg@^1.0.0: normalize-package-data "^2.3.2" path-type "^1.0.0" +read-pkg@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-2.0.0.tgz#8ef1c0623c6a6db0dc6713c4bfac46332b2368f8" + dependencies: + load-json-file "^2.0.0" + normalize-package-data "^2.3.2" + path-type "^2.0.0" + readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@^2.0.6, readable-stream@^2.1.4, readable-stream@^2.2.2, readable-stream@^2.2.6, readable-stream@^2.2.9: version "2.2.10" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.10.tgz#effe72bb7c884c0dd335e2379d526196d9d011ee" @@ -6362,6 +6454,10 @@ strip-bom@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3" +strip-eof@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/strip-eof/-/strip-eof-1.0.0.tgz#bb43ff5598a6eb05d89b59fcd129c983313606bf" + strip-indent@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/strip-indent/-/strip-indent-1.0.1.tgz#0c7962a6adefa7bbd4ac366460a638552ae1a0a2" @@ -6900,6 +6996,10 @@ which-module@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/which-module/-/which-module-1.0.0.tgz#bba63ca861948994ff307736089e3b96026c2a4f" +which-module@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.0.tgz#d9ef07dce77b9902b8a3a8fa4b31c3e3f7e6e87a" + which@1, which@^1.2.9: version "1.2.14" resolved "https://registry.yarnpkg.com/which/-/which-1.2.14.tgz#9a87c4378f03e827cecaf1acdf56c736c01c14e5" @@ -6988,6 +7088,12 @@ yargs-parser@^5.0.0: dependencies: camelcase "^3.0.0" +yargs-parser@^7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-7.0.0.tgz#8d0ac42f16ea55debd332caf4c4038b3e3f5dfd9" + dependencies: + camelcase "^4.1.0" + yargs@^6.0.0: version "6.6.0" resolved "https://registry.yarnpkg.com/yargs/-/yargs-6.6.0.tgz#782ec21ef403345f830a808ca3d513af56065208" @@ -7024,6 +7130,24 @@ yargs@^7.0.0: y18n "^3.2.1" yargs-parser "^5.0.0" +yargs@^8.0.1: + version "8.0.1" + resolved "https://registry.yarnpkg.com/yargs/-/yargs-8.0.1.tgz#420ef75e840c1457a80adcca9bc6fa3849de51aa" + dependencies: + camelcase "^4.1.0" + cliui "^3.2.0" + decamelize "^1.1.1" + get-caller-file "^1.0.1" + os-locale "^2.0.0" + read-pkg-up "^2.0.0" + require-directory "^2.1.1" + require-main-filename "^1.0.1" + set-blocking "^2.0.0" + string-width "^2.0.0" + which-module "^2.0.0" + y18n "^3.2.1" + yargs-parser "^7.0.0" + yargs@~3.10.0: version "3.10.0" resolved "https://registry.yarnpkg.com/yargs/-/yargs-3.10.0.tgz#f7ee7bd857dd7c1d2d38c0e74efbd681d1431fd1"