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() {
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();

View File

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

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

View File

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

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"
\fBresin\fR \- tab completion for resin
.SH DESCRIPTION

View File

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

View File

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

View File

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