简体   繁体   English

Yeoman composeWith仅在子生成器中调用构造函数

[英]Yeoman composeWith only calling constructor in subgenerator

I'm trying to use composeWith , to call a subgenerator within the same generator package. 我正在尝试使用composeWith ,在同一生成器包中调用子生成器。 Yet for some reason, only the constructor of the called generator is invoked. 但是由于某种原因,只有被调用生成器的构造函数被调用。

My generator-package generator-test looks like this: 我的generator-package generator-test看起来像这样:

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

My main generator in app looks like this: 我在app主要生成器如下所示:

'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 : 我在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: 这应该生成4个控制台输出,但是我只得到前三个:

constructing app
initializing app
constructed base

initialized base is never displayed. initialized base永远不会显示。 Why? 为什么? Am I misunderstanding the concept of composeWith ? 我是否误解了composeWith的概念?

Or is there another way to solve this? 还是有解决此问题的另一种方法?

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. 调用this.async()会暂停运行循环,直到调用回调。 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. Yeoman的作品必须独立。 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. 为了实现这一点,除运行循环优先级外, yeoman-generator不提供合成的流控制。

You'll need to rethink your code and decouple those generators. 您需要重新考虑代码并解耦这些生成器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM