From ece2c092dc2405ef2460f346fd689edd27f4488a Mon Sep 17 00:00:00 2001 From: Alex Fedoseev Date: Tue, 27 Aug 2019 10:44:55 +0800 Subject: [PATCH] Fix extract script --- examples/TranslationsExtractor.re | 81 ------------------------------- examples/extractor/index.js | 46 ++++++++++++++++++ package.json | 2 +- 3 files changed, 47 insertions(+), 82 deletions(-) delete mode 100644 examples/TranslationsExtractor.re create mode 100644 examples/extractor/index.js diff --git a/examples/TranslationsExtractor.re b/examples/TranslationsExtractor.re deleted file mode 100644 index d5dec98..0000000 --- a/examples/TranslationsExtractor.re +++ /dev/null @@ -1,81 +0,0 @@ -open Belt; - -module Fs = Node.Fs; -module Cp = Node.Child_process; -module Path = Node.Path; -module Process = Node.Process; - -let cwd = Process.cwd(); -let src = Path.join([|cwd, "examples"|]); -let bin = Path.join([|cwd, "node_modules", ".bin"|]); - -Js.log({j|=== ⏳ Extracting messages...|j}); -let extracted = - {j|$(bin)/bs-react-intl-extractor $(src)|j} - ->Cp.execSync(Cp.option(~encoding="utf8", ())) - ->Js.Json.parseExn - ->Js.Json.decodeArray - ->Option.getExn; -Js.log({j|=== ✅ Extracting messages... done.\n|j}); - -Locale.all -->Array.forEach(locale => { - let locale = locale->Locale.toString; - - Js.log({j|=== ⏳ Updating $(locale) translations...|j}); - let file = Path.join([|src, "translations", {j|$(locale).json|j}|]); - let cache = - file - ->Fs.readFileSync(`utf8) - ->Js.Json.parseExn - ->Js.Json.decodeArray - ->Option.getExn - ->Array.reduce( - Js.Dict.empty(), - (acc, json) => { - let msg = json->Js.Json.decodeObject->Option.getExn; - let id = - msg - ->Js.Dict.unsafeGet("id") - ->Js.Json.decodeString - ->Option.getExn; - acc->Js.Dict.set(id, msg); - acc; - }, - ); - let messages = - extracted - ->Array.map(json => { - let msg = json->Js.Json.decodeObject->Option.getExn; - let id = - msg->Js.Dict.unsafeGet("id")->Js.Json.decodeString->Option.getExn; - let defaultMessage = - msg - ->Js.Dict.unsafeGet("defaultMessage") - ->Js.Json.decodeString - ->Option.getExn; - let message = - switch (cache->Js.Dict.get(id)) { - | None => "" - | Some(msg) => - switch (msg->Js.Dict.get("message")) { - | None => "" - | Some(message) - when message->Js.Json.decodeString->Option.getExn == "" => "" - | Some(message) => message->Js.Json.decodeString->Option.getExn - } - }; - msg->Js.Dict.set("id", id->Js.Json.string); - msg->Js.Dict.set("defaultMessage", defaultMessage->Js.Json.string); - msg->Js.Dict.set("message", message->Js.Json.string); - msg->Js.Json.object_; - }) - ->Js.Json.array - ->Js.Json.stringifyWithSpace(2); - - file->Fs.writeFileAsUtf8Sync(messages ++ "\n"); - - Js.log({j|=== ✅ Updating $(locale) translations... done.\n|j}); - }); - -Js.log({j|✨ Done.|j}); diff --git a/examples/extractor/index.js b/examples/extractor/index.js new file mode 100644 index 0000000..d71af53 --- /dev/null +++ b/examples/extractor/index.js @@ -0,0 +1,46 @@ +const fs = require("fs"); +const cp = require("child_process"); +const path = require("path"); + +const locales = [ + "en", + "ru", +]; + +const cwd = process.cwd(); +const src = path.join(cwd, "examples"); +const bin = path.join(cwd, "node_modules", ".bin", "bs-react-intl-extractor"); + +console.log(`=== ⏳ Extracting messages...`); +const extracted = JSON.parse(cp.execSync(`${bin} --allow-duplicates ${src}`)); +console.log(`=== ✅ Extracting messages... done.`); + +for (const locale of locales) { + console.log(`\n=== ⏳ Updating ${locale} translation...`); + const file = path.join(src, "translations", `${locale}.json`); + + let content; + try { + content = JSON.parse(fs.readFileSync(file)); + } catch(error) { + if (error.code === "ENOENT") { + console.log(`=== ⚠️ Translation for ${locale} wasn't found. Creating new one.`); + content = []; + } else { + throw error; + } + } + + const cache = content.reduce( + (acc, msg) => ({ ...acc, [msg.id]: msg }), + {}, + ); + const messages = extracted.map(msg => ({ + id: msg.id, + defaultMessage: msg.defaultMessage, + message: cache[msg.id] && cache[msg.id].message ? cache[msg.id].message : "", + })); + + fs.writeFileSync(file, JSON.stringify(messages, null, 2) + "\n"); + console.log(`=== ✅ Updating ${locale} translation... done.`); +} diff --git a/package.json b/package.json index 1cbd350..ae53667 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "build": "bsb -clean-world -make-world", "watch": "bsb -clean-world -make-world -w", "clean": "bsb -clean-world", - "extract": "node examples/TranslationsExtractor.bs.js", + "extract": "node examples/extractor", "preextract": "yarn run build", "format": "bsrefmt --in-place **/*.re", "test": "exit 0",