调用Yeoman子生成器不会提示

我想从主生成器中调用一些子生成器,并让它们使用提示来获取自己的信息.我当前的实现与调用的子生成器的提示步骤同时执行主生成器的编写步骤,但是我想执行以下步骤:

>主发电机提示步骤
>被调用子生成器的提示步骤
>主发电机写步骤
>编写被调用子生成器的步骤

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

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

这是我的副发电机

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

解决方法:

在真正完成该子生成器中的提示之前,您不应该调用done()函数.实际上,在将工作分派给子生成器之后,您就可以继续执行.取而代之的是,您应该仅异步调用done()(因为在大多数情况下,异步/完成是用例).

为此,我相信您可以将composeWith命令与.on链接起来,如下所示:

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

(按照base.js行358,在每个流程结束时都会发出“结束”事件)

上一篇:javascript – Yeoman:将grunt中的sass / compass支持添加到现有项目中


下一篇:android-科尔多瓦相机-离子