Skip to content

Commit

Permalink
[fixes #1265] don’t diverge the promise chain (we must wait for the u…
Browse files Browse the repository at this point in the history
…pdate to occur)

also some MISC refactoring
  • Loading branch information
stefanpenner committed Jul 4, 2014
1 parent 01809ff commit 0d537c0
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions lib/tasks/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ var chalk = require('chalk');
var Task = require('../models/task');
var fs = require('fs');

function npmInstall(npm) {
return Promise.denodeify(npm.commands.install)(['ember-cli']);
}

module.exports = Task.extend({
run: function(options, updateInfo) {
var env = options.environment || 'development';
process.env.EMBER_ENV = process.env.EMBER_ENV || env;

this.ui.write(chalk.yellow('\nA new version of ember-cli is available (' +
updateInfo.newestVersion + ').\n'));

Expand All @@ -22,26 +29,29 @@ module.exports = Task.extend({

return this.ui.prompt(updatePrompt).then(function(response) {
if (response.answer === true) {
this.runNpmUpdate(updateInfo.newestVersion);
return this.runNpmUpdate(updateInfo.newestVersion);
}
}.bind(this));
},
init: function() {
this.npm = this.npm || require('npm');
this.loadNPM = Promise.denodeify(this.npm.load);
},

runNpmUpdate: function(newestVersion) {
this.ui.pleasantProgress.start(chalk.green('Updating ember-cli'), chalk.green('.'));

this.loadNPM({
loglevel: 'silent',
global: true
})
.then(function(npm) {
return Promise.denodeify(npm.commands.install)(['ember-cli']);
}.bind(this))
.then(function() {
// first, run `npm install -g ember-cli`
var npm = require('npm');
var loadNPM = Promise.denodeify(npm.load);

var stopProgress (function() {
this.ui.pleasantProgress.stop();
}.bind(this));

var reportFailure = (function() {
this.ui.write('There was an error – possibly a permissions issue. You ' +
'may need to manually run ' +
chalk.green('npm install -g ember-cli') + '.\n');
}.bind(this));

var updateDependencies = (function() {
// update project's package.json file manually
var packageFilePath = this.project.root + '/package.json';

Expand All @@ -57,15 +67,16 @@ module.exports = Task.extend({
fs.writeFileSync(packageFilePath, JSON.stringify(jsonContent, null, 2));
this.ui.write(chalk.green('\n✓ ember-cli was successfully updated!\n'));
this.showEmberInitPrompt();
}.bind(this))
.catch(function() {
this.ui.write('There was an error – possibly a permissions issue. You ' +
'may need to manually run ' +
chalk.green('npm install -g ember-cli') + '.\n');
}.bind(this))
.finally(function() {
this.ui.pleasantProgress.stop();
}.bind(this));

return loadNPM({
loglevel: 'silent',
global: true
})
.then(npmInstall)
.then(updateDependencies)
.catch(reportFailure)
.finally(stopProgress);
},

showEmberInitPrompt: function() {
Expand Down Expand Up @@ -97,5 +108,4 @@ module.exports = Task.extend({
}
}.bind(this));
}

});

0 comments on commit 0d537c0

Please sign in to comment.