task.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Task = void 0;
  4. const rxjs_1 = require("rxjs");
  5. const stream_1 = require("stream");
  6. const listr_interface_1 = require("../interfaces/listr.interface");
  7. const state_constants_1 = require("../interfaces/state.constants");
  8. const index_1 = require("../index");
  9. const renderer_1 = require("../utils/renderer");
  10. const uuid_1 = require("../utils/uuid");
  11. class Task extends rxjs_1.Subject {
  12. constructor(listr, tasks, options, rendererOptions) {
  13. var _a, _b, _c;
  14. super();
  15. this.listr = listr;
  16. this.tasks = tasks;
  17. this.options = options;
  18. this.rendererOptions = rendererOptions;
  19. this.message = {};
  20. this.id = uuid_1.generateUUID();
  21. this.title = (_a = this.tasks) === null || _a === void 0 ? void 0 : _a.title;
  22. this.cleanTitle = this.title;
  23. this.task = this.tasks.task;
  24. this.skip = ((_b = this.tasks) === null || _b === void 0 ? void 0 : _b.skip) || (() => false);
  25. this.enabledFn = ((_c = this.tasks) === null || _c === void 0 ? void 0 : _c.enabled) || (() => true);
  26. this.rendererTaskOptions = this.tasks.options;
  27. this.renderHook$ = this.listr.renderHook$;
  28. this.subscribe(() => {
  29. this.renderHook$.next();
  30. });
  31. }
  32. set state$(state) {
  33. this.state = state;
  34. this.next({
  35. type: 'STATE',
  36. data: state
  37. });
  38. }
  39. set output$(data) {
  40. this.output = data;
  41. this.next({
  42. type: 'DATA',
  43. data
  44. });
  45. }
  46. set message$(data) {
  47. this.message = { ...this.message, ...data };
  48. this.next({
  49. type: 'MESSAGE',
  50. data
  51. });
  52. }
  53. set title$(title) {
  54. this.title = title;
  55. this.cleanTitle = title;
  56. this.next({
  57. type: 'TITLE',
  58. data: title
  59. });
  60. }
  61. async check(ctx) {
  62. if (this.state === undefined) {
  63. if (typeof this.enabledFn === 'function') {
  64. this.enabled = await this.enabledFn(ctx);
  65. }
  66. else {
  67. this.enabled = this.enabledFn;
  68. }
  69. this.next({
  70. type: 'ENABLED',
  71. data: this.enabled
  72. });
  73. }
  74. }
  75. hasSubtasks() {
  76. var _a;
  77. return ((_a = this.subtasks) === null || _a === void 0 ? void 0 : _a.length) > 0;
  78. }
  79. isPending() {
  80. return this.state === state_constants_1.stateConstants.PENDING;
  81. }
  82. isSkipped() {
  83. return this.state === state_constants_1.stateConstants.SKIPPED;
  84. }
  85. isCompleted() {
  86. return this.state === state_constants_1.stateConstants.COMPLETED;
  87. }
  88. hasFailed() {
  89. return this.state === state_constants_1.stateConstants.FAILED;
  90. }
  91. isEnabled() {
  92. return this.enabled;
  93. }
  94. hasTitle() {
  95. return typeof (this === null || this === void 0 ? void 0 : this.title) === 'string';
  96. }
  97. isPrompt() {
  98. if (this.prompt) {
  99. return true;
  100. }
  101. else {
  102. return false;
  103. }
  104. }
  105. async run(context, wrapper) {
  106. const handleResult = (result) => {
  107. if (result instanceof index_1.Listr) {
  108. result.options = { ...this.options, ...result.options };
  109. const rendererClass = renderer_1.getRenderer('silent');
  110. result.rendererClass = rendererClass.renderer;
  111. result.renderHook$.subscribe(() => {
  112. this.renderHook$.next();
  113. });
  114. this.subtasks = result.tasks;
  115. this.next({ type: 'SUBTASK' });
  116. result = result.run(context);
  117. }
  118. else if (this.isPrompt()) {
  119. }
  120. else if (result instanceof Promise) {
  121. result = result.then(handleResult);
  122. }
  123. else if (result instanceof stream_1.Readable) {
  124. result = new Promise((resolve, reject) => {
  125. result.on('data', (data) => {
  126. this.output$ = data.toString();
  127. });
  128. result.on('error', (error) => reject(error));
  129. result.on('end', () => resolve());
  130. });
  131. }
  132. else if (result instanceof rxjs_1.Observable) {
  133. result = new Promise((resolve, reject) => {
  134. result.subscribe({
  135. next: (data) => {
  136. this.output$ = data;
  137. },
  138. error: reject,
  139. complete: resolve
  140. });
  141. });
  142. }
  143. return result;
  144. };
  145. const startTime = Date.now();
  146. this.state$ = state_constants_1.stateConstants.PENDING;
  147. let skipped;
  148. if (typeof this.skip === 'function') {
  149. skipped = await this.skip(context);
  150. }
  151. if (skipped) {
  152. if (typeof skipped === 'string') {
  153. this.message$ = { skip: skipped };
  154. }
  155. else if (this.hasTitle()) {
  156. this.message$ = { skip: this.title };
  157. }
  158. else {
  159. this.message$ = { skip: 'Skipped task without a title.' };
  160. }
  161. this.state$ = state_constants_1.stateConstants.SKIPPED;
  162. return;
  163. }
  164. try {
  165. await handleResult(this.task(context, wrapper));
  166. if (this.isPending()) {
  167. this.message$ = { duration: Date.now() - startTime };
  168. this.state$ = state_constants_1.stateConstants.COMPLETED;
  169. }
  170. }
  171. catch (error) {
  172. this.state$ = state_constants_1.stateConstants.FAILED;
  173. if (this.prompt instanceof listr_interface_1.PromptError) {
  174. error = new Error(this.prompt.message);
  175. }
  176. if (error instanceof listr_interface_1.ListrError) {
  177. wrapper.report(error);
  178. return;
  179. }
  180. if (!this.hasSubtasks()) {
  181. this.title = error.message;
  182. }
  183. wrapper.report(error);
  184. if (this.listr.options.exitOnError !== false) {
  185. throw error;
  186. }
  187. }
  188. finally {
  189. this.complete();
  190. }
  191. }
  192. }
  193. exports.Task = Task;
  194. //# sourceMappingURL=task.js.map