Question

I am using my custom yeoman generator programmatical in one of my nodejs module. I have written an Adapter to replace default TerminalAdapter. The issues are,

  • When I am triggering custom event using emit method, I am not able to listen for that event in my module. It is not getting fired.
  • Even end event listener also not getting fired.

Please let me know what I am missing here,

Below is my module code,

'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;
        }
    };
};

Below is my Generator code,

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 () { ...
Était-ce utile?

La solution

I took _installBower method out of this.on('end', function () {}); and made it a separate async func. This works fine. I don't need custom event anymore!

Thx...

bowerInstallHelper: function(){
        if (!this.options['skip-install']) {
            var cb = this.async();
            this._installBower(cb);
        }
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top