Search code examples
javascriptnode.jsyeomanyeoman-generator

Pass data between prompts


Can i pass data between two prompts in yeoman?

Eg i've got two prompts like

{
  type: 'input',
  name: 'Name',
  message: 'Name?'
},{
  type: 'input',
  name: 'package',
  message: 'Package?',
  default: 'org.my.app.'+<prompt.name>
}

I want to show name property as default value for package? One way i can think of is:

  • Show a template in default (like in example)
  • Change the value later when creating the final template for user.

Another way that i tried is using when

{
  type: 'input',
  name: 'Name',
  message: 'Name?'
},{
  when: (response) => {
    this.testValue = response.Name
    return true
  },
  type: 'input',
  name: 'package',
  message: 'Package?',
  default: 'org.my.app.'+this.testValue
}

but it gives undefined even though inside the function value has been stored for in this.testValue

Is there any better way?


Solution

  • i finally found the answer. The way to achieve it is using two prompt variables and running the second one after first's promise returns

    const prompt1 = [{
      type: 'input',
      name: 'Name',
      message: 'Name?'
    }];
    
    return this.prompt(prompt1).then(props => {
      const prompt2 = [{
        type: 'input',
        name: 'package',
        message: 'Package?',
        default: 'org.my.app.'+props.name
      }];
    
      return this.prompt(prompt2).then(props => {
       //code goes here
      });
    });