Yeoman提示使用Rx接口

时间:2015-05-25 18:41:56

标签: yeoman

我正在尝试编写一个Yeoman生成器,而且我真的不喜欢用于编写提示的文档界面。 Reactive Interface似乎更容易编写分支和循环接口。但是,当我像这样写我的时候:

  prompting: function () {
    var prompts = [{ type: 'input',
        name: 'howdy',
        message:'howdy'
      }]; 
    prompts = Rx.Observable.from(prompts);
    this.prompt(prompts, function(answers) { this.log(answers); }.bind(this));

  },  

我收到此错误:

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: You must provide a `message` parameter
    at Prompt.throwParamError (/Users/user/.nvm/versions/node/v0.12.0/lib/node_modules/yo/node_modules/inquirer/lib/prompts/base.js:88:9)
    at Prompt (/Users/user/.nvm/versions/node/v0.12.0/lib/node_modules/yo/node_modules/inquirer/lib/prompts/base.js:44:10)
    at new Prompt (/Users/user/.nvm/versions/node/v0.12.0/lib/node_modules/yo/node_modules/inquirer/lib/prompts/input.js:25:15)
    at PromptUI.fetchAnswer (/Users/user/.nvm/versions/node/v0.12.0/lib/node_modules/yo/node_modules/inquirer/lib/ui/prompt.js:92:16)
    at MapObserver.selector (/Users/user/.nvm/versions/node/v0.12.0/lib/node_modules/yo/node_modules/inquirer/node_modules/rx/dist/rx.js:4215:20)
    at MapObserver.tryCatcher (/Users/user/.nvm/versions/node/v0.12.0/lib/node_modules/yo/node_modules/inquirer/node_modules/rx/dist/rx.js:568:29)
    at MapObserver.onNext (/Users/user/.nvm/versions/node/v0.12.0/lib/node_modules/yo/node_modules/inquirer/node_modules/rx/dist/rx.js:4423:42)
    at MapObserver.tryCatcher (/Users/user/.nvm/versions/node/v0.12.0/lib/node_modules/yo/node_modules/inquirer/node_modules/rx/dist/rx.js:568:29)
    at AutoDetachObserverPrototype.next (/Users/user/.nvm/versions/node/v0.12.0/lib/node_modules/yo/node_modules/inquirer/node_modules/rx/dist/rx.js:4856:51)
    at AutoDetachObserver.Rx.internals.AbstractObserver.AbstractObserver.onNext (/Users/user/.nvm/versions/node/v0.12.0/lib/node_modules/yo/node_modules/inquirer/node_modules/rx/dist/rx.js:1856:35)

1 个答案:

答案 0 :(得分:0)

我安装Inquirer并按照他们的示例,而不是通过this.prompt()使用Inquirer实例内置的生成器。它完美地运作;除了重复第一个提示。

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

    var log = function(answers) { this.log(answers); }.bind(this);
    var complete = function() {
      this.log('complete');
      done();
    }.bind(this);

    var prompts = Rx.Observable.create(function(obs) {
      this.log(obs);
      obs.onNext({ type: 'input',
        name: 'howdy',
        message:'howdy'
      }); 
      obs.onNext({ type: 'input',
        name: 'okee',
        message:'okee'
      }); 
      obs.onCompleted();
    }.bind(this));
    inquirer.prompt(prompts).process.subscribe(log, log, complete);
  }