var Bluebird = require('bluebird');
var path = require('path');
var util = require('util');
var fs = require('fs');
var EventEmitter = require('eventemitter3');
var ssh = require('promised-ssh');
var config = require('./config');
var notify = require('./notify');
var errors = require('./errors');
var redisHandler = require('./redis');
var notifyError = notify.notifyError;
var notifySuccess = notify.notifySuccess;

function Deployment(project, options) {
  this.stdout = '';
  this.stderr = '';
  this.success = null;
  this.project = project;
  this.projectConfig = config.PROJECTS[project];
  this.options = options;
}
util.inherits(Deployment, EventEmitter);

Deployment.prototype.run = function run() {
  var onStdout = function onStdout(data) {
    this.stdout += data;
    this.emit('stdout', data);
  }.bind(this);

  var onStderr = function onStderr(data) {
    this.stderr += data;
    this.emit('stderr', data);
  }.bind(this);

  var tasks = [];
  this.projectConfig.tasks.deploy.forEach(function(task) {
    tasks.push('cd ' + this.projectConfig.path + ' && ' + task);
  }.bind(this));

  return ssh
    .connect({
      host: this.projectConfig.hostname,
      username: this.projectConfig.user,
      onStdout: onStdout,
      onStderr: onStderr,
      debug: true,
      privateKey: fs.readFileSync(config.PATH_TO_PRIVATE_KEY)
    })
    .bind(this)
    .then(function(connection) {
      return connection.exec(tasks);
    })
    .spread(function(code, stdout, stderr) {
      this.success = code === 0;
      this.emit('done');
    }).catch(function(error) {
      this.success = false;
      this.emit('done', error);
      throw error;
    })
    .finally(function() {
      this.report();
      this.notify();
    });
};

Deployment.prototype.notify = function() {
  if (this.success) {
    notifySuccess(this.project, this.options.source);
  } else {
    notifyError(this.project, this.options.source, this.stderr);
  }
};

Deployment.prototype.commit = function() {
  var match = this.stdout.match(/HEAD is now at ([a-z0-9]{7})/);
  if (match) {
    return match[1];
  }
};

Deployment.prototype.deploymentStatus = function() {
  return {
    project: this.project,
    commit: this.commit(),
    timestamp: Date.now(),
    success: this.success
  };
};

Deployment.prototype.report = function() {
  if (config.REDIS) {
    return redisHandler
      .reportDeployment(this.project, this.deploymentStatus())
      .return(true);
  }

  return Bluebird.resolve(false);
};

module.exports = Deployment;

@@ -90,24 +90,46 @@
 );%0Avar fs = 
+Bluebird.promisifyAll(
 require('fs'
@@ -129,16 +129,17 @@
 re('fs')
+)
 ;%0Avar Ev
@@ -1117,17 +1117,16 @@
 this));%0A
-%0A
   return
@@ -1130,16 +1130,116 @@
 urn 
-ssh%0A    
+fs%0A    .readFileAsync(config.PATH_TO_PRIVATE_KEY)%0A    .bind(this)%0A    .then(function(key) %7B%0A      return ssh
 .con
@@ -1251,16 +1251,18 @@
 %7B%0A      
+  
 host: th
@@ -1288,16 +1288,18 @@
 stname,%0A
+  
       us
@@ -1333,24 +1333,26 @@
 user,%0A      
+ 
+ 
 onStdout: on
@@ -1359,16 +1359,18 @@
 Stdout,%0A
+  
       on
@@ -1393,16 +1393,18 @@
 ,%0A      
+  
 debug: t
@@ -1414,16 +1414,18 @@
 ,%0A      
+ 
+ 
 privateK
@@ -1432,73 +1432,27 @@
 ey: 
-fs.readFileSync(config.PATH_TO_PRIVATE_KEY)%0A
+key%0A  
     %7D)
+;
 %0A    
-.bind(this
+%7D
 )%0A  

Read private key async