extract tar extraction code

This commit is contained in:
Iuri Matias 2017-12-16 12:23:02 -05:00
parent 8e62a1f2e9
commit a216aa980f
2 changed files with 18 additions and 18 deletions

View File

@ -2,9 +2,6 @@
// TODO: this is horrible and needs to be refactored ASAP
let utils = require('../utils/utils.js');
let fs = require('../core/fs.js');
let o_fs = require('fs-extra');
var tar = require('tar');
class Npm {
@ -46,19 +43,14 @@ class Npm {
self.logger.info("downloading " + packageName + " " + version + "....");
utils.downloadFile(fileLocation, packageDirectory + "/" + packageName + ".js", function() {
o_fs.createReadStream(packageDirectory + "/" + packageName + ".js").pipe(
tar.x({
strip: 1,
C: packageDirectory
}).on('end', function() {
utils.extractTar(packageDirectory + "/" + packageName + ".js", packageDirectory, function() {
if (returnContent) {
let distFile = packageDirectory + packageName + ".js";
callback(fs.readFileSync(distFile).toString());
} else {
callback(packageDirectory);
}
})
);
});
});
}
@ -77,19 +69,14 @@ class Npm {
self.logger.info("downloading " + packageName + " " + version + "....");
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() {
utils.extractTar(packageDirectory + "/downloaded_package.tgz", packageDirectory, function() {
if (returnContent) {
let distFile = packageDirectory + returnContent;
callback(fs.readFileSync(distFile).toString());
} else {
callback(packageDirectory);
}
})
);
});
});
}
}

View File

@ -4,6 +4,7 @@ let merge = require('merge');
let http = require('follow-redirects').http;
let https = require('follow-redirects').https;
let shelljs = require('shelljs');
var tar = require('tar');
//let fs = require('../core/fs.js');
let o_fs = require('fs-extra');
@ -92,6 +93,17 @@ function downloadFile(url, dest, cb) {
});
}
function extractTar(filename, packageDirectory, cb) {
o_fs.createReadStream(filename).pipe(
tar.x({
strip: 1,
C: packageDirectory
}).on('end', function() {
cb();
})
);
}
module.exports = {
joinPath: joinPath,
filesMatchingPattern: filesMatchingPattern,
@ -104,5 +116,6 @@ module.exports = {
cd: cd,
sed: sed,
exit: exit,
downloadFile: downloadFile
downloadFile: downloadFile,
extractTar: extractTar
};