Yeoman 递归提示,回调没有 运行

Yeoman recursive prompt, callback doesn't run

我是 Yeoman 的新手,但我正在尝试完成递归提示。受 jhipster 中的 EntityGenerator 启发 (https://github.com/jhipster/generator-jhipster/tree/master/generators/entity).

然而,似乎某处有错误阻止循环发生。

我的代码如下:

var yeoman = require('yeoman-generator');

var inputFields = [];

module.exports = yeoman.Base.extend({
  prompting: {
    askForName: askForName,
    askForFields: askForFields
  }
});

function askForData() {
  var prompts = [{
    type: 'input',
    name: 'name',
    message: 'Name?',
    default: 'Slim Shady'
  }];

  return this.prompt(prompts).then(function (props) {
    this.props = props;
    this.async();
  }.bind(this));
}

function askForFields() {
  var cb = this.async();
  askForField.call(this, cb);
}

function askForField(cb) {
  var prompts = [{
    type: 'confirm',
    name: 'fieldAdd',
    message: 'Do you want to add a field?',
    default: true
  }, {
    when: function (response) {
      return response.fieldAdd === true;
    },
    type: 'input',
    name: 'fieldName',
    message: 'What is the name of your field?'
  };

  this.prompt(prompts, function (props) {
    this.log("Done prompting: ", props);
    if (props.fieldAdd) {
      var field = {
        fieldName: props.fieldName,
      };
      inputFields.push(field);
    }
    if (props.fieldAdd) {
      askForField.call(that, cb);
    } else {
      cb();
    }
  }.bind(this));
}

递归提示的第一个循环按预期工作,但日志记录 this.log("Done prompting: ", props); 从未执行。就像提示的回调永远不会发生一样。生成器在一个 运行.

后退出

所有函数的this-s我都记录下来对比了,都是一样的。我看过很多类似的 Whosebug 问题,但我看不出有什么问题,只是它不起作用。

感谢任何帮助或提示!

您似乎在使用 yeoman-generator v0.23.0 及更高版本。

根据 yeoman-generator v0.23.0 (https://github.com/yeoman/generator/releases/tag/v0.23.0) 的发布:

Base#prompt() functions now returns a promise instead of taking a callback parameter. That'll make it easier to use inside asynchronous tasks.

prompting: function () {
   return this.prompt(questions).then(function (answers) {
    this.answers = answers;
  }.bind(this));
}

这是由于 Inquirer.js v1.0.0 (https://github.com/SBoudrias/Inquirer.js/releases/tag/v1.0.0) 的发布:

The whole inquirer API is now based on promises!

  • The base API interface is now inquirer.prompt(questions).then(). There's no more callback function.

  • Any async question functions is taking a promise as return value instead of requiring this.async().


解决方案

在你的 askForField() 函数中,你必须替换:

this.prompt(prompts, function (props) {
    this.log("Done prompting: ", props);
    if (props.fieldAdd) {
        var field = {
            fieldName: props.fieldName,
        };
        inputFields.push(field);
    }
    if (props.fieldAdd) {
        askForField.call(that, cb);
    } else {
        cb();
    }
}.bind(this));

作者:

return this.prompt(prompts).then(function (props) {
    this.log("Done prompting: ", props);
    if (props.fieldAdd) {
        var field = {
            fieldName: props.fieldName,
        };
        inputFields.push(field);
    }
    if (props.fieldAdd) {
        askForField.call(that, cb);
    } else {
        cb();
    }
}.bind(this));

正如您在 askForData() 函数中所做的那样:

return this.prompt(prompts).then(function (props) {
    this.props = props;
    this.async();
}.bind(this));