我是一名JavaScript初学者,通过编写第一个生成器教程并通读其他生成器代码,我设法通过第一个生成器实现了这一目标。

我尝试了各种方法来使此工作正常进行,但不幸失败了,如果有人能指出正确的方向,我将不胜感激。我可能应该指出,我已经使用了generator-generator v0.4.3

哎呀,忘了说问题是从口味清单中获得响应。

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

最佳答案

可以使用lodash中的_.includes代替使用hasFeature函数。这是提取选项的简便方法。

this.includeModernizr = _.includes(props.features, 'includeModernizr');

关于yeoman - 如何获取嵌套提示导致Yeoman Generator,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23353361/

10-14 22:47