refactor httpsGet

This commit is contained in:
Iuri Matias 2017-12-15 22:05:38 -05:00
parent bee4d0e1f2
commit 510bc2c542
2 changed files with 53 additions and 52 deletions

View File

@ -17,43 +17,37 @@ class Npm {
let self = this;
let npmRegistry = "https://registry.npmjs.org/" + packageName + "/" + version;
utils.httpsGet(npmRegistry, function (res) {
utils.httpsGet(npmRegistry, function (body) {
let registryJSON = JSON.parse(body);
let body = '';
res.on('data', function (d) {
body += d;
});
res.on('end', function () {
let registryJSON = JSON.parse(body);
let tarball = registryJSON.dist.tarball;
let tarball = registryJSON.dist.tarball;
if (getFromGit) {
let repoName = registryJSON.repository.url.replace("git+https://github.com/", "").replace(".git","");
let gitHead = registryJSON.gitHead;
if (getFromGit) {
let repoName = registryJSON.repository.url.replace("git+https://github.com/", "").replace(".git","");
let gitHead = registryJSON.gitHead;
if (!gitHead) {
console.error("Could not download " + packageName + " " + version);
return callback("error");
}
if (!gitHead) {
console.error("Could not download " + packageName + " " + version);
return callback("error");
}
let fileLocation = "https://raw.githubusercontent.com/" + repoName + "/" + gitHead + "/dist/web3.min.js";
let fileLocation = "https://raw.githubusercontent.com/" + repoName + "/" + gitHead + "/dist/web3.min.js";
let packageDirectory = './.embark/versions/' + packageName + '/' + version + '/';
if (fs.existsSync(packageDirectory + "/" + packageName + ".js")) {
if (returnContent) {
let distFile = packageDirectory + packageName + ".js";
callback(fs.readFileSync(distFile).toString());
} else {
callback(packageDirectory);
}
let packageDirectory = './.embark/versions/' + packageName + '/' + version + '/';
if (fs.existsSync(packageDirectory + "/" + packageName + ".js")) {
if (returnContent) {
let distFile = packageDirectory + packageName + ".js";
callback(fs.readFileSync(distFile).toString());
} else {
fs.mkdirpSync(packageDirectory);
self.logger.info("downloading " + packageName + " " + version + "....");
callback(packageDirectory);
}
} else {
fs.mkdirpSync(packageDirectory);
self.logger.info("downloading " + packageName + " " + version + "....");
utils.downloadFile(fileLocation, packageDirectory + "/" + packageName + ".js", function() {
o_fs.createReadStream(packageDirectory + "/" + packageName + ".js").pipe(
tar.x({
utils.downloadFile(fileLocation, packageDirectory + "/" + packageName + ".js", function() {
o_fs.createReadStream(packageDirectory + "/" + packageName + ".js").pipe(
tar.x({
strip: 1,
C: packageDirectory
}).on('end', function() {
@ -64,27 +58,27 @@ class Npm {
callback(packageDirectory);
}
})
);
});
}
);
});
}
} else {
} else {
let packageDirectory = './.embark/versions/' + packageName + '/' + version + '/';
if (fs.existsSync(packageDirectory + "/downloaded_package.tgz")) {
if (returnContent) {
let distFile = packageDirectory + returnContent;
callback(fs.readFileSync(distFile).toString());
} else {
callback(packageDirectory);
}
let packageDirectory = './.embark/versions/' + packageName + '/' + version + '/';
if (fs.existsSync(packageDirectory + "/downloaded_package.tgz")) {
if (returnContent) {
let distFile = packageDirectory + returnContent;
callback(fs.readFileSync(distFile).toString());
} else {
fs.mkdirpSync(packageDirectory);
self.logger.info("downloading " + packageName + " " + version + "....");
callback(packageDirectory);
}
} else {
fs.mkdirpSync(packageDirectory);
self.logger.info("downloading " + packageName + " " + version + "....");
utils.downloadFile(tarball, packageDirectory + "/downloaded_package.tgz", function() {
o_fs.createReadStream(packageDirectory + '/downloaded_package.tgz').pipe(
tar.x({
utils.downloadFile(tarball, packageDirectory + "/downloaded_package.tgz", function() {
o_fs.createReadStream(packageDirectory + '/downloaded_package.tgz').pipe(
tar.x({
strip: 1,
C: packageDirectory
}).on('end', function() {
@ -95,12 +89,11 @@ class Npm {
callback(packageDirectory);
}
})
);
});
}
);
});
}
}
});
});
}
}

View File

@ -37,7 +37,15 @@ function httpGet(url, callback) {
}
function httpsGet(url, callback) {
return https.get(url, callback);
https.get(url, function(res) {
let body = '';
res.on('data', function (d) {
body += d;
});
res.on('end', function () {
callback(body);
});
});
}
function runCmd(cmd, options) {