2014-04-01 165 views
0

我正在使用我的自定義yeoman生成器programmatical在我的一個nodejs模塊中。我寫了一個適配器來替換默認的TerminalAdapter。 的問題是,發送自定義事件

  • 當我使用的觸發方法emit自定義事件,我不能 聽我的模塊在該事件中。它沒有被解僱。
  • 即使end事件偵聽器也沒有被解僱。

請讓我知道我在這裏失蹤,

下面是我的模塊代碼,

'use strict'; 
var fs = require('fs'); 
var path = require('path'); 
var MyOwnAdapter = require('./MyOwnAdapter'); 
var adapt = new MyOwnAdapter(); 
var env = require('./generator-myframewrk/node_modules/yeoman-generator')(null, {}, adapt); 

env.register(path.resolve(__dirname, './generator-myframewrk'), 'myframewrk'); 
exports.run = function (options, answers) { 
    var obj = {}; 
    adapt.setAnswers(answers); 
    process.chdir(options.projdir); 

    env.on("end", function(){ 
     this.log("Even this is not getting called !!!"); //not coming here 
    }.bind(this)); 

    env.on("alldone", function(){ 
     this.log("Everything is done (including Bower install)"); //not coming here 
     obj.cb(); 
    }.bind(this)); 

    env.run('myframewrk', function(){ 
     this.log('ran yo myframewrk, But bower install might be pending'); //coming here 
    }); 

    return { 
     done: function (cb) { 
      obj.cb = cb; 
     } 
    }; 
}; 

下面是我的生成代碼,

var MyframewrkGenerator = yeoman.generators.Base.extend({ 
    init: function() { 
     this.pkg = require('../package.json'); 

     this.on('end', function() { 
      if (!this.options['skip-install']) { 
       this._installBower(); 
      } 
     }); 
    }, 

    _installBower: function() { 
     this.log("Running bower install..."); 
     /*reads bower.json and installs*/ 
     bower.commands.install([], {}, {directory : "./"}).on('error', function (error) { 
      this.log("BOWER error::"); 
      this.log(JSON.stringify(error)); 
     }.bind(this)).on('log', function (log) { 
      this.log("BOWER LOG::"); // coming here 
     }.bind(this)).on('end', function (installed) { 
      this.log("BOWER END::"); // coming here 
      this.emit("alldone"); // my custom event 
     }.bind(this)); 
    }, 

    askFor: function() { ... 

回答

0

我把_installBower方法出this.on('end', function() {});並使其成爲一個單獨的異步函數。這工作正常。我不再需要自定義事件了!

Thx ...

bowerInstallHelper: function(){ 
     if (!this.options['skip-install']) { 
      var cb = this.async(); 
      this._installBower(cb); 
     } 
    }