2015-03-04 18:03:08 +00:00
( function ( ) {
2015-04-01 16:46:41 +00:00
var _ , child _process , npm , packageJSON , president ;
_ = require ( 'lodash' ) ;
2015-03-24 12:44:02 +00:00
child _process = require ( 'child_process' ) ;
2015-03-04 18:03:08 +00:00
2015-04-01 16:46:41 +00:00
president = require ( 'president' ) ;
2015-03-11 12:49:26 +00:00
npm = require ( '../npm' ) ;
2015-03-04 18:03:08 +00:00
packageJSON = require ( '../../package.json' ) ;
exports . update = {
signature : 'update' ,
description : 'update the resin cli' ,
2015-03-04 18:47:46 +00:00
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' ,
2015-03-04 18:03:08 +00:00
action : function ( params , options , done ) {
2015-03-24 12:44:02 +00:00
return npm . isUpdated ( packageJSON . name , packageJSON . version , function ( error , isUpdated ) {
2015-04-01 16:46:41 +00:00
var command , onUpdate ;
2015-03-11 12:49:26 +00:00
if ( error != null ) {
return done ( error ) ;
2015-03-04 18:03:08 +00:00
}
2015-03-24 12:44:02 +00:00
if ( isUpdated ) {
return done ( new Error ( 'You\'re already running the latest version.' ) ) ;
}
2015-04-01 16:46:41 +00:00
onUpdate = function ( error , stdout , stderr ) {
2015-03-24 12:44:02 +00:00
if ( error != null ) {
return done ( error ) ;
}
if ( ! _ . isEmpty ( stderr ) ) {
return done ( new Error ( stderr ) ) ;
}
console . info ( "Upgraded " + packageJSON . name + "." ) ;
return done ( ) ;
2015-04-01 16:46:41 +00:00
} ;
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 ) ;
2015-03-24 12:44:02 +00:00
} ) ;
2015-03-11 12:49:26 +00:00
} ) ;
2015-03-04 18:03:08 +00:00
}
} ;
} ) . call ( this ) ;