inquirer.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Inquirer.js
  3. * A collection of common interactive command line user interfaces.
  4. */
  5. import { default as List } from './prompts/list.js';
  6. import { default as Input } from './prompts/input.js';
  7. import { default as Number } from './prompts/number.js';
  8. import { default as Confirm } from './prompts/confirm.js';
  9. import { default as RawList } from './prompts/rawlist.js';
  10. import { default as Expand } from './prompts/expand.js';
  11. import { default as Checkbox } from './prompts/checkbox.js';
  12. import { default as Password } from './prompts/password.js';
  13. import { default as Editor } from './prompts/editor.js';
  14. import { default as BottomBar } from './ui/bottom-bar.js';
  15. import { default as Prompt } from './ui/prompt.js';
  16. import { default as Separator } from './objects/separator.js';
  17. /**
  18. * Create a new self-contained prompt module.
  19. */
  20. export function createPromptModule(opt) {
  21. const promptModule = function (questions, answers) {
  22. let uiInstance;
  23. try {
  24. uiInstance = new Prompt(promptModule.prompts, opt);
  25. } catch (error) {
  26. return Promise.reject(error);
  27. }
  28. const promise = uiInstance.run(questions, answers);
  29. // Monkey patch the UI on the promise object so
  30. // that it remains publicly accessible.
  31. promise.ui = uiInstance;
  32. return promise;
  33. };
  34. promptModule.prompts = {};
  35. /**
  36. * Register a prompt type
  37. * @param {String} name Prompt type name
  38. * @param {Function} prompt Prompt constructor
  39. * @return {inquirer}
  40. */
  41. promptModule.registerPrompt = function (name, prompt) {
  42. promptModule.prompts[name] = prompt;
  43. return this;
  44. };
  45. /**
  46. * Register the defaults provider prompts
  47. */
  48. promptModule.restoreDefaultPrompts = function () {
  49. this.registerPrompt('list', List);
  50. this.registerPrompt('input', Input);
  51. this.registerPrompt('number', Number);
  52. this.registerPrompt('confirm', Confirm);
  53. this.registerPrompt('rawlist', RawList);
  54. this.registerPrompt('expand', Expand);
  55. this.registerPrompt('checkbox', Checkbox);
  56. this.registerPrompt('password', Password);
  57. this.registerPrompt('editor', Editor);
  58. };
  59. promptModule.restoreDefaultPrompts();
  60. return promptModule;
  61. }
  62. /**
  63. * Public CLI helper interface
  64. * @param {Array|Object|Rx.Observable} questions - Questions settings array
  65. * @param {Function} cb - Callback being passed the user answers
  66. * @return {ui.Prompt}
  67. */
  68. const prompt = createPromptModule();
  69. // Expose helper functions on the top level for easiest usage by common users
  70. function registerPrompt(name, newPrompt) {
  71. prompt.registerPrompt(name, newPrompt);
  72. }
  73. function restoreDefaultPrompts() {
  74. prompt.restoreDefaultPrompts();
  75. }
  76. const inquirer = {
  77. prompt,
  78. ui: {
  79. BottomBar,
  80. Prompt,
  81. },
  82. createPromptModule,
  83. registerPrompt,
  84. restoreDefaultPrompts,
  85. Separator,
  86. };
  87. export default inquirer;