2013-09-29 60 views
3

爲了在grunt插件上運行ssh,使用ssh2,我創建了一個任務,但在connect()稍後發生on事件之後,進程將被終止。什麼可能導致這個?grunt.js任務爲什麼不事件監聽?

在正常程序中沒有問題,您不想使用grunt.js。 。 。

Gruntfile

ssh2sample: { 
     default_options: { 
     options: { 
     'host': 'xxx.xxx.xxx.xxx', 
     'port': 22, 
     'username': 'names', 
     'privateKey': 'keys', 
     'passphrase': 'pass' 
    }, 

任務/ ssh2sample.js

'use strict'; 

module.exports = function(grunt) { 

// Please see the Grunt documentation for more information regarding task 
// creation: http://gruntjs.com/creating-tasks 

grunt.registerMultiTask('ssh2sample', 'The best Grunt plugin ever.', function() { 

// Merge task-specific and/or target-specific options with these defaults. 

    var Connection = require('ssh2'); 


    var options = this.options(); 
    options.privateKey = require('fs').readFileSync(options.privateKey); 

    var c = new Connection(); 

    c.on('connect', function() { 
     console.log('Connection :: connect'); 
    }); 

    c.on('ready', function() { 
     console.log('Connection :: ready'); 
     c.exec('uptime', function(err, stream) { 
      //if (err) throw err; 
      stream.on('data', function(data, extended) { 
       console.log(extended === 'stderr' ? 'STDERR: ' : 'STDOUT: '+ data); 
      }); 
      stream.on('end', function() { 
       console.log('Stream :: EOF'); 
      }); 
      stream.on('close', function() { 
       console.log('Stream :: close'); 
      }); 
      stream.on('exit', function(code, signal) { 
       console.log('Stream :: exit :: code: ' + code + ', signal: ' + signal); 
       c.end(); 
      }); 
     }); 
    }); 
    c.on('error', function(err) { 
     console.log('Connection :: error :: ' + err); 
    }); 
    c.on('end', function() { 
     console.log('Connection :: end'); 
    }); 
    c.on('close', function(had_error) { 
     console.log('Connection :: close'); 
    }); 

    c.connect(options); 
} 
}); 

沒有問題ssh2connect

var Connection = require('ssh2'); 


    var options = this.options(); 
    options.privateKey = require('fs').readFileSync(options.privateKey); 

    var c = new Connection(); 

    c.on('connect', function() { 
     console.log('Connection :: connect'); 
    }); 

    c.on('ready', function() { 
     console.log('Connection :: ready'); 
     c.exec('uptime', function(err, stream) { 
      //if (err) throw err; 
      stream.on('data', function(data, extended) { 
       console.log(extended === 'stderr' ? 'STDERR: ' : 'STDOUT: '+ data); 
      }); 
      stream.on('end', function() { 
       console.log('Stream :: EOF'); 
      }); 
      stream.on('close', function() { 
       console.log('Stream :: close'); 
      }); 
      stream.on('exit', function(code, signal) { 
       console.log('Stream :: exit :: code: ' + code + ', signal: ' + signal); 
       c.end(); 
      }); 
     }); 
    }); 
    c.on('error', function(err) { 
     console.log('Connection :: error :: ' + err); 
    }); 
    c.on('end', function() { 
     console.log('Connection :: end'); 
    }); 
    c.on('close', function(had_error) { 
     console.log('Connection :: close'); 
    }); 

    var key = require(fs).readdirSync(keys); 

    c.connect({ 
     'host': 'xxx.xxx.xxx.xxx', 
     'port': 22, 
     'username': 'names', 
     'privateKey': key, 
     'passphrase': 'pass' 
    }); 

回答