Yeoman composeWith only calling constructor in sub

2019-09-17 07:22发布

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条回答
放荡不羁爱自由
2楼-- · 2019-09-17 08:02

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.

查看更多
登录 后发表回答