Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get nested prompts result in yeoman generator

I am a javascript beginner and by following the writing your first generator tutorial and reading through other generators code I have managed to get this far with my first generator.

I have tried various ways to get this working and have failed miserably, I would be grateful if anyone could point me in the right direction. I should probably point out I have used generator-generator v0.4.3

oops forgot to say the problem is getting the response from the flavour list.

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

    var prompts = [{
      name: 'sitename',
      message: 'What would you like to name your app/site?',
      default: 'New Project'
    },{
      type: 'checkbox',
      name: 'features',
      message: 'Please choose additional features',
      choices:[{
        name: 'Modernizr',
        value: 'includeModernizr',
        checked: false
      },{
        name: 'Respond',
        value: 'includeRespond',
        checked: false
      }]
    },{
      type: 'list',
      name: 'flavour',
      message: 'Which type of css would you like to use?',
      choices: [{
        name: 'Sass',
        value: 'includeSass'
      },{
        name: 'Sass with Compass',
        value: 'includeCompass'
      },{
        name: 'Stylus',
        value: 'includeStylus'
      },{
        name: 'Standard css',
        value: 'includeCss'
      }]
      ,default: 1
    },{
      when: function (props) {
        return props.flavour.indexOf('includeSass') !== -1;
      },
      type: 'confirm',
      name: 'libsass',
      value: 'includeLibSass',
      message: 'Would you like to use libsass? Read about libsass at \n' + chalk.green('https://github.com/andrew/node-sass#nodesass'),
      default: false
    }];

    this.prompt(prompts, function (props) {
      var features = props.features;

      function hasFeature (feat) {
        return features.indexOf(feat) !== -1;
      }

      this.sitename = props.sitename;

      this.includeModernizr = hasFeature('includeModernizr');
      this.includeRespond = hasFeature('includeRespond');

      //this is obviously the wrong way to do it
      this.includeSass = props.includeSass;

      this.includeLibSass = props.libsass;
      this.includeRubySass = !props.libsass;

      done();
    }.bind(this));
  },
like image 708
Nijomi Avatar asked Dec 14 '25 01:12

Nijomi


1 Answers

Instead of using the hasFeature function, you can use _.includes from lodash. This is an easy way to extract choices.

this.includeModernizr = _.includes(props.features, 'includeModernizr');
like image 165
mike.bukosky Avatar answered Dec 16 '25 23:12

mike.bukosky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!