diff --git a/build/actions/update.js b/build/actions/update.js index fb589023..068dd8c3 100644 --- a/build/actions/update.js +++ b/build/actions/update.js @@ -26,10 +26,11 @@ }, function(data, lite, callback) { var newVersion; if (_.isEmpty(data)) { - return done(new Error('You are already running the latest version')); + return callback(new Error('You are already running the latest version')); } newVersion = _.last(_.first(_.last(data)).split('@')); - return console.info("Upgraded " + packageJSON.name + " to v" + newVersion + "."); + console.info("Upgraded " + packageJSON.name + " to v" + newVersion + "."); + return callback(); } ], done); } diff --git a/build/app.js b/build/app.js index c2ed822d..330ef582 100644 --- a/build/app.js +++ b/build/app.js @@ -1,5 +1,5 @@ (function() { - var _, actions, async, capitano, changeProjectDirectory, errors, plugins, resin; + var _, actions, async, capitano, changeProjectDirectory, errors, plugins, resin, update; _ = require('lodash'); @@ -15,6 +15,8 @@ plugins = require('./plugins'); + update = require('./update'); + capitano.permission('user', function(done) { return resin.auth.isLoggedIn(function(isLoggedIn) { if (!isLoggedIn) { @@ -145,6 +147,8 @@ async.waterfall([ function(callback) { + return update.check(callback); + }, function(callback) { return plugins.register('resin-plugin-', callback); }, function(callback) { var dataPrefix; diff --git a/build/update.js b/build/update.js new file mode 100644 index 00000000..c7df4b62 --- /dev/null +++ b/build/update.js @@ -0,0 +1,37 @@ +(function() { + var packageJSON, updateAction, updateNotifier; + + updateNotifier = require('update-notifier'); + + packageJSON = require('../package.json'); + + updateAction = require('./actions/update'); + + exports.perform = function(callback) { + return updateAction.update.action(null, null, callback); + }; + + exports.notify = function(update) { + if (!process.stdout.isTTY) { + return; + } + return console.log("> Major update available: " + update.current + " -> " + update.latest + "\n> Run resin update to update.\n> Beware that a major release might introduce breaking changes.\n"); + }; + + exports.check = function(callback) { + var notifier; + notifier = updateNotifier({ + pkg: packageJSON + }); + if (notifier.update == null) { + return callback(); + } + if (notifier.update.type === 'major') { + exports.notify(notifier.update); + return callback(); + } + console.log("Performing " + notifier.update.type + " update, hold tight..."); + return exports.perform(callback); + }; + +}).call(this); diff --git a/lib/actions/update.coffee b/lib/actions/update.coffee index 2eafbe95..fe9a78c7 100644 --- a/lib/actions/update.coffee +++ b/lib/actions/update.coffee @@ -35,9 +35,11 @@ exports.update = (data, lite, callback) -> if _.isEmpty(data) - return done(new Error('You are already running the latest version')) + return callback(new Error('You are already running the latest version')) newVersion = _.last(_.first(_.last(data)).split('@')) console.info("Upgraded #{packageJSON.name} to v#{newVersion}.") + return callback() + ], done) diff --git a/lib/app.coffee b/lib/app.coffee index 12bf90cd..c9e4b563 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -5,6 +5,7 @@ resin = require('resin-sdk') actions = require('./actions') errors = require('./errors') plugins = require('./plugins') +update = require('./update') capitano.permission 'user', (done) -> resin.auth.isLoggedIn (isLoggedIn) -> @@ -117,6 +118,9 @@ changeProjectDirectory = (directory) -> async.waterfall([ + (callback) -> + update.check(callback) + (callback) -> plugins.register('resin-plugin-', callback) diff --git a/lib/update.coffee b/lib/update.coffee new file mode 100644 index 00000000..c8f3cc6b --- /dev/null +++ b/lib/update.coffee @@ -0,0 +1,26 @@ +updateNotifier = require('update-notifier') +packageJSON = require('../package.json') +updateAction = require('./actions/update') + +exports.perform = (callback) -> + updateAction.update.action(null, null, callback) + +exports.notify = (update) -> + return if not process.stdout.isTTY + + console.log """ + > Major update available: #{update.current} -> #{update.latest} + > Run resin update to update. + > Beware that a major release might introduce breaking changes.\n + """ + +exports.check = (callback) -> + notifier = updateNotifier(pkg: packageJSON) + return callback() if not notifier.update? + + if notifier.update.type is 'major' + exports.notify(notifier.update) + return callback() + + console.log("Performing #{notifier.update.type} update, hold tight...") + exports.perform(callback) diff --git a/package.json b/package.json index 66b6fcd9..bd8bc1e5 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,7 @@ "progress-stream": "^0.5.0", "resin-cli-visuals": "resin-io/resin-cli-visuals", "resin-sdk": "^0.0.1", - "underscore.string": "~2.4.0" + "underscore.string": "~2.4.0", + "update-notifier": "^0.3.1" } }