task-wrapper.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.TaskWrapper = void 0;
  4. const through = require("through");
  5. const clearline_regex_constants_1 = require("../constants/clearline-regex.constants");
  6. const state_constants_1 = require("../constants/state.constants");
  7. const listr_error_interface_1 = require("../interfaces/listr-error.interface");
  8. const listr_1 = require("../listr");
  9. const general_1 = require("../utils/general");
  10. const prompt_1 = require("../utils/prompt");
  11. /**
  12. * Extend the task to have more functionality while accesing from the outside.
  13. */
  14. class TaskWrapper {
  15. constructor(task, errors, options) {
  16. this.task = task;
  17. this.errors = errors;
  18. this.options = options;
  19. }
  20. /** Change the title of the current task. */
  21. set title(data) {
  22. this.task.title$ = data;
  23. }
  24. /** Get the title of the current task. */
  25. get title() {
  26. return this.task.title;
  27. }
  28. /** Send a output to the output channel. */
  29. set output(data) {
  30. this.task.output$ = data;
  31. }
  32. /** Get the output from the output channel. */
  33. get output() {
  34. return this.task.output;
  35. }
  36. /** Create a new subtask with given renderer selection from the parent task. */
  37. newListr(task, options) {
  38. let tasks;
  39. if (typeof task === 'function') {
  40. tasks = task(this);
  41. }
  42. else {
  43. tasks = task;
  44. }
  45. return new listr_1.Listr(tasks, options);
  46. }
  47. /** Report a error in process for error collection. */
  48. report(error, type) {
  49. var _a, _b, _c;
  50. this.errors.push(new listr_error_interface_1.ListrError(error, type, (0, general_1.cloneObject)(this.task.listr.ctx), (0, general_1.cloneObject)(this.task)));
  51. this.task.message$ = { error: (_c = (_a = error.message) !== null && _a !== void 0 ? _a : (_b = this.task) === null || _b === void 0 ? void 0 : _b.title) !== null && _c !== void 0 ? _c : 'Task with no title.' };
  52. }
  53. /** Skip current task. */
  54. skip(message) {
  55. var _a, _b;
  56. this.task.state$ = state_constants_1.ListrTaskState.SKIPPED;
  57. if (message) {
  58. this.task.message$ = { skip: (_b = message !== null && message !== void 0 ? message : (_a = this.task) === null || _a === void 0 ? void 0 : _a.title) !== null && _b !== void 0 ? _b : 'Task with no title.' };
  59. }
  60. }
  61. /** Get the number of retrying, else returns false */
  62. isRetrying() {
  63. return this.task.isRetrying() ? this.task.retry : { count: 0 };
  64. }
  65. /**
  66. * Create a new Enquirer prompt using prompt options.
  67. *
  68. * Since process.stdout is controlled by Listr, this will passthrough all Enquirer data through internal stdout.
  69. */
  70. async prompt(options) {
  71. var _a;
  72. return prompt_1.createPrompt.bind(this)(options, { ...(_a = this.options) === null || _a === void 0 ? void 0 : _a.injectWrapper });
  73. }
  74. /** Cancels the current prompt attach to this task. */
  75. cancelPrompt(throwError = false) {
  76. return prompt_1.destroyPrompt.bind(this)(throwError);
  77. }
  78. /**
  79. * Pass stream of data to internal stdout.
  80. *
  81. * Since Listr2 takes control of process.stdout utilizing the default renderer, any data outputted to process.stdout
  82. * will corupt its looks.
  83. *
  84. * This returns a fake stream to pass any stream inside Listr as task data.
  85. */
  86. stdout() {
  87. return through((chunk) => {
  88. chunk = chunk.toString();
  89. chunk = chunk.replace(new RegExp(clearline_regex_constants_1.CLEAR_LINE_REGEX, 'gmi'), '');
  90. chunk = chunk.replace(new RegExp(clearline_regex_constants_1.BELL_REGEX, 'gmi'), '');
  91. if (chunk !== '') {
  92. this.output = chunk;
  93. }
  94. });
  95. }
  96. /** Run this task. */
  97. run(ctx) {
  98. return this.task.run(ctx, this);
  99. }
  100. }
  101. exports.TaskWrapper = TaskWrapper;