inquirer.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Inquirer.js
  3. * A collection of common interactive command line user interfaces.
  4. */
  5. var inquirer = module.exports;
  6. /**
  7. * Client interfaces
  8. */
  9. inquirer.prompts = {};
  10. inquirer.Separator = require('./objects/separator');
  11. inquirer.ui = {
  12. BottomBar: require('./ui/bottom-bar'),
  13. Prompt: require('./ui/prompt')
  14. };
  15. /**
  16. * Create a new self-contained prompt module.
  17. */
  18. inquirer.createPromptModule = function (opt) {
  19. var promptModule = function (questions, allDone) {
  20. var ui = new inquirer.ui.Prompt(promptModule.prompts, opt);
  21. ui.run(questions, allDone);
  22. return ui;
  23. };
  24. promptModule.prompts = {};
  25. /**
  26. * Register a prompt type
  27. * @param {String} name Prompt type name
  28. * @param {Function} prompt Prompt constructor
  29. * @return {inquirer}
  30. */
  31. promptModule.registerPrompt = function (name, prompt) {
  32. promptModule.prompts[name] = prompt;
  33. return this;
  34. };
  35. /**
  36. * Register the defaults provider prompts
  37. */
  38. promptModule.restoreDefaultPrompts = function () {
  39. this.registerPrompt('list', require('./prompts/list'));
  40. this.registerPrompt('input', require('./prompts/input'));
  41. this.registerPrompt('confirm', require('./prompts/confirm'));
  42. this.registerPrompt('rawlist', require('./prompts/rawlist'));
  43. this.registerPrompt('expand', require('./prompts/expand'));
  44. this.registerPrompt('checkbox', require('./prompts/checkbox'));
  45. this.registerPrompt('password', require('./prompts/password'));
  46. };
  47. promptModule.restoreDefaultPrompts();
  48. return promptModule;
  49. };
  50. /**
  51. * Public CLI helper interface
  52. * @param {Array|Object|rx.Observable} questions - Questions settings array
  53. * @param {Function} cb - Callback being passed the user answers
  54. * @return {inquirer.ui.Prompt}
  55. */
  56. inquirer.prompt = inquirer.createPromptModule();
  57. // Expose helper functions on the top level for easiest usage by common users
  58. inquirer.registerPrompt = function (name, prompt) {
  59. inquirer.prompt.registerPrompt(name, prompt);
  60. };
  61. inquirer.restoreDefaultPrompts = function () {
  62. inquirer.prompt.restoreDefaultPrompts();
  63. };