task.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. 'use strict';
  2. const isPromise = require('is-promise');
  3. const streamToObservable = require('@samverschueren/stream-to-observable');
  4. const Subject = require('rxjs').Subject;
  5. const renderer = require('./renderer');
  6. const state = require('./state');
  7. const utils = require('./utils');
  8. const ListrError = require('./listr-error');
  9. const defaultSkipFn = () => false;
  10. class Task extends Subject {
  11. constructor(listr, task, options) {
  12. super();
  13. if (!task) {
  14. throw new TypeError('Expected a task');
  15. }
  16. if (typeof task.title !== 'string') {
  17. throw new TypeError(`Expected property \`title\` to be of type \`string\`, got \`${typeof task.title}\``);
  18. }
  19. if (typeof task.task !== 'function') {
  20. throw new TypeError(`Expected property \`task\` to be of type \`function\`, got \`${typeof task.task}\``);
  21. }
  22. if (task.skip && typeof task.skip !== 'function') {
  23. throw new TypeError(`Expected property \`skip\` to be of type \`function\`, got \`${typeof task.skip}\``);
  24. }
  25. if (task.enabled && typeof task.enabled !== 'function') {
  26. throw new TypeError(`Expected property \`enabled\` to be of type \`function\`, got \`${typeof task.enabled}\``);
  27. }
  28. this._listr = listr;
  29. this._options = options || {};
  30. this._subtasks = [];
  31. this._enabledFn = task.enabled;
  32. this._isEnabled = true;
  33. this.output = undefined;
  34. this.title = task.title;
  35. this.skip = task.skip || defaultSkipFn;
  36. this.task = task.task;
  37. }
  38. get subtasks() {
  39. return this._subtasks;
  40. }
  41. set state(state) {
  42. this._state = state;
  43. this.next({
  44. type: 'STATE'
  45. });
  46. }
  47. get state() {
  48. return state.toString(this._state);
  49. }
  50. check(ctx) {
  51. // Check if a task is enabled or disabled
  52. if (this._state === undefined && this._enabledFn) {
  53. const isEnabled = this._enabledFn(ctx);
  54. if (this._isEnabled !== isEnabled) {
  55. this._isEnabled = isEnabled;
  56. this.next({
  57. type: 'ENABLED',
  58. data: isEnabled
  59. });
  60. }
  61. }
  62. }
  63. hasSubtasks() {
  64. return this._subtasks.length > 0;
  65. }
  66. isPending() {
  67. return this._state === state.PENDING;
  68. }
  69. isSkipped() {
  70. return this._state === state.SKIPPED;
  71. }
  72. isCompleted() {
  73. return this._state === state.COMPLETED;
  74. }
  75. isEnabled() {
  76. return this._isEnabled;
  77. }
  78. hasFailed() {
  79. return this._state === state.FAILED;
  80. }
  81. run(context, wrapper) {
  82. const handleResult = result => {
  83. // Detect the subtask
  84. if (utils.isListr(result)) {
  85. result._options = Object.assign(this._options, result._options);
  86. result.exitOnError = result._options.exitOnError;
  87. result.setRenderer(renderer.getRenderer('silent'));
  88. this._subtasks = result.tasks;
  89. this.next({
  90. type: 'SUBTASKS'
  91. });
  92. return result.run(context);
  93. }
  94. // Detect stream
  95. if (utils.isStream(result)) {
  96. result = streamToObservable(result);
  97. }
  98. // Detect Observable
  99. if (utils.isObservable(result)) {
  100. result = new Promise((resolve, reject) => {
  101. result.subscribe({
  102. next: data => {
  103. this.output = data;
  104. this.next({
  105. type: 'DATA',
  106. data
  107. });
  108. },
  109. error: reject,
  110. complete: resolve
  111. });
  112. });
  113. }
  114. // Detect promise
  115. if (isPromise(result)) {
  116. return result.then(handleResult);
  117. }
  118. return result;
  119. };
  120. return Promise.resolve()
  121. .then(() => {
  122. this.state = state.PENDING;
  123. return this.skip(context);
  124. })
  125. .then(skipped => {
  126. if (skipped) {
  127. if (typeof skipped === 'string') {
  128. this.output = skipped;
  129. }
  130. this.state = state.SKIPPED;
  131. return;
  132. }
  133. return handleResult(this.task(context, wrapper));
  134. })
  135. .then(() => {
  136. if (this.isPending()) {
  137. this.state = state.COMPLETED;
  138. }
  139. })
  140. .catch(err => {
  141. this.state = state.FAILED;
  142. if (err instanceof ListrError) {
  143. wrapper.report(err);
  144. return;
  145. }
  146. if (!this.hasSubtasks()) {
  147. // Do not show the message if we have subtasks as the error is already shown in the subtask
  148. this.output = err.message;
  149. }
  150. this.next({
  151. type: 'DATA',
  152. data: err.message
  153. });
  154. wrapper.report(err);
  155. if (this._listr.exitOnError !== false) {
  156. // Do not exit when explicitely set to `false`
  157. throw err;
  158. }
  159. })
  160. .then(() => {
  161. // Mark the Observable as completed
  162. this.complete();
  163. });
  164. }
  165. }
  166. module.exports = Task;