2018-11-16 02:45:41 -07:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const requestPromise = require('request-promise');
|
2019-08-20 07:12:11 -07:00
|
|
|
const twoskyConfig = require('../../.twosky.json')[0];
|
2018-11-16 02:45:41 -07:00
|
|
|
|
2019-08-20 07:12:11 -07:00
|
|
|
const { project_id: TWOSKY_PROJECT_ID, languages } = twoskyConfig;
|
2018-11-18 23:57:45 -07:00
|
|
|
const LOCALES_DIR = '../../client/src/__locales';
|
2019-08-20 07:12:11 -07:00
|
|
|
const LOCALES_LIST = Object.keys(languages);
|
2019-07-18 08:35:23 -07:00
|
|
|
const BASE_FILE = 'en.json';
|
|
|
|
const TWOSKY_URI = process.env.TWOSKY_URI;
|
2018-11-16 02:45:41 -07:00
|
|
|
|
|
|
|
/**
|
2019-07-18 08:35:23 -07:00
|
|
|
* Prepare params to get translations from twosky
|
2018-11-16 02:45:41 -07:00
|
|
|
* @param {string} locale language shortcut
|
2019-07-18 08:35:23 -07:00
|
|
|
* @param {object} twosky config twosky
|
2018-11-16 02:45:41 -07:00
|
|
|
*/
|
2019-07-18 08:35:23 -07:00
|
|
|
const getRequestUrl = (locale, url, projectId) => {
|
|
|
|
return `${url}/download?format=json&language=${locale}&filename=${BASE_FILE}&project=${projectId}`;
|
2018-11-16 02:45:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Promise wrapper for writing in file
|
|
|
|
* @param {string} filename
|
|
|
|
* @param {any} body
|
|
|
|
*/
|
|
|
|
function writeInFile(filename, body) {
|
2019-07-18 08:35:23 -07:00
|
|
|
let normalizedBody = removeEmpty(JSON.parse(body));
|
|
|
|
|
2018-11-16 02:45:41 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-07-18 08:35:23 -07:00
|
|
|
if (typeof normalizedBody !== 'string') {
|
2018-11-16 02:45:41 -07:00
|
|
|
try {
|
2021-04-13 09:36:55 -07:00
|
|
|
normalizedBody = JSON.stringify(normalizedBody, null, 4) + '\n'; // eslint-disable-line
|
2018-11-16 02:45:41 -07:00
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 08:35:23 -07:00
|
|
|
fs.writeFile(filename, normalizedBody, (err) => {
|
2018-11-16 02:45:41 -07:00
|
|
|
if (err) reject(err);
|
|
|
|
resolve('Ok');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-18 08:35:23 -07:00
|
|
|
* Clear initial from empty value keys
|
|
|
|
* @param {object} initialObject
|
|
|
|
*/
|
|
|
|
function removeEmpty(initialObject) {
|
|
|
|
let processedObject = {};
|
|
|
|
Object.keys(initialObject).forEach(prop => {
|
|
|
|
if (initialObject[prop]) {
|
|
|
|
processedObject[prop] = initialObject[prop];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return processedObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request twosky
|
2018-11-16 02:45:41 -07:00
|
|
|
* @param {string} url
|
|
|
|
* @param {string} locale
|
|
|
|
*/
|
|
|
|
const request = (url, locale) => (
|
|
|
|
requestPromise.get(url)
|
|
|
|
.then((res) => {
|
|
|
|
if (res.length) {
|
|
|
|
const pathToFile = path.join(LOCALES_DIR, `${locale}.json`);
|
|
|
|
return writeInFile(pathToFile, res);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
let result = locale;
|
|
|
|
result += res ? ' - OK' : ' - Empty';
|
|
|
|
return result;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
return `${locale} - Not OK`;
|
|
|
|
}));
|
|
|
|
|
2021-02-10 05:00:39 -07:00
|
|
|
/**
|
|
|
|
* Sleep.
|
|
|
|
* @param {number} ms
|
|
|
|
*/
|
|
|
|
const sleep = (ms) => new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
|
|
|
|
2018-11-16 02:45:41 -07:00
|
|
|
/**
|
|
|
|
* Download locales
|
|
|
|
*/
|
2021-02-10 05:00:39 -07:00
|
|
|
const download = async () => {
|
2018-11-16 02:45:41 -07:00
|
|
|
const locales = LOCALES_LIST;
|
2019-07-18 08:35:23 -07:00
|
|
|
|
|
|
|
if (!TWOSKY_URI) {
|
|
|
|
console.error('No credentials');
|
|
|
|
return;
|
2018-11-16 02:45:41 -07:00
|
|
|
}
|
|
|
|
|
2021-02-10 05:00:39 -07:00
|
|
|
const requests = [];
|
|
|
|
for (let i = 0; i < locales.length; i++) {
|
|
|
|
const locale = locales[i];
|
2019-07-18 08:35:23 -07:00
|
|
|
const url = getRequestUrl(locale, TWOSKY_URI, TWOSKY_PROJECT_ID);
|
2021-02-10 05:00:39 -07:00
|
|
|
requests.push(request(url, locale));
|
|
|
|
|
|
|
|
// Don't request the Crowdin API too aggressively to prevent spurious
|
|
|
|
// 400 errors.
|
|
|
|
await sleep(200);
|
|
|
|
}
|
2018-11-16 02:45:41 -07:00
|
|
|
|
|
|
|
Promise
|
|
|
|
.all(requests)
|
|
|
|
.then((res) => {
|
|
|
|
res.forEach(item => console.log(item));
|
|
|
|
})
|
|
|
|
.catch(err => console.log(err));
|
|
|
|
};
|
|
|
|
|
|
|
|
download();
|