task-wrapper.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.TaskWrapper = void 0;
  7. const through_1 = __importDefault(require("through"));
  8. const listr_interface_1 = require("../interfaces/listr.interface");
  9. const state_constants_1 = require("../interfaces/state.constants");
  10. const index_1 = require("../index");
  11. const prompt_1 = require("../utils/prompt");
  12. class TaskWrapper {
  13. constructor(task, errors, options) {
  14. this.task = task;
  15. this.errors = errors;
  16. this.options = options;
  17. }
  18. set title(data) {
  19. this.task.title$ = data;
  20. }
  21. get title() {
  22. return this.task.title;
  23. }
  24. set output(data) {
  25. this.task.output$ = data;
  26. }
  27. get output() {
  28. return this.task.output;
  29. }
  30. set state(data) {
  31. this.task.state$ = data;
  32. }
  33. set message(data) {
  34. this.task.message$ = data;
  35. }
  36. newListr(task, options) {
  37. let tasks;
  38. if (typeof task === 'function') {
  39. tasks = task(this);
  40. }
  41. else {
  42. tasks = task;
  43. }
  44. return new index_1.Listr(tasks, options);
  45. }
  46. report(error) {
  47. var _a, _b;
  48. if (error instanceof listr_interface_1.ListrError) {
  49. for (const err of error.errors) {
  50. this.errors.push(err);
  51. this.message = { error: err.message || ((_a = this.task) === null || _a === void 0 ? void 0 : _a.title) || 'Task with no title.' };
  52. }
  53. }
  54. else {
  55. this.errors.push(error);
  56. this.message = { error: error.message || ((_b = this.task) === null || _b === void 0 ? void 0 : _b.title) || 'Task with no title.' };
  57. }
  58. }
  59. skip(message) {
  60. var _a;
  61. this.state = state_constants_1.stateConstants.SKIPPED;
  62. if (message) {
  63. this.message = { skip: message || ((_a = this.task) === null || _a === void 0 ? void 0 : _a.title) || 'Task with no title.' };
  64. }
  65. }
  66. async prompt(options) {
  67. var _a;
  68. this.task.prompt = true;
  69. return prompt_1.createPrompt.bind(this)(options, { ...(_a = this.options) === null || _a === void 0 ? void 0 : _a.injectWrapper });
  70. }
  71. stdout() {
  72. return through_1.default((chunk) => {
  73. const pattern = new RegExp('(?:\\u001b|\\u009b)\\[[\\=><~/#&.:=?%@~_-]*[0-9]*[\\a-ln-tqyz=><~/#&.:=?%@~_-]+', 'gmi');
  74. chunk = chunk.toString();
  75. chunk = chunk.replace(pattern, '');
  76. chunk = chunk.replace(new RegExp(/\u0007/, 'gmi'), '');
  77. if (chunk !== '') {
  78. this.output = chunk;
  79. }
  80. });
  81. }
  82. run(ctx) {
  83. return this.task.run(ctx, this);
  84. }
  85. }
  86. exports.TaskWrapper = TaskWrapper;
  87. //# sourceMappingURL=task-wrapper.js.map