Chargement des profils distant via HTTP

This commit is contained in:
2015-11-05 16:40:42 +01:00
parent 996ccd90f1
commit 736d7600c0
7 changed files with 113 additions and 20 deletions

View File

@@ -3,14 +3,47 @@ var cp = require('child_process');
var glob = require('glob');
var ini = require('ini');
var Cache = require('./cache');
var request = require('request');
var HTTP_REGEX = /^https?:\/\//i;
/**
* Load a JSON file
* Load a JSON file (http or local)
*
* @param filePath The path of the json file
* @return Promise
*/
exports.loadJSON = function(filePath) {
exports.loadJSON = function(jsonUrl) {
if( HTTP_REGEX.test(jsonUrl) ) {
return exports.fetchRemoteJSON(jsonUrl);
} else {
return exports.loadLocalJSON(jsonUrl);
}
};
/**
* Load a remote JSON file via http
*
* @param filePath The path of the json file
* @return Promise
*/
exports.fetchRemoteJSON = function(fileUrl) {
return new Promise(function(resolve, reject) {
request(fileUrl, { followRedirect: true, json: true }, function (err, res, body) {
if(err) return reject(err);
console.log(body);
return resolve(body);
});
});
};
/**
* Load a local JSON file
*
* @param filePath The path of the json file
* @return Promise
*/
exports.loadLocalJSON = function(filePath) {
return new Promise(function(resolve, reject) {
fs.readFile(filePath, 'utf8', function(err, fileContent) {
if(err) return reject(err);