inquirer.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, answers) {
  21. var ui;
  22. try {
  23. ui = new inquirer.ui.Prompt(promptModule.prompts, opt);
  24. } catch (error) {
  25. return Promise.reject(error);
  26. }
  27. var promise = ui.run(questions, answers);
  28. // Monkey patch the UI on the promise object so
  29. // that it remains publicly accessible.
  30. promise.ui = ui;
  31. return promise;
  32. };
  33. promptModule.prompts = {};
  34. /**
  35. * Register a prompt type
  36. * @param {String} name Prompt type name
  37. * @param {Function} prompt Prompt constructor
  38. * @return {inquirer}
  39. */
  40. promptModule.registerPrompt = function (name, prompt) {
  41. promptModule.prompts[name] = prompt;
  42. return this;
  43. };
  44. /**
  45. * Register the defaults provider prompts
  46. */
  47. promptModule.restoreDefaultPrompts = function () {
  48. this.registerPrompt('list', require('./prompts/list'));
  49. this.registerPrompt('input', require('./prompts/input'));
  50. this.registerPrompt('number', require('./prompts/number'));
  51. this.registerPrompt('confirm', require('./prompts/confirm'));
  52. this.registerPrompt('rawlist', require('./prompts/rawlist'));
  53. this.registerPrompt('expand', require('./prompts/expand'));
  54. this.registerPrompt('checkbox', require('./prompts/checkbox'));
  55. this.registerPrompt('password', require('./prompts/password'));
  56. this.registerPrompt('editor', require('./prompts/editor'));
  57. };
  58. promptModule.restoreDefaultPrompts();
  59. return promptModule;
  60. };
  61. /**
  62. * Public CLI helper interface
  63. * @param {Array|Object|Rx.Observable} questions - Questions settings array
  64. * @param {Function} cb - Callback being passed the user answers
  65. * @return {inquirer.ui.Prompt}
  66. */
  67. inquirer.prompt = inquirer.createPromptModule();
  68. // Expose helper functions on the top level for easiest usage by common users
  69. inquirer.registerPrompt = function (name, prompt) {
  70. inquirer.prompt.registerPrompt(name, prompt);
  71. };
  72. inquirer.restoreDefaultPrompts = function () {
  73. inquirer.prompt.restoreDefaultPrompts();
  74. };