inquirer.js 2.2 KB

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