简体   繁体   中英

How to properly do a conditional prompt in yeoman generator

What I'm trying to do is ask a question only if a field is set to true in the prompt.

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

the problem is that the 'writing' function is called before the if (whatever the if works), so I'd like to know how to do it properly because i guess this isn't the good way

Well Yeoman is only JavaScript so if you handle the asynchronous order correctly, any if/else statement would work.

That being said, you'd remove all that async overhead if you rely on the when property of a question. See https://github.com/SBoudrias/Inquirer.js#question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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