diff --git a/lib/transifex.js b/lib/transifex.js index 2a7f4168..7273cca7 100644 --- a/lib/transifex.js +++ b/lib/transifex.js @@ -104,4 +104,61 @@ const txResources = async function (project) { return slugs; }; -module.exports = {txPull, txResources}; +/** + * Uploads English source strings to a resource in transifex + * @param {string} project - project slug (for example, "scratch-editor") + * @param {string} resource - resource slug (for example, "blocks") + * @param {object} sourceStrings - json of source strings + */ +const txPush = async function (project, resource, sourceStrings) { + const resourceObj = { + data: { + id: `o:${ORG_NAME}:p:${project}:r:${resource}`, + type: 'resources' + } + }; + + await transifexApi.ResourceStringsAsyncUpload.upload({ + resource: resourceObj, + content: JSON.stringify(sourceStrings) + }); +}; + +/** + * Creates a new resource, and then uploads source strings to it if they are provided + * @param {*} project - project slug (for example, "scratch-editor") + * @param {*} param1 + * - slug - resource slug + * - name - resource name + * - i18nType - i18n format id + * - sourceStrings - json object of source strings + */ +const txCreateResource = async function (project, {slug, name, i18nType, sourceStrings}) { + const i18nFormat = { + data: { + id: i18nType || 'KEYVALUEJSON', + type: 'i18n_formats' + } + }; + + const projectObj = { + data: { + id: `o:${ORG_NAME}:p:${project}`, + type: 'projects' + } + }; + + await transifexApi.Resource.create({ + attributes: {slug: slug, name: name}, + relationships: { + i18n_format: i18nFormat, + project: projectObj + } + }); + + if (sourceStrings) { + await txPush(project, slug, sourceStrings); + } +}; + +module.exports = {txPull, txPush, txResources, txCreateResource}; diff --git a/scripts/tx-push-src.js b/scripts/tx-push-src.js index 68c49527..99d70ee4 100755 --- a/scripts/tx-push-src.js +++ b/scripts/tx-push-src.js @@ -10,7 +10,8 @@ const fs = require('fs'); const path = require('path'); -const transifex = require('transifex'); +const txPush = require('../lib/transifex.js').txPush; +const txCreateResource = require('../lib/transifex.js').txCreateResource; const args = process.argv.slice(2); @@ -33,10 +34,6 @@ if (args.length < 3 || !process.env.TX_TOKEN) { // Globals const PROJECT = args[0]; const RESOURCE = args[1]; -const TX = new transifex({ - project_slug: PROJECT, - credential: 'api:' + process.env.TX_TOKEN -}); let en = fs.readFileSync(path.resolve(args[2])); en = JSON.parse(en); @@ -76,33 +73,31 @@ const getResourceType = (project, resource) => { }; // update Transifex with English source -TX.uploadSourceLanguageMethod(PROJECT, RESOURCE, {content: JSON.stringify(en)}, (err) => { - if (err && err.response.statusCode !== 404) { - process.stdout.write(`Transifex Error: ${err.message}\n`); - process.stdout.write( - `Transifex Error ${err.response.statusCode.toString()}: ${err.response.body}\n`); - process.exitCode = 1; - return; +const pushSource = async function () { + try { + await txPush(PROJECT, RESOURCE, en); + } catch (err) { + if (err.statusCode !== 404) { + process.stdout.write(`Transifex Error: ${err.message}\n`); + process.stdout.write( + `Transifex Error ${err.response.statusCode.toString()}: ${err.response.body}\n`); + process.exitCode = 1; + return; + } + // file not found - create it, but also give message + if (err.statusCode === 404) { + process.stdout.write(`Transifex Resource not found, creating: ${RESOURCE}\n`); + const resourceData = { + slug: RESOURCE, + name: RESOURCE, + priority: 0, // default to normal priority + i18nType: getResourceType(PROJECT, RESOURCE), + content: en + }; + await txCreateResource(PROJECT, resourceData); + } + process.exitCode = 0; } - // file not found - create it, but also give message - if (err && err.response.statusCode === 404) { - process.stdout.write(`Transifex Resource not found, creating: ${RESOURCE}\n`); - const resourceData = { - slug: RESOURCE, - name: RESOURCE, - priority: 0, // default to normal priority - i18n_type: getResourceType(PROJECT, RESOURCE), - content: JSON.stringify(en) - }; - TX.resourceCreateMethod(PROJECT, resourceData, (err1) => { - if (err1) { - process.stdout.write(`Transifex Error: ${err1.message}\n`); - process.stdout.write( - `Transifex Error ${err1.response.statusCode.toString()}: ${err1.response.body}\n`); - process.exitCode = 1; - return; - } - }); - } - process.exitCode = 0; -}); +}; + +pushSource();