balena-cli/build/actions/update.js

52 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-03-04 18:03:08 +00:00
(function() {
var _, child_process, npm, packageJSON, president;
_ = require('lodash');
child_process = require('child_process');
2015-03-04 18:03:08 +00:00
president = require('president');
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) {
return npm.isUpdated(packageJSON.name, packageJSON.version, function(error, isUpdated) {
var command, onUpdate;
if (error != null) {
return done(error);
2015-03-04 18:03:08 +00:00
}
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) {
2015-04-20 16:42:31 +00:00
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-04 18:03:08 +00:00
}
};
}).call(this);