extract file download

This commit is contained in:
Iuri Matias 2017-12-15 21:53:11 -05:00
parent 79c8c72477
commit bee4d0e1f2
2 changed files with 20 additions and 19 deletions

View File

@ -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,

View File

@ -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
};