UNPKG

lingohub-api-simple-client

Version:

Client for the LingoHub REST API to upload and download translations

88 lines (73 loc) 3.41 kB
const axios = require('axios'); const path = require('path'); const fs = require('fs'); const {BASE_URL} = require('./config'); const LANGUAGE_KEY_VARIABLE = /{LANGUAGE_KEY}/g; const getRessources = async ({account, project, token}) => { const RESOURCERS_URL = `${BASE_URL}${account}/projects/${project}/resources?auth_token=${token}`; const resources = await axios.get(RESOURCERS_URL); return resources && resources.data && resources.data.members; }; const downloadAllResources = async ({ account, project, token, workingDirectory, fileName = false, baseOverrideLanguage = false, baseOverrideFileName = false, }) => { const resources = await getRessources({account, project, token}); const workingPath = path.resolve(process.cwd(), workingDirectory); console.info('Download resource files into target directory', workingPath); return Promise.all(resources.map(({links, name, project_locale}) => new Promise(async (resolve, reject) => { let currentWorkingPath = workingPath; if (baseOverrideLanguage && baseOverrideFileName && project_locale === baseOverrideLanguage) { console.info(`Overwrite base locale (${baseOverrideLanguage}) to`, baseOverrideFileName); currentWorkingPath = path.join( currentWorkingPath, baseOverrideFileName.replace(LANGUAGE_KEY_VARIABLE, project_locale), ); } else if (fileName) { currentWorkingPath = path.join(workingPath, fileName.replace(LANGUAGE_KEY_VARIABLE, project_locale)); } else { currentWorkingPath = path.join(workingPath, name); } const fileWriteStream = fs.createWriteStream(path.resolve(currentWorkingPath)); const {data} = await axios.get(`${links[0].href}?auth_token=${token}`, {responseType: "stream"}); data.pipe(fileWriteStream); fileWriteStream.on('close', () => { console.info('Downloaded', name); resolve(); }); fileWriteStream.on('error', err => reject(err)); }))); }; module.exports = async function LingoHubDownload({ account, project, token, workingDirectory, fileName = false, baseOverrideLanguage = false, baseOverrideFileName = false, }) { if (!account || !project || !token || !workingDirectory) { return console.error('Download: invalid parameters. Please consider the help and examples.'); } try { console.info('Start downloading resource files.'); await downloadAllResources({ account, project, token, workingDirectory, fileName, baseOverrideLanguage, baseOverrideFileName }); console.info('Finished downloading resource files.'); } catch (e) { console.error(e); } };