Yeoman composeWith 只在子生成器中调用构造函数 [英] Yeoman composeWith only calling constructor in subgenerator

查看:41
本文介绍了Yeoman composeWith 只在子生成器中调用构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 composeWith,在同一个生成器包中调用子生成器.然而由于某种原因,只调用了被调用生成器的构造函数.

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.

我的生成器包 generator-test 看起来像这样:

My generator-package generator-test looks like this:

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

我在 app 中的主要生成器如下所示:

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();
            });
    }
});

我在 base 中的子生成器:

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

这应该会生成 4 个控制台输出,但我只得到前三个:

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

constructing app
initializing app
constructed base

initialized base 永远不会显示.为什么?我误解了 composeWith 的概念吗?

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

或者有其他方法可以解决这个问题吗?

Or is there another way to solve this?

推荐答案

这不起作用,因为您不能等待组合生成器的结束.

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

调用 this.async() 会暂停运行循环,直到调用回调.在这种情况下,它永远不会被调用,因为在调用回调之前子生成器不会开始运行(但回调等待子生成器运行).基本上,你已经死锁了你的节点进程.

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.

Yeoman 中的组合必须是独立的.发电机保持解耦非常重要.为了强制执行此操作,yeoman-generator 除了运行循环优先级外,不为组合提供流量控制.

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.

这篇关于Yeoman composeWith 只在子生成器中调用构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆