From 4bfe52d73b766a7c5b9bcb91507070e1f6ad5b03 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 11 Mar 2015 08:49:26 -0400 Subject: [PATCH] Require CLI to be updated when running os install --- build/actions/os.js | 16 +++++++++- build/actions/update.js | 34 +++++---------------- build/npm.js | 63 +++++++++++++++++++++++++++++++++++++++ lib/actions/os.coffee | 20 +++++++++++++ lib/actions/update.coffee | 35 ++++------------------ lib/npm.coffee | 57 +++++++++++++++++++++++++++++++++++ 6 files changed, 168 insertions(+), 57 deletions(-) create mode 100644 build/npm.js create mode 100644 lib/npm.coffee diff --git a/build/actions/os.js b/build/actions/os.js index 7c655be8..535cef24 100644 --- a/build/actions/os.js +++ b/build/actions/os.js @@ -1,5 +1,5 @@ (function() { - var _, async, commandOptions, diskio, fs, mkdirp, os, path, progressStream, resin, visuals; + var _, async, commandOptions, diskio, fs, mkdirp, npm, os, packageJSON, path, progressStream, resin, updateActions, visuals; _ = require('lodash-contrib'); @@ -23,6 +23,12 @@ commandOptions = require('./command-options'); + npm = require('../npm'); + + packageJSON = require('../../package.json'); + + updateActions = require('./update'); + exports.download = { signature: 'os download ', description: 'download device OS', @@ -86,6 +92,14 @@ action: function(params, options, done) { return async.waterfall([ function(callback) { + return npm.isUpdated(packageJSON.name, packageJSON.version, callback); + }, function(isUpdated, callback) { + if (isUpdated) { + return callback(); + } + console.info('Resin CLI is outdated.\n\nIn order to avoid device compatibility issues, this command\nrequires that you have the Resin CLI updated.\n\nUpdating now...'); + return updateActions.update.action(params, options, _.unary(done)); + }, function(callback) { if (params.device != null) { return callback(null, params.device); } diff --git a/build/actions/update.js b/build/actions/update.js index f865130d..66b3b004 100644 --- a/build/actions/update.js +++ b/build/actions/update.js @@ -1,11 +1,7 @@ (function() { - var _, async, npm, packageJSON; + var npm, packageJSON; - async = require('async'); - - _ = require('lodash-contrib'); - - npm = require('npm'); + npm = require('../npm'); packageJSON = require('../../package.json'); @@ -14,27 +10,13 @@ description: 'update the resin cli', help: 'Use this command to update the Resin CLI\n\nThis command outputs information about the update process.\nUse `--quiet` to remove that output.\n\nThe Resin CLI checks for updates once per day.\n\nMajor updates require a manual update with this update command,\nwhile minor updates are applied automatically.\n\nExamples:\n\n $ resin update', action: function(params, options, done) { - return async.waterfall([ - function(callback) { - options = { - loglevel: 'silent', - global: true - }; - return npm.load(options, _.unary(callback)); - }, function(callback) { - return npm.commands.update([packageJSON.name], function(error, data) { - return callback(error, data); - }); - }, function(data, callback) { - var newVersion; - if (_.isEmpty(data)) { - 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(); + return npm.update(packageJSON.name, function(error, version) { + if (error != null) { + return done(error); } - ], done); + console.info("Upgraded " + packageJSON.name + " to v" + version + "."); + return done(null, version); + }); } }; diff --git a/build/npm.js b/build/npm.js new file mode 100644 index 00000000..a23f5361 --- /dev/null +++ b/build/npm.js @@ -0,0 +1,63 @@ +(function() { + var _, async, npm; + + npm = require('npm'); + + async = require('async'); + + _ = require('lodash-contrib'); + + exports.update = function(name, callback) { + return async.waterfall([ + function(callback) { + var options; + options = { + loglevel: 'silent', + global: true + }; + return npm.load(options, _.unary(callback)); + }, function(callback) { + return npm.commands.update([name], function(error, data) { + return callback(error, data); + }); + }, function(data, callback) { + var error, newVersion; + if (_.isEmpty(data)) { + error = new Error('You are already running the latest version'); + return callback(error); + } + newVersion = _.last(_.first(_.last(data)).split('@')); + return callback(null, newVersion); + } + ], callback); + }; + + exports.getLatestVersion = function(name, callback) { + return async.waterfall([ + function(callback) { + var options; + options = { + loglevel: 'silent', + global: true + }; + return npm.load(options, _.unary(callback)); + }, function(callback) { + return npm.commands.view([name], true, function(error, data) { + var versions; + versions = _.keys(data); + return callback(error, _.first(versions)); + }); + } + ], callback); + }; + + exports.isUpdated = function(name, currentVersion, callback) { + return exports.getLatestVersion(name, function(error, latestVersion) { + if (error != null) { + return callback(error); + } + return callback(null, currentVersion === latestVersion); + }); + }; + +}).call(this); diff --git a/lib/actions/os.coffee b/lib/actions/os.coffee index 6c94a0bf..db112d8d 100644 --- a/lib/actions/os.coffee +++ b/lib/actions/os.coffee @@ -9,6 +9,9 @@ visuals = require('resin-cli-visuals') progressStream = require('progress-stream') diskio = require('diskio') commandOptions = require('./command-options') +npm = require('../npm') +packageJSON = require('../../package.json') +updateActions = require('./update') exports.download = signature: 'os download ' @@ -118,6 +121,23 @@ exports.install = action: (params, options, done) -> async.waterfall [ + (callback) -> + npm.isUpdated(packageJSON.name, packageJSON.version, callback) + + (isUpdated, callback) -> + return callback() if isUpdated + + console.info ''' + Resin CLI is outdated. + + In order to avoid device compatibility issues, this command + requires that you have the Resin CLI updated. + + Updating now... + ''' + + updateActions.update.action(params, options, _.unary(done)) + (callback) -> return callback(null, params.device) if params.device? diff --git a/lib/actions/update.coffee b/lib/actions/update.coffee index 14845763..05f187db 100644 --- a/lib/actions/update.coffee +++ b/lib/actions/update.coffee @@ -1,6 +1,4 @@ -async = require('async') -_ = require('lodash-contrib') -npm = require('npm') +npm = require('../npm') packageJSON = require('../../package.json') exports.update = @@ -22,30 +20,7 @@ exports.update = $ resin update ''' action: (params, options, done) -> - async.waterfall([ - - (callback) -> - options = - - # TODO: There is no way to quiet npm install completely. - # Some output is still shown once the module is updated - # https://github.com/npm/npm/issues/2040 - loglevel: 'silent' - global: true - - npm.load(options, _.unary(callback)) - - (callback) -> - npm.commands.update [ packageJSON.name ], (error, data) -> - return callback(error, data) - - (data, callback) -> - if _.isEmpty(data) - 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) + npm.update packageJSON.name, (error, version) -> + return done(error) if error? + console.info("Upgraded #{packageJSON.name} to v#{version}.") + return done(null, version) diff --git a/lib/npm.coffee b/lib/npm.coffee new file mode 100644 index 00000000..4e7466c7 --- /dev/null +++ b/lib/npm.coffee @@ -0,0 +1,57 @@ +npm = require('npm') +async = require('async') +_ = require('lodash-contrib') + +exports.update = (name, callback) -> + async.waterfall([ + + (callback) -> + options = + + # TODO: There is no way to quiet npm install completely. + # Some output is still shown once the module is updated + # https://github.com/npm/npm/issues/2040 + loglevel: 'silent' + global: true + + npm.load(options, _.unary(callback)) + + (callback) -> + npm.commands.update [ name ], (error, data) -> + return callback(error, data) + + (data, callback) -> + if _.isEmpty(data) + error = new Error('You are already running the latest version') + return callback(error) + + newVersion = _.last(_.first(_.last(data)).split('@')) + return callback(null, newVersion) + + ], callback) + +exports.getLatestVersion = (name, callback) -> + async.waterfall([ + + (callback) -> + options = + + # TODO: There is no way to quiet npm install completely. + # Some output is still shown once the module is updated + # https://github.com/npm/npm/issues/2040 + loglevel: 'silent' + global: true + + npm.load(options, _.unary(callback)) + + (callback) -> + npm.commands.view [ name ], true, (error, data) -> + versions = _.keys(data) + return callback(error, _.first(versions)) + + ], callback) + +exports.isUpdated = (name, currentVersion, callback) -> + exports.getLatestVersion name, (error, latestVersion) -> + return callback(error) if error? + return callback(null, currentVersion is latestVersion)