var fs = require('fs');
var path = require('path');
var TransifexApi = require('transifex-api-es6');
require('dotenv').config();

var transifex = new TransifexApi({
    user: process.env.TRANSIFEX_USERNAME,
    password: process.env.TRANSIFEX_PASSWORD,
    projectName: 'subject-search',
});

// Until PR is merged
transifex.baseUrl = `https://www.transifex.com/api/2/project/subject-search`;

let resource = 'emnesok'
let resourceSlug = `${resource}pot`;
let languages = ['nb', 'nn'];
let poDir = path.join(__dirname, '../src/po/');
let potFile = path.join(poDir, `${resource}.pot`);


function pull() {
    return Promise.all(
        languages.map(
            lang => transifex.getResourceTranslation(lang, resourceSlug)
                .then(poData => {
                    let poFile = path.join(poDir, `${lang}/${resource}.po`);
                    fs.writeFileSync(poFile, poData);
                    console.log(`Wrote ${poFile}`);
                })
        )
    )
}

function push() {
    transifex.getResource(resourceSlug)
        .then(data => {
            return {
                i18n_type: data.i18n_type,
                name: data.name,
                slug: data.slug,
                content: fs.createReadStream(potFile),
            };
        })
        .then(resourceData => {
            return transifex.updateResource(resourceData, resourceSlug);
        })
        .then(results => {
            console.log('Results:');
            console.log(results);
        })
        .catch((err) => {
            console.error(err);
        });
}

switch (process.argv[2]) {
case 'pull':
    pull();
    break;
case 'push':
    push();
    break;
default:
    console.log('Unknown command!');
}

@@ -436,22 +436,14 @@
  = %60
-$%7Bresource%7Dpot
+master
 %60;%0Al
@@ -938,32 +938,122 @@
              %7D)%0A
+                .catch(err =%3E %7B%0A                    console.error(err)%0A                %7D)%0A
         )%0A    )%0A

Fix transifex script