Skip to content

Commit

Permalink
Merge pull request #455 from nwjs-community/develop
Browse files Browse the repository at this point in the history
3.4.1
  • Loading branch information
adam-lynch authored Jun 5, 2017
2 parents b8d91c1 + 9fa2016 commit ab700a5
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 201 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ cache
tmp/
test/temp/
.DS_Store
package-lock.json

example/build
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file and in the [
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [3.4.1] - 2017-06-05

### Removed

- The `bluebird` dependency. We're now using native promises instead.


## [3.4.0] - 2017-05-28

### Added
Expand Down
173 changes: 86 additions & 87 deletions lib/downloader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Promise = require('bluebird');
var request = require('request');
var progress = require('progress');
var fs = require('fs');
Expand All @@ -21,7 +20,7 @@ module.exports = {
checkCache: function(cachepath, files) {
var missing;

// if the version is >=0.12.3, then we don't know which files we want from the archives, so just check that the
// if the version is >=0.12.3, then we don't know which files we want from the archives, so just check that the
// folder exists and has at least 3 files in it.
if(files.length === 1 && files[0] === '*'){
return fs.existsSync(cachepath) && fs.readdirSync(cachepath).length >= 2;
Expand All @@ -44,7 +43,6 @@ module.exports = {
},
downloadAndUnpack: function(cachepath, url) {
var extention = path.extname(url),
done = Promise.defer(),
self = this,
rq = request(url),
len,
Expand All @@ -54,85 +52,83 @@ module.exports = {
return statusCode + ': ' + require('http').STATUS_CODES[statusCode];
}

rq.proxy = true;
rq.on('error', function(err) {
bar && bar.terminate();
done.reject(err);
});
rq.on('response', function (res) {
len = parseInt(res.headers['content-length'], 10);
if (res.statusCode !== 200) {
done.reject({
statusCode: res.statusCode,
msg: 'Recieved status code ' + format(res.statusCode)
});
} else if (len) {
if (!bar) {
bar = new progress(' downloading [:bar] :percent :etas', {
complete: '=',
incomplete: '-',
width: 20,
total: len
return new Promise(function(resolve, reject){
rq.proxy = true;
rq.on('error', function(err) {
bar && bar.terminate();
reject(err);
});
rq.on('response', function (res) {
len = parseInt(res.headers['content-length'], 10);
if (res.statusCode !== 200) {
reject({
statusCode: res.statusCode,
msg: 'Recieved status code ' + format(res.statusCode)
});
} else {
bar.total += len;
} else if (len) {
if (!bar) {
bar = new progress(' downloading [:bar] :percent :etas', {
complete: '=',
incomplete: '-',
width: 20,
total: len
});
} else {
bar.total += len;
}
}
}
});
rq.on('data', function(chunk) {
len && bar && bar.tick(chunk.length);
});
});
rq.on('data', function(chunk) {
len && bar && bar.tick(chunk.length);
});

if (extention === '.zip') {
stream = temp.createWriteStream();
if (extention === '.zip') {
stream = temp.createWriteStream();

stream.on('close', function() {
if(done.promise.isRejected()) return;
self.extractZip(stream.path, cachepath).then(self.stripRootFolder).then(function(files) {
done.resolve(files);
stream.on('finish', function() {
self.extractZip(stream.path, cachepath).then(self.stripRootFolder).then(function(files) {
resolve(files);
});
});
});

rq.pipe(stream);
}
rq.pipe(stream);
}

if (extention === '.gz') {
rq.on('response', function(res) {
if(res.statusCode !== 200) return;
self.extractTar(res, cachepath).then(self.stripRootFolder).then(function(files) {
done.resolve(files);
else if (extention === '.gz') {
rq.on('response', function(res) {
if(res.statusCode !== 200) return;
self.extractTar(res, cachepath).then(self.stripRootFolder).then(function(files) {
resolve(files);
});
});
});
}

return done.promise;
}
});
},
extractTar: function(tarstream, destination) {
var done = Promise.defer(),
gunzip = zlib.createGunzip(),
var gunzip = zlib.createGunzip(),
files = [];

tarstream
.pipe(gunzip)
.on('error', function(err){
done.reject(err);
})
.pipe(tar.extract(destination, {
umask: (isWin ? false : 0),
map: function(header) {
files.push({path: path.basename(header.name)});
return header;
}
}))
.on('finish', function() {
done.resolve({files:files, destination:destination});
});
return new Promise(function(resolve, reject){
tarstream
.pipe(gunzip)
.on('error', function(err){
reject(err);
})
.pipe(tar.extract(destination, {
umask: (isWin ? false : 0),
map: function(header) {
files.push({path: path.basename(header.name)});
return header;
}
}))
.on('finish', function() {
resolve({files:files, destination:destination});
});

return done.promise;
});
},
extractZip: function(zipfile, destination) {
var files = [],
done = Promise.defer();
var files = [];

var onEntry = function(entry){
files.push({
Expand All @@ -141,24 +137,24 @@ module.exports = {
});
};

extract(zipfile, { dir: destination, onEntry: onEntry }, function(err){
if(err){
return done.reject(err);
}
return new Promise(function(resolve, reject){
extract(zipfile, { dir: destination, onEntry: onEntry }, function(err){
if(err){
return reject(err);
}

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

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

done.resolve({ files: files, destination: destination });
});

return done.promise;
},
stripRootFolder: function(extracted){
var done = Promise.defer(),
files = extracted.files,
var files = extracted.files,
destination = extracted.destination,
rootFiles = fs.readdirSync(destination),
fromDir = path.join(destination, rootFiles.length === 1 ? rootFiles[0] : '');
Expand All @@ -174,17 +170,20 @@ module.exports = {
i--;
}
}
// move stripped folder to destination
ncp(fromDir, destination, function (err) {
if (err) done.reject();
else rimraf(fromDir, function(){
done.resolve(files);

return new Promise(function(resolve, reject){
// move stripped folder to destination
ncp(fromDir, destination, function (err) {
if (err) {
return reject();
}
else rimraf(fromDir, function(){
resolve(files);
});
});
});
} else {
done.resolve(files);
return Promise.resolve(files);
}

return done.promise;
}
};
Loading

0 comments on commit ab700a5

Please sign in to comment.