prompt.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.destroyPrompt = exports.createPrompt = void 0;
  4. const event_constants_1 = require("../constants/event.constants");
  5. const state_constants_1 = require("../constants/state.constants");
  6. const listr_error_interface_1 = require("../interfaces/listr-error.interface");
  7. const task_wrapper_1 = require("../lib/task-wrapper");
  8. /**
  9. * Create a new prompt with Enquirer externally.
  10. * This extends enquirer so you dont have to give a name to single prompts and such so it is also
  11. * useful to use externally.
  12. * @param this
  13. * @param options
  14. * @param settings
  15. */
  16. async function createPrompt(options, settings) {
  17. // override cancel callback
  18. let cancelCallback;
  19. /* istanbul ignore if */
  20. if (settings === null || settings === void 0 ? void 0 : settings.cancelCallback) {
  21. cancelCallback = settings.cancelCallback;
  22. } /* istanbul ignore next */
  23. else {
  24. cancelCallback = defaultCancelCallback;
  25. }
  26. // assign default if there is single prompt
  27. if (!Array.isArray(options)) {
  28. options = [{ ...options, name: 'default' }];
  29. } /* istanbul ignore next */
  30. else if (options.length === 1) {
  31. options = options.reduce((o, option) => {
  32. return [...o, Object.assign(option, { name: 'default' })];
  33. }, []);
  34. }
  35. // assign default enquirer options
  36. options = options.reduce((o, option) => {
  37. var _a;
  38. return [
  39. ...o,
  40. Object.assign(option, {
  41. // this is for outside calls, if it is not called from taskwrapper with bind
  42. stdout: this instanceof task_wrapper_1.TaskWrapper ? (_a = settings === null || settings === void 0 ? void 0 : settings.stdout) !== null && _a !== void 0 ? _a : this.stdout() : process.stdout,
  43. onCancel: cancelCallback.bind(this, settings)
  44. })
  45. ];
  46. }, []);
  47. let enquirer;
  48. if (settings === null || settings === void 0 ? void 0 : settings.enquirer) {
  49. // injected enquirer
  50. enquirer = settings.enquirer;
  51. }
  52. else {
  53. try {
  54. enquirer = new (await Promise.resolve().then(() => require('enquirer')))();
  55. } /* istanbul ignore next */
  56. catch (e) {
  57. this.task.prompt = new listr_error_interface_1.PromptError('Enquirer is a peer dependency that must be installed separately.');
  58. throw new Error(e);
  59. }
  60. }
  61. // i use this externally as well, this is a bandaid
  62. if (this instanceof task_wrapper_1.TaskWrapper) {
  63. // Capture the prompt instance so we can use it later
  64. enquirer.on('prompt', (prompt) => this.task.prompt = prompt);
  65. // Clear the prompt instance once it's submitted
  66. // Can't use on cancel, since that might hold a PromptError object
  67. enquirer.on('submit', () => this.task.prompt = undefined);
  68. this.task.subscribe((event) => {
  69. if (event.type === event_constants_1.ListrEventType.STATE && event.data === state_constants_1.ListrTaskState.SKIPPED) {
  70. if (this.task.prompt && !(this.task.prompt instanceof listr_error_interface_1.PromptError)) {
  71. this.task.prompt.submit();
  72. }
  73. }
  74. });
  75. }
  76. const response = (await enquirer.prompt(options));
  77. // return default name if it is single prompt
  78. if (options.length === 1) {
  79. return response.default;
  80. }
  81. else {
  82. return response;
  83. }
  84. }
  85. exports.createPrompt = createPrompt;
  86. function destroyPrompt(throwError = false) {
  87. if (!this.task.prompt || this.task.prompt instanceof listr_error_interface_1.PromptError) {
  88. // If there's no prompt, can't cancel
  89. return;
  90. }
  91. if (throwError) {
  92. this.task.prompt.cancel();
  93. }
  94. else {
  95. this.task.prompt.submit();
  96. }
  97. }
  98. exports.destroyPrompt = destroyPrompt;
  99. function defaultCancelCallback(settings) {
  100. const errorMsg = 'Cancelled prompt.';
  101. if (this instanceof task_wrapper_1.TaskWrapper) {
  102. this.task.prompt = new listr_error_interface_1.PromptError(errorMsg);
  103. } /* istanbul ignore next */
  104. else if ((settings === null || settings === void 0 ? void 0 : settings.error) !== false) {
  105. throw new Error(errorMsg);
  106. } /* istanbul ignore next */
  107. else {
  108. return errorMsg;
  109. }
  110. }