diff --git a/build/actions/os.js b/build/actions/os.js index 59e9563f..6744fd6d 100644 --- a/build/actions/os.js +++ b/build/actions/os.js @@ -1,5 +1,5 @@ (function() { - var _, async, capitano, commandOptions, elevate, image, mkdirp, npm, os, packageJSON, path, resin, visuals; + var _, async, capitano, commandOptions, elevate, image, mkdirp, os, packageJSON, path, resin, selfupdate, visuals; capitano = require('capitano'); @@ -19,9 +19,9 @@ visuals = require('resin-cli-visuals'); - commandOptions = require('./command-options'); + selfupdate = require('selfupdate'); - npm = require('../npm'); + commandOptions = require('./command-options'); packageJSON = require('../../package.json'); @@ -103,7 +103,7 @@ action: function(params, options, done) { return async.waterfall([ function(callback) { - return npm.isUpdated(packageJSON.name, packageJSON.version, callback); + return selfupdate.isUpdated(packageJSON, callback); }, function(isUpdated, callback) { if (isUpdated) { return callback(); diff --git a/build/actions/update.js b/build/actions/update.js index 1685e24c..24335b00 100644 --- a/build/actions/update.js +++ b/build/actions/update.js @@ -1,13 +1,7 @@ (function() { - var _, child_process, npm, packageJSON, president; + var packageJSON, selfupdate; - _ = require('lodash'); - - child_process = require('child_process'); - - president = require('president'); - - npm = require('../npm'); + selfupdate = require('selfupdate'); packageJSON = require('../../package.json'); @@ -16,34 +10,12 @@ 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 npm.isUpdated(packageJSON.name, packageJSON.version, function(error, isUpdated) { - var command, onUpdate; + return selfupdate.update(packageJSON, function(error, version) { if (error != null) { return done(error); } - if (isUpdated) { - return done(new Error('You\'re already running the latest version.')); - } - onUpdate = function(error, stdout, stderr) { - if (error != null) { - return done(error); - } - if (!_.isEmpty(stderr)) { - return done(new Error(stderr)); - } - console.info("Upgraded " + packageJSON.name + "."); - return done(); - }; - command = "npm install --global " + packageJSON.name; - return child_process.exec(command, function(error, stdout, stderr) { - if (error == null) { - return onUpdate(null, stdout, stderr); - } - if (_.any([error.code === 3, error.code === 'EPERM', error.code === 'ACCES'])) { - return president.execute(command, onUpdate); - } - return done(error); - }); + console.info("Updated " + packageJSON.name + " to version " + version + "."); + return done(); }); } }; diff --git a/build/npm.js b/build/npm.js deleted file mode 100644 index 94b0ad9d..00000000 --- a/build/npm.js +++ /dev/null @@ -1,38 +0,0 @@ -(function() { - var _, async, npm; - - npm = require('npm'); - - async = require('async'); - - _ = require('lodash-contrib'); - - 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 9bcdb2fc..8e60f8a4 100644 --- a/lib/actions/os.coffee +++ b/lib/actions/os.coffee @@ -7,8 +7,8 @@ mkdirp = require('mkdirp') resin = require('resin-sdk') image = require('resin-image') visuals = require('resin-cli-visuals') +selfupdate = require('selfupdate') commandOptions = require('./command-options') -npm = require('../npm') packageJSON = require('../../package.json') elevate = require('../elevate') @@ -120,7 +120,7 @@ exports.install = async.waterfall [ (callback) -> - npm.isUpdated(packageJSON.name, packageJSON.version, callback) + selfupdate.isUpdated(packageJSON, callback) (isUpdated, callback) -> return callback() if isUpdated diff --git a/lib/actions/update.coffee b/lib/actions/update.coffee index 1ca7b5ee..a37d6946 100644 --- a/lib/actions/update.coffee +++ b/lib/actions/update.coffee @@ -1,7 +1,4 @@ -_ = require('lodash') -child_process = require('child_process') -president = require('president') -npm = require('../npm') +selfupdate = require('selfupdate') packageJSON = require('../../package.json') exports.update = @@ -23,36 +20,7 @@ exports.update = $ resin update ''' action: (params, options, done) -> - npm.isUpdated packageJSON.name, packageJSON.version, (error, isUpdated) -> + selfupdate.update packageJSON, (error, version) -> return done(error) if error? - - if isUpdated - return done(new Error('You\'re already running the latest version.')) - - onUpdate = (error, stdout, stderr) -> - return done(error) if error? - return done(new Error(stderr)) if not _.isEmpty(stderr) - console.info("Upgraded #{packageJSON.name}.") - return done() - - command = "npm install --global #{packageJSON.name}" - - # Attempting to self update using the NPM API was not considered safe. - # A safer thing to do is to call npm as a child process - # https://github.com/npm/npm/issues/7723 - child_process.exec command, (error, stdout, stderr) -> - return onUpdate(null, stdout, stderr) if not error? - - if _.any [ - - # 3 is equivalent to EACCES for NPM - error.code is 3 - - error.code is 'EPERM' - error.code is 'ACCES' - ] - return president.execute(command, onUpdate) - - return done(error) - - + console.info("Updated #{packageJSON.name} to version #{version}.") + return done() diff --git a/lib/npm.coffee b/lib/npm.coffee deleted file mode 100644 index 3a7d429b..00000000 --- a/lib/npm.coffee +++ /dev/null @@ -1,29 +0,0 @@ -npm = require('npm') -async = require('async') -_ = require('lodash-contrib') - -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) diff --git a/man/resin-completion.1 b/man/resin-completion.1 index 86cb5bb9..4b210238 100644 --- a/man/resin-completion.1 +++ b/man/resin-completion.1 @@ -1,4 +1,4 @@ -.TH "RESIN" "1" "March 2015" "" "" +.TH "RESIN" "1" "May 2015" "" "" .SH "NAME" \fBresin\fR \- tab completion for resin .SH DESCRIPTION diff --git a/man/resin-plugins.1 b/man/resin-plugins.1 index 9b00954c..f69f2860 100644 --- a/man/resin-plugins.1 +++ b/man/resin-plugins.1 @@ -1,4 +1,4 @@ -.TH "RESIN\-PLUGINS" "1" "March 2015" "" "" +.TH "RESIN\-PLUGINS" "1" "May 2015" "" "" .SH "NAME" \fBresin-plugins\fR \- Creating Resin CLI plugins .SH DESCRIPTION diff --git a/man/resin.1 b/man/resin.1 index 9ccaa64b..560d471f 100644 --- a/man/resin.1 +++ b/man/resin.1 @@ -1,4 +1,4 @@ -.TH "RESIN" "1" "March 2015" "" "" +.TH "RESIN" "1" "May 2015" "" "" .SH "NAME" \fBresin\fR \- command line tool to interact with resin\.io .SH SYNOPSIS diff --git a/package.json b/package.json index 6374f166..5908ab0e 100644 --- a/package.json +++ b/package.json @@ -59,12 +59,12 @@ "nplugm": "^2.2.0", "npm": "^2.6.1", "open": "0.0.5", - "president": "^1.0.0", "resin-cli-visuals": "^0.1.0", - "resin-sdk": "^1.4.3", "resin-image": "^1.1.2", + "resin-sdk": "^1.4.3", "resin-settings-client": "^1.0.0", "resin-vcs": "^1.2.0", + "selfupdate": "^1.1.0", "tmp": "^0.0.25", "underscore.string": "~2.4.0", "update-notifier": "^0.3.1"