Yeoman composeWith only calling constructor in subgenerator

若如初见. 提交于 2019-12-25 14:11:50

问题


I'm trying to use composeWith, to call a subgenerator within the same generator package. Yet for some reason, only the constructor of the called generator is invoked.

My generator-package generator-test looks like this:

package.json
generators
-- app
  -- index.js
-- base
  -- index.js

My main generator in app looks like this:

'use strict';

var yeoman = require('yeoman-generator');
var yosay = require('yosay');

module.exports = yeoman.generators.Base.extend({
    constructor: function () {
        console.log('constructing app');
        yeoman.generators.Base.apply(this, arguments);
    },
    initializing: function() {
        console.log('initializing app');
        var done = this.async();
        this.composeWith('test:base',{ options: { } })
            .on('end', function() {
                done();
            });
    }
});

My subgenerator in base:

'use strict';
var yeoman = require('yeoman-generator');

module.exports = yeoman.generators.Base.extend({
    constructor: function () {
        console.log('constructed base');
        yeoman.generators.Base.apply(this, arguments);
    },
    initializing: function() {
        console.log('initialized base');
    }
});

This should generate 4 console outputs, yet I am only getting the first three:

constructing app
initializing app
constructed base

initialized base is never displayed. Why? Am I misunderstanding the concept of composeWith?

Or is there another way to solve this?


回答1:


This doesn't work because you cannot wait for the end on a composed generator.

Calling this.async() is pausing the run loop until the callback is called. In this case, it's never going to be called because the sub generator won't start running until the callback is called (but the callback wait for the sub generator to run). Basically, you're dead locking your node process.

Composition in Yeoman must be independent. It is very important generators stay decoupled. To enforce this, yeoman-generator don't offer flow control for composition other than the run loop priorities.

You'll need to rethink your code and decouple those generators.



来源:https://stackoverflow.com/questions/30531160/yeoman-composewith-only-calling-constructor-in-subgenerator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!