Skip to content

Commit

Permalink
Use a consistent function to optionally strip out an empty root folde…
Browse files Browse the repository at this point in the history
…r for zip and tar downloads

fixes #57
  • Loading branch information
gabepaez committed Aug 20, 2014
1 parent 59dac4a commit abac0fb
Showing 1 changed file with 36 additions and 35 deletions.
71 changes: 36 additions & 35 deletions lib/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module.exports = {

stream.on('close', function() {
if(done.promise.isRejected()) return;
self.extractZip(stream.path, cachepath).then(function(files) {
self.extractZip(stream.path, cachepath).then(self.stripRootFolder).then(function(files) {
done.resolve(files);
});
});
Expand All @@ -92,7 +92,7 @@ module.exports = {
if (extention === '.gz') {
rq.on('response', function(res) {
if(res.statusCode !== 200) return;
self.extractTar(res, cachepath).then(function(files) {
self.extractTar(res, cachepath).then(self.stripRootFolder).then(function(files) {
done.resolve(files);
});
});
Expand All @@ -113,17 +113,12 @@ module.exports = {
.pipe(tar.extract(destination, {
umask: (isWin ? false : 0),
map: function(header) {
var filename = header.name.split('/');
header.name = (filename[1] ? filename[1] : filename[0]);
files.push({path: path.basename(header.name)});
return header;
},
ignore: function(name) {
files.push({path: path.basename(name)});
return false;
}
}))
.on('finish', function() {
done.resolve(files);
done.resolve({files:files, destination:destination});
});

return done.promise;
Expand All @@ -137,37 +132,12 @@ module.exports = {
done.reject(err);
})
.on('extract', function(log) {

// Setup chmodSync to fix permissions
files.forEach(function(file) {
fs.chmodSync(path.join(destination, file.path), file.mode);
});

var rootFiles = fs.readdirSync(destination);
var fromDir = path.join(destination, rootFiles.length === 1 ? rootFiles[0] : '');
// strip out root folder if it exists
if(rootFiles.length === 1 && fs.statSync(fromDir).isDirectory() ){
// strip folder from files
for (var i = 0; i < files.length; i++) {
var file = files[i];
file.path = path.relative(rootFiles[0], file.path);
if(file.path === '') {
console.log('kill it', i);
files.splice(i, 1);
i--;
}
}
// move stripped folder to destination
ncp(fromDir, destination, function (err) {
if (err) done.reject();
else rimraf(fromDir, function(){
done.resolve(files);
});
});
} else {
done.resolve(files);
}

done.resolve({files:files, destination:destination});
})
.extract({
path: destination,
Expand All @@ -181,6 +151,37 @@ module.exports = {
}
});

return done.promise;
},
stripRootFolder: function(extracted){
var done = Promise.defer(),
files = extracted.files,
destination = extracted.destination,
rootFiles = fs.readdirSync(destination),
fromDir = path.join(destination, rootFiles.length === 1 ? rootFiles[0] : '');

// strip out root folder if it exists
if(rootFiles.length === 1 && fs.statSync(fromDir).isDirectory() ){
// strip folder from files
for (var i = 0; i < files.length; i++) {
var file = files[i];
file.path = path.relative(rootFiles[0], file.path);
if(file.path === '') {
files.splice(i, 1);
i--;
}
}
// move stripped folder to destination
ncp(fromDir, destination, function (err) {
if (err) done.reject();
else rimraf(fromDir, function(){
done.resolve(files);
});
});
} else {
done.resolve(files);
}

return done.promise;
}
};

0 comments on commit abac0fb

Please sign in to comment.