问题
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