Skip to content

Commit

Permalink
Validates latest version before using
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-lynch authored and stwiname committed Sep 14, 2015
1 parent 6230d91 commit d3c4634
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
31 changes: 26 additions & 5 deletions lib/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ var semver = require('semver');
var platforms = require('./platforms.js')
var request = require('request');
var _ = require('lodash');
var resolveUrl = require('url').resolve;

function getVersionList(url){
var done = Promise.defer(),
self = this,
scrapePtrn = /href="v?([0-9]+\.[0-9]+\.[0-9]+[^"]*)\/"/ig,
searchRes,
versions = [];
Expand All @@ -25,10 +25,31 @@ function getVersionList(url){
}
}
// order with newest version at front of array
versions = versions.sort(function(a,b){ return semver.compare(b,a); })
done.resolve(versions);
}
versions = versions.sort(function(a,b){ return semver.compare(b,a); });

// filter out invalid / alpha versions
var validationPromises = [];
versions.forEach(function(version){
validationPromises.push(new Promise(function(resolve, reject){
// check if windows 64-bit ZIP exists
request.head(resolveUrl(url, module.exports.getVersionNames(version).platforms.win64), function(err, res){
// note: this takes a version string and replaces it with an object (will be converted back later)
resolve({
version: version,
valid: !err && res.statusCode === 200
})
});
}));
});

Promise.all(validationPromises)
.then(function(versions){
// convert back to array of version strings (filtered)
versions = versions.filter(function(versionObj){ return versionObj.valid; })
.map(function(versionObj){ return versionObj.version; });
done.resolve(versions);
}, done.reject);
}
});

return done.promise;
Expand Down Expand Up @@ -62,4 +83,4 @@ module.exports = {

return versionNames;
}
};
};
6 changes: 6 additions & 0 deletions test/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ test('getLatestVersion', function (t) {
t.plan(1);

nock(dlUrl).get('/').replyWithFile(200, fixturesVersionsHtml);
nock(dlUrl).head('/v0.10.2/node-webkit-v0.10.2-win-x64.zip')
.replyWithFile(200, fixturesVersionsHtml); // needs to reply with *any* content
versions.getLatestVersion(dlUrl).then(function(result){
t.equal(result, expectedVersions[0]);
});
Expand All @@ -21,6 +23,10 @@ test('getVersions', function (t) {
t.plan(1);

nock(dlUrl).get('/').replyWithFile(200, fixturesVersionsHtml);
expectedVersions.forEach(function(expectedVersion){
nock(dlUrl).head('/v' + expectedVersion + '/node-webkit-v' + expectedVersion + '-win-x64.zip')
.replyWithFile(200, fixturesVersionsHtml); // needs to reply with *any* content
});
versions.getVersions(dlUrl).then(function(result){
t.deepEqual(result, expectedVersions);
});
Expand Down

0 comments on commit d3c4634

Please sign in to comment.