Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Dependencies #5

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
Default format returned is js for i18n-node-2.
*/

var request = require('request');
var fs = require('fs');
var _ = require('lodash');
var async = require('async');
const request = require('request');
const fs = require('fs');
const _ = require('lodash');
const async = require('async');

var path = 'https://api.phraseapp.com/v2';
const path = 'https://api.phraseapp.com/v2';

module.exports = {
initialize: function(options, callback) {
Expand All @@ -19,37 +19,41 @@ module.exports = {

if (!callback) {
callback = function(err) {
if (err) { throw new Error(err); }
if (err) {
throw new Error(err);
}
};
}

var config = module.exports.configure(options);
const config = module.exports.configure(options);
module.exports.download(config, callback);
},

configure: function(options) {
var default_options = {
file_format: "node_json",
file_extension: "js",
let default_options = {
file_format: 'node_json',
file_extension: 'js',
location: process.cwd(),
transform: function(translations) { return translations; }
transform: function(translations) {
return translations;
}
};

return _.extend({}, default_options, options);
},

download: function(options, callback) {
module.exports.fetchLocales(options,
function (err, locales) {
console.log("Got locales", locales);
function(err, locales) {
console.log('Got locales', locales);
if (!err) {
async.eachLimit(locales, 2, function(l, callback) {
module.exports.downloadTranslationFile(l, options, function(err, res) {
if (!err) {
console.log("Translation for " + l + " downloaded successfully.");
console.log('Translation for ' + l + ' downloaded successfully.');
return callback(null);
} else {
console.error("Error downloading " + l + ".", err);
console.error('Error downloading ' + l + '.', err);
return callback(err);
}
});
Expand All @@ -59,42 +63,42 @@ module.exports = {
},

fetchLocales: function(options, callback) {
var locales;
let locales;

request(path + '/projects/' + options.project_id + '/locales?access_token=' + options.access_token, function(err, res, body) {
if (!err && res.statusCode == 200) {
locales = _.pluck(JSON.parse(body), "code");
if (!err && res.statusCode === 200) {
locales = _.pluck(JSON.parse(body), 'code');
return callback(null, locales);
} else if (err) {
console.error("An error occurred when fetching locales", err);
console.error('An error occurred when fetching locales', err);
return callback(err);
}
});
},

downloadTranslationFile: function(locale, options, callback) {
var translationPath = path + '/projects/' + options.project_id + '/locales/' + locale + '/download?access_token=' + options.access_token + '&file_format=' + options.file_format;
let translationPath = path + '/projects/' + options.project_id + '/locales/' + locale + '/download?access_token=' + options.access_token + '&file_format=' + options.file_format;

request(translationPath, function(err, res, body) {
if (!err && res.statusCode >= 200 && res.statusCode < 300) {
var transformed = options.transform(body);
var fileName = options.location + "/" + locale + "." + options.file_extension;
let transformed = options.transform(body);
let fileName = options.location + '/' + locale + '.' + options.file_extension;

fs.writeFile(fileName, transformed, function(err) {
if (err) {
return console.error("An error occured when downloading translation file", err);
return console.error('An error occured when downloading translation file', err);
}

return callback(null, fileName);
})
});
} else {
if (err) {
console.error("An error occured when downloading translation file", err);
console.error('An error occured when downloading translation file', err);
return callback(err);
}
console.error("Got status code " + res.statusCode);
console.error('Got status code ' + res.statusCode);
return callback(true);
}
});
}
}
};
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "load-phraseapp-translations",
"version": "0.2.5",
"version": "0.2.6",
"description": "Download PhraseApp translation files for your app. Default format is i18n-node-2 compliant.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -32,13 +32,13 @@
},
"homepage": "https://github.com/Skookum/load-phraseapp-translations#readme",
"devDependencies": {
"chai": "^3.2.0",
"mocha": "^2.2.5",
"nock": "^2.9.1"
"chai": "^4.3.6",
"mocha": "^9.2.2",
"nock": "^13.2.4"
},
"dependencies": {
"async": "^1.4.2",
"lodash": "^3.10.0",
"request": "^2.60.0"
"async": "^3.2.3",
"lodash": "^4.17.21",
"request": "^2.88.2"
}
}
Loading