From 510bc2c542dc05901c3c505f9bcef493625579cb Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 15 Dec 2017 22:05:38 -0500 Subject: [PATCH] refactor httpsGet --- lib/pipeline/npm.js | 95 +++++++++++++++++++++------------------------ lib/utils/utils.js | 10 ++++- 2 files changed, 53 insertions(+), 52 deletions(-) diff --git a/lib/pipeline/npm.js b/lib/pipeline/npm.js index 46c375c9..1c066621 100644 --- a/lib/pipeline/npm.js +++ b/lib/pipeline/npm.js @@ -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); } }) - ); - }); - } + ); + }); } + } - }); }); } } diff --git a/lib/utils/utils.js b/lib/utils/utils.js index c72335e4..9fabfb1e 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -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) {