From e189205f4a3cc4f1e6219c3cd66c2bcb3ded5d61 Mon Sep 17 00:00:00 2001 From: Bradley Meck Date: Thu, 11 Aug 2011 11:26:34 -0500 Subject: [PATCH] [fix] #70 request for version should be performed async and aborted if jitsu finishes before the request finishes --- lib/jitsu.js | 16 +++++++++++++++- lib/jitsu/utils/index.js | 27 +++++++++------------------ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/jitsu.js b/lib/jitsu.js index 9f7ebda..6d65f3e 100644 --- a/lib/jitsu.js +++ b/lib/jitsu.js @@ -87,7 +87,21 @@ jitsu.start = function (argv, callback) { winston.info('Welcome to ' + 'Nodejitsu'.grey); winston.info('It worked if it ends with ' + 'Nodejitsu'.grey + ' ok'.green.bold); - jitsu.utils.checkVersion(function () { + jitsu.utils.checkVersion(function (handler) { + var oldCallback = callback; + callback = function() { + if(handler.req) { + var timer = setTimeout(function() { + if(handler.req) { + handler.req.abort(); + } + handler.on('end', function() { + clearTimeout(timer); + }) + }, 200); + } + oldCallback.call(this, arguments); + } jitsu.config.load(function (err) { if (err) { callback(err); diff --git a/lib/jitsu/utils/index.js b/lib/jitsu/utils/index.js index 3bbc1b5..cb8fce6 100644 --- a/lib/jitsu/utils/index.js +++ b/lib/jitsu/utils/index.js @@ -143,22 +143,11 @@ utils.objectDiff = function (current, update, level) { utils.checkVersion = function (callback) { var responded = false - // - // Only allow the `checkVersion` function 200ms - // to attempt to contact GitHub. - // - setTimeout(function () { - if (!responded) { - responded = true; - callback(); - } - }, 200); - // // Check the GitHub tags for `jitsu` to see if the current - // version is outdated. + // version is outdated. If it is not make sure to message the user at the end. // - request({ + var handler = request({ uri: 'http://registry.npmjs.org/jitsu/latest' }, function (err, res, body) { if (!responded) { @@ -168,8 +157,10 @@ utils.checkVersion = function (callback) { var pkg = JSON.parse(body); if (semver.gt(pkg.version, jitsu.version)) { - winston.warn('A newer version of jitsu is available: ' + pkg.version); - winston.warn('Please run `npm update jitsu`'); + process.on('exit', function() { + winston.warn('A newer version of jitsu is available: ' + pkg.version); + winston.warn('Please run `npm update jitsu`'); + }); } } catch (ex) { @@ -178,8 +169,8 @@ utils.checkVersion = function (callback) { // of an upgrade at the next possible opportunity. // } - - callback(); } }); -}; \ No newline at end of file + + callback(handler); +};