Make use of selfupdate

This commit is contained in:
Juan Cruz Viotti 2015-05-13 15:37:27 -03:00
parent 80e69c56d0
commit e7e8ec296c
10 changed files with 20 additions and 147 deletions

View File

@ -1,5 +1,5 @@
(function() { (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'); capitano = require('capitano');
@ -19,9 +19,9 @@
visuals = require('resin-cli-visuals'); visuals = require('resin-cli-visuals');
commandOptions = require('./command-options'); selfupdate = require('selfupdate');
npm = require('../npm'); commandOptions = require('./command-options');
packageJSON = require('../../package.json'); packageJSON = require('../../package.json');
@ -103,7 +103,7 @@
action: function(params, options, done) { action: function(params, options, done) {
return async.waterfall([ return async.waterfall([
function(callback) { function(callback) {
return npm.isUpdated(packageJSON.name, packageJSON.version, callback); return selfupdate.isUpdated(packageJSON, callback);
}, function(isUpdated, callback) { }, function(isUpdated, callback) {
if (isUpdated) { if (isUpdated) {
return callback(); return callback();

View File

@ -1,13 +1,7 @@
(function() { (function() {
var _, child_process, npm, packageJSON, president; var packageJSON, selfupdate;
_ = require('lodash'); selfupdate = require('selfupdate');
child_process = require('child_process');
president = require('president');
npm = require('../npm');
packageJSON = require('../../package.json'); packageJSON = require('../../package.json');
@ -16,34 +10,12 @@
description: 'update the resin cli', 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', 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) { action: function(params, options, done) {
return npm.isUpdated(packageJSON.name, packageJSON.version, function(error, isUpdated) { return selfupdate.update(packageJSON, function(error, version) {
var command, onUpdate;
if (error != null) { if (error != null) {
return done(error); return done(error);
} }
if (isUpdated) { console.info("Updated " + packageJSON.name + " to version " + version + ".");
return done(new Error('You\'re already running the latest version.')); return done();
}
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);
});
}); });
} }
}; };

View File

@ -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);

View File

@ -7,8 +7,8 @@ mkdirp = require('mkdirp')
resin = require('resin-sdk') resin = require('resin-sdk')
image = require('resin-image') image = require('resin-image')
visuals = require('resin-cli-visuals') visuals = require('resin-cli-visuals')
selfupdate = require('selfupdate')
commandOptions = require('./command-options') commandOptions = require('./command-options')
npm = require('../npm')
packageJSON = require('../../package.json') packageJSON = require('../../package.json')
elevate = require('../elevate') elevate = require('../elevate')
@ -120,7 +120,7 @@ exports.install =
async.waterfall [ async.waterfall [
(callback) -> (callback) ->
npm.isUpdated(packageJSON.name, packageJSON.version, callback) selfupdate.isUpdated(packageJSON, callback)
(isUpdated, callback) -> (isUpdated, callback) ->
return callback() if isUpdated return callback() if isUpdated

View File

@ -1,7 +1,4 @@
_ = require('lodash') selfupdate = require('selfupdate')
child_process = require('child_process')
president = require('president')
npm = require('../npm')
packageJSON = require('../../package.json') packageJSON = require('../../package.json')
exports.update = exports.update =
@ -23,36 +20,7 @@ exports.update =
$ resin update $ resin update
''' '''
action: (params, options, done) -> action: (params, options, done) ->
npm.isUpdated packageJSON.name, packageJSON.version, (error, isUpdated) -> selfupdate.update packageJSON, (error, version) ->
return done(error) if error? return done(error) if error?
console.info("Updated #{packageJSON.name} to version #{version}.")
if isUpdated return done()
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)

View File

@ -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)

View File

@ -1,4 +1,4 @@
.TH "RESIN" "1" "March 2015" "" "" .TH "RESIN" "1" "May 2015" "" ""
.SH "NAME" .SH "NAME"
\fBresin\fR \- tab completion for resin \fBresin\fR \- tab completion for resin
.SH DESCRIPTION .SH DESCRIPTION

View File

@ -1,4 +1,4 @@
.TH "RESIN\-PLUGINS" "1" "March 2015" "" "" .TH "RESIN\-PLUGINS" "1" "May 2015" "" ""
.SH "NAME" .SH "NAME"
\fBresin-plugins\fR \- Creating Resin CLI plugins \fBresin-plugins\fR \- Creating Resin CLI plugins
.SH DESCRIPTION .SH DESCRIPTION

View File

@ -1,4 +1,4 @@
.TH "RESIN" "1" "March 2015" "" "" .TH "RESIN" "1" "May 2015" "" ""
.SH "NAME" .SH "NAME"
\fBresin\fR \- command line tool to interact with resin\.io \fBresin\fR \- command line tool to interact with resin\.io
.SH SYNOPSIS .SH SYNOPSIS

View File

@ -59,12 +59,12 @@
"nplugm": "^2.2.0", "nplugm": "^2.2.0",
"npm": "^2.6.1", "npm": "^2.6.1",
"open": "0.0.5", "open": "0.0.5",
"president": "^1.0.0",
"resin-cli-visuals": "^0.1.0", "resin-cli-visuals": "^0.1.0",
"resin-sdk": "^1.4.3",
"resin-image": "^1.1.2", "resin-image": "^1.1.2",
"resin-sdk": "^1.4.3",
"resin-settings-client": "^1.0.0", "resin-settings-client": "^1.0.0",
"resin-vcs": "^1.2.0", "resin-vcs": "^1.2.0",
"selfupdate": "^1.1.0",
"tmp": "^0.0.25", "tmp": "^0.0.25",
"underscore.string": "~2.4.0", "underscore.string": "~2.4.0",
"update-notifier": "^0.3.1" "update-notifier": "^0.3.1"