UNPKG

@rapharacing/error-handler

Version:

Error Handler

199 lines (165 loc) 6.09 kB
"use strict"; function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } /* eslint-disable consistent-return */ require("dotenv").config(); const path = require("path"); const fse = require("fs-extra"); const axios = require("axios"); const reduce = require("lodash/reduce"); const merge = require("lodash/merge"); const map = require("lodash/map"); const BUILD_DIR = path.join(__dirname, "./locales"); const _process$env = process.env, AIRTABLE_KEY = _process$env.AIRTABLE_KEY, AIRTABLE_SHEET_ID = _process$env.AIRTABLE_SHEET_ID; const config = { AIRTABLE_API_KEY: AIRTABLE_KEY, TRANSLATIONS_SHEET_BASE: `https://api.airtable.com/v0/${AIRTABLE_SHEET_ID}/` }; // Each Page of the Airtable Document const tableUrls = ["Common", "Auth", "SignUp", "ConfirmSignUp", "ForgotPassword", "ChangePassword"]; const localeToFileName = locale => { return locale.toLowerCase(); }; /* This function goes to airtable and fetches all of the translations, as the max records airtable can return in a single call is 100 we use a recursive function to call it with the offset key if it exists. */ const fetchTranslations = /*#__PURE__*/function () { var _ref = _asyncToGenerator(function* ({ offset = "", url = "" }) { try { const response = yield axios.get(url, { params: { view: "Grid view", offset }, headers: { Authorization: `Bearer ${config.AIRTABLE_API_KEY}` } }); let records = response.data.records; if (response.data.offset) { const moreRecords = yield fetchTranslations({ offset: response.data.offset, url: response.config.url }); records = [...records, ...moreRecords]; } return records; } catch (e) { // eslint-disable-next-line console.error("error: ", e); } }); return function fetchTranslations(_x) { return _ref.apply(this, arguments); }; }(); /* This takes the data from the API response and maps over it, first we grab the Key value as this is the translations key. We then rduce the fields object which contains the following shape data { Key: 'translation_key', EN: 'English translation', ES: 'Spanish Translation', ... etc } We ignore the Key iteration as we dont need it, and then create an object that contains the locale key followed by and object that contains the translation key and the locale value: { EN: { translation_key: 'English translation' }, ES: { translation_key: 'Spanish Translation' } }, { EN: { another_translation_key: 'English translation' }, ES: { another_translation_key: 'Spanish Translation' } } This does the same for each row in the airtable table. */ const createLocaleObjects = data => { return data.map(d => { const keyValue = d.fields.Key; return reduce(d.fields, (result, value, key) => { if (key === "Key") { return result; } // eslint-disable-next-line result[key] = { [keyValue]: value }; return result; }, {}); }); }; /* Next we need to simple merge all the objects into one massive object that contains the locale key followed by the object full of keys like so { EN: { translation_key: 'English translation', another_translation_key: 'English translation' }, ES: { translation_key: 'Spanish translation', another_translation_key: 'Spanish translation' } } */ const mergeAllLocales = data => data.reduce((acc, curr) => merge(acc, curr), {}); // The final step is to map over each locale and use the locale key as the filename and then write the object to the correct file const writeLocalesToFiles = data => { map(data, (value, key) => { const fileName = `${BUILD_DIR}/${localeToFileName(key)}/common.json`; fse.outputJson(fileName, value, { spaces: 2 }).then(() => { console.log(`File: ${fileName} has been saved!`); }).catch(e => console.log(e)); }); }; const createTranslationsArray = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(function* () { try { const tables = tableUrls.map( /*#__PURE__*/function () { var _ref3 = _asyncToGenerator(function* (table) { const encodedTableName = encodeURIComponent(table); return fetchTranslations({ url: config.TRANSLATIONS_SHEET_BASE + encodedTableName }); }); return function (_x2) { return _ref3.apply(this, arguments); }; }()); const res = yield Promise.all(tables); const concatArray = []; return concatArray.concat(...res); } catch (e) { // eslint-disable-next-line console.error("error: ", e); } }); return function createTranslationsArray() { return _ref2.apply(this, arguments); }; }(); // This runs each of the steps above const runScript = /*#__PURE__*/function () { var _ref4 = _asyncToGenerator(function* () { if (!AIRTABLE_KEY || !AIRTABLE_SHEET_ID) { throw Error("Missing variable, one of AIRTABLE_KEY or AIRTABLE_SHEET_ID"); } const translations = yield createTranslationsArray(); const localeObjects = createLocaleObjects(translations); const mergedLocales = mergeAllLocales(localeObjects); writeLocalesToFiles(mergedLocales); }); return function runScript() { return _ref4.apply(this, arguments); }; }(); runScript();