Require CLI to be updated when running os install

This commit is contained in:
Juan Cruz Viotti 2015-03-11 08:49:26 -04:00
parent 58f557065e
commit 4bfe52d73b
6 changed files with 168 additions and 57 deletions

View File

@ -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 <id>',
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);
}

View File

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

63
build/npm.js Normal file
View File

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

View File

@ -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 <id>'
@ -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?

View File

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

57
lib/npm.coffee Normal file
View File

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