简体   繁体   English

调用Yeoman子生成器不提示

[英]Invoked Yeoman sub generator doesn't prompt

I would like to invoke some subgenerators from my main generator and have them use a prompt to get their own information. 我想从我的主生成器调用一些子生成器,让他们使用提示来获取自己的信息。 My current implementation does the writing step of the main generator at the same time as the prompting step of the invoked subgenerator, but I would like to do the steps as following: 我当前的实现与调用的子生成器的提示步骤同时执行主生成器的写入步骤,但我想执行以下步骤:

  1. Prompt step of main generator 主发电机的提示步骤
  2. Prompt step of invoked subgenerators 调用子生成器的提示步骤
  3. Write step of main generator 写主发电机的步骤
  4. Write step of invoked subgenerators 写入调用的子生成器的步骤

My main generator looks like this: 我的主发电机看起来像这样:

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

module.exports = yeoman.generators.Base.extend({
  initializing: function () {
    this.pkg = require('../package.json');
  },

  prompting: function () {
    var done = this.async();

    // Have Yeoman greet the user.
    this.log(yosay(
      'Welcome to the neat ' + chalk.red('DockerSetup') + ' generator!'
    ));

    // Check for usage of redis, postgres and mysql subgenerators
    this.prompt([
    {
      type: 'input',
      name: 'subApplications',
      message: 'Enter the names of the sub-apps comma seperated'
    }], function (props) {
      this.subApplications = props.subApplications.length ? props.subApplications.split(',') : [];


    // Run subgenerators

    this.subApplications.forEach(function(name) {
      this.composeWith('docker-setup:sub-application', { args: [name] });
    }.bind(this));

      done();
    }.bind(this));
  },

  writing: function () {
    this.fs.copyTpl(
      this.templatePath('_Readme.md'),
      this.destinationPath('Readme.md')
    );
  }
});

And this is my sub-generator 这是我的子发电机

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

module.exports = yeoman.generators.NamedBase.extend({
  initializing: function () {
    this.log('You called the DockerSetup subgenerator with the argument ' + this.name + '.');
  },

  prompting: function () {
    // Assume that the sub-apps are one level under this with same name

    this.prompt([
    {
      type: 'list',
      name: 'mainTech',
      message: 'Which is the main technology used?',
      choices: ['rails', 'yii', 'frontend']
    }, {
      type: 'checkbox',
      name: 'additionalTechnologies',
      message: 'Which technologies are used in this subapp?',
      choices: ['redis', 'postgres', 'mysql']
    }], function (props) {
      this.mainTech = props.mainTech;
      this.additionalTechnologies = props.additionalTechnologies;


      // This is done here, because if it's in the writing part it gets called before the prompt
      var path = this.destinationPath('fig.yml'),
          file = this.readFileAsString(path),
          content;

      switch(this.mainTech) {
        case 'rails':
          content = 'content';
        break;

        case 'yii':
        break;

        case 'frontend':
        break;
      }

      this.additionalTechnologies.forEach(function (tech) {
        content += ('    - ' + tech);
      });

      file += content;
      this.write(path, file);

      done();
    }.bind(this));
  }
});

You shouldn't call the done() function until you're actually done with the prompting within that subgenerator. 在实际完成该子生成器中的提示之前,不应调用done()函数。 As it is, you're giving yo the go-ahead to continue execution right after dispatching the work to the subgenerator. 实际上,在将工作分配给子生成器之后,您可以立即继续执行。 Instead, you should call done() only asynchronously (as it is the use-case most of the time with async/done). 相反,您应该仅异步调用done() (因为它是大多数情况下使用async / done的用例)。

For this, I believe you can chain the composeWith command with .on as such: 为此,我相信您可以使用.on链接composeWith命令:

this.subApplications.forEach(function(name) {
      this.composeWith('docker-setup:sub-application', { args: [name] })
          .on('end',function(){
                        done();
                    });
    }.bind(this));

(The 'end' event is emitted at the end of every yo process, as per base.js line 358) (根据base.js第358行,在每个yo进程结束时发出'end'事件)

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

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