diff --git a/lib/pipeline/npm.js b/lib/pipeline/npm.js index 3bc419953..46c375c9f 100644 --- a/lib/pipeline/npm.js +++ b/lib/pipeline/npm.js @@ -4,8 +4,6 @@ let utils = require('../utils/utils.js'); let fs = require('../core/fs.js'); let o_fs = require('fs-extra'); -let http = require('follow-redirects').http; -let https = require('follow-redirects').https; var tar = require('tar'); class Npm { @@ -30,19 +28,6 @@ class Npm { let tarball = registryJSON.dist.tarball; - var download = function(url, dest, cb) { - var file = o_fs.createWriteStream(dest); - (url.substring(0,5) === 'https' ? https : http).get(url, function(response) { - response.pipe(file); - file.on('finish', function() { - file.close(cb); // close() is async, call cb after close completes. - }); - }).on('error', function(err) { // Handle errors - fs.unlink(dest); // Delete the file async. (But we don't check the result) - if (cb) cb(err.message); - }); - }; - if (getFromGit) { let repoName = registryJSON.repository.url.replace("git+https://github.com/", "").replace(".git",""); let gitHead = registryJSON.gitHead; @@ -66,7 +51,7 @@ class Npm { fs.mkdirpSync(packageDirectory); self.logger.info("downloading " + packageName + " " + version + "...."); - download(fileLocation, packageDirectory + "/" + packageName + ".js", function() { + utils.downloadFile(fileLocation, packageDirectory + "/" + packageName + ".js", function() { o_fs.createReadStream(packageDirectory + "/" + packageName + ".js").pipe( tar.x({ strip: 1, @@ -97,7 +82,7 @@ class Npm { fs.mkdirpSync(packageDirectory); self.logger.info("downloading " + packageName + " " + version + "...."); - download(tarball, packageDirectory + "/downloaded_package.tgz", function() { + utils.downloadFile(tarball, packageDirectory + "/downloaded_package.tgz", function() { o_fs.createReadStream(packageDirectory + '/downloaded_package.tgz').pipe( tar.x({ strip: 1, diff --git a/lib/utils/utils.js b/lib/utils/utils.js index cae503f41..c72335e41 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -5,6 +5,9 @@ let http = require('follow-redirects').http; let https = require('follow-redirects').https; let shelljs = require('shelljs'); +//let fs = require('../core/fs.js'); +let o_fs = require('fs-extra'); + function joinPath() { return path.join.apply(path.join, arguments); } @@ -61,7 +64,19 @@ function exit(code) { process.exit(code); } -//TODO: Maybe desired to just `module.exports = this` ? +function downloadFile(url, dest, cb) { + var file = o_fs.createWriteStream(dest); + (url.substring(0,5) === 'https' ? https : http).get(url, function(response) { + response.pipe(file); + file.on('finish', function() { + file.close(cb); + }); + }).on('error', function(err) { + o_fs.unlink(dest); + if (cb) cb(err.message); + }); +} + module.exports = { joinPath: joinPath, filesMatchingPattern: filesMatchingPattern, @@ -73,5 +88,6 @@ module.exports = { runCmd: runCmd, cd: cd, sed: sed, - exit: exit + exit: exit, + downloadFile: downloadFile };