Skip to content

Commit

Permalink
Fix the 429 responce code when pulling multiple translation files
Browse files Browse the repository at this point in the history
  • Loading branch information
dobrevkalm committed Mar 20, 2020
1 parent 1266548 commit 8c19787
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions src/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,50 @@ module.exports = {
}

let spinners = {};
let fileIndex = 0;
let timeout = 100;

//Loop files and download content
files.forEach( (file) => {
spinners[file.locale_code] = Utils.createSpinner(`Pulling file for ${file.locale_code} locale in ${file.name}`);

Utils.get(`/files/${masterId}/locales/${file.locale_code}`)
.then( (result) => {
// Download the content for each file
this.getTranslatedFile(fileIndex, files, spinners, masterId, timeout);
});
},

//Remove null values
if(config.removeNullValues){
result = Utils.stripNullValues(result);
/**
* Recurrsively send GET requests with a timeout between them
*/
getTranslatedFile: function(fileIndex, files, spinners, masterId, timeout) {
if (fileIndex >= files.length) return;
setTimeout(() => {
const file = files[fileIndex];
spinners[file.locale_code] = Utils.createSpinner(`Pulling file for ${file.locale_code} locale in ${file.name}`);

Utils.get(`/files/${masterId}/locales/${file.locale_code}`)
.then( (result) => {

//Remove null values
if(config.removeNullValues){
result = Utils.stripNullValues(result);
}

//Write file
fs.outputFile(process.cwd() + `/${file.name}`, JSON.stringify(result, null, 4), (err) => {
if(err){
spinners[file.locale_code].fail();

Utils.handleError({
"error": `There was a problem writing ${file.name}`
});
} else {
spinners[file.locale_code].succeed();
}

//Write file
fs.outputFile(process.cwd() + `/${file.name}`, JSON.stringify(result, null, 4), (err) => {
if(err){
spinners[file.locale_code].fail();

Utils.handleError({
"error": `There was a problem writing ${file.name}`
});
} else {
spinners[file.locale_code].succeed();
}
});
})
.catch( (err) => {
spinners[file.locale_code].fail();
});
})
.catch( (err) => {
spinners[file.locale_code].fail();
});

});
// call recursively with the next file
this.getTranslatedFile(++fileIndex, files, spinners, masterId, timeout);
}, timeout);
},

/**
Expand Down

0 comments on commit 8c19787

Please sign in to comment.