yeoman - 如何在 yeoman 生成器中获取嵌套提示结果

标签 yeoman yeoman-generator

我是一名 javascript 初学者,通过遵循编写第一个生成器教程并阅读其他生成器代码,我已经成功地使用我的第一个生成器实现了这一目标。

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

哎呀忘了说问题是从 flavor 列表中获取响应。

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

最佳答案

您可以使用 _.includes 而不是使用 hasFeature 函数来自洛达什。这是提取选择的简单方法。

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

关于yeoman - 如何在 yeoman 生成器中获取嵌套提示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23353361/

相关文章:

api - 为什么从本地主机向本地主机发出请求时会启动同源策略?

angularjs - 使用 Yeoman 的 Angular-Fullstack 在 postman 中发帖时出现 403

angularjs - bower-angular-bootstrap3 存储库移到了哪里?

linux - 我如何知道 `yo` 从哪里运行?如何指定它运行的位置?

javascript - 如何处理 Yeoman 的 package.json 冲突错误?

javascript - 使用 Yeoman 生成器重复提示

javascript - 从节点脚本调用时如何跳过 Yeoman 生成器步骤

javascript - 我如何告诉 Grunt 不要在构建任务中缩小或连接 js 文件?

yeoman - 在哪里更改应用程序的名称?

node.js - 如何控制spawnCommand方法的执行位置