如何在 yeoman 生成器中正确执行条件提示

How to properly do a conditional prompt in yeoman generator

我想做的是仅当提示中的某个字段设置为 true 时才提出问题。

this.prompt(prompts, function (props) {
  this.isShared = props.isShared;
  this.componentName = props.componentName;
  // To access props later use this.props.someAnswer;
  if (this.isShared)
  {
    prompts = [{
      name: 'inPack',
      message: 'Yo dawg, in which pack is your component ? (you can just press enter if it\'s not in a pack)',
      default: ''
    }]
    this.prompt(prompts, function (props) {
      this.inPack = props.inPack;
    }.bind(this));
    done();
  }
  else
    done();
}.bind(this));

问题是 'writing' 函数是在 if 之前调用的(无论 if 是什么),所以我想知道如何正确执行它,因为我想这不是好方法

嗯,Yeoman 只是 JavaScript,所以如果您正确处理异步顺序,任何 if/else 语句都可以工作。

也就是说,如果您依赖问题的 when 属性,您将消除所有异步开销。参见 https://github.com/SBoudrias/Inquirer.js#question