Yeoman Prompts with Rx Interface

心不动则不痛 提交于 2021-01-28 20:10:02

问题


I'm trying to write a Yeoman generator, and I really don't enjoy the documented interface for writing prompts. The Reactive Interface seems like it would be much easier to write branching and looping interfaces. However when I write mine like so:

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

  },  

I get this error:

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:


Instead of using the generator's built in instance of Inquirer via this.prompt(), I installed Inquirer and followed their example. It works perfectly; except it duplicates the first prompt.

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


来源:https://stackoverflow.com/questions/30444288/yeoman-prompts-with-rx-interface

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!