listr.interface.d.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /// <reference types="node" />
  2. import type * as Enquirer from 'enquirer';
  3. import type { Observable } from 'rxjs';
  4. import { Readable } from 'stream';
  5. import { ListrDefaultNonTTYRendererOptions, ListrDefaultRendererOptions, ListrDefaultRendererValue, ListrFallbackRendererValue, ListrGetRendererTaskOptions, ListrRendererFactory, ListrRendererValue } from './renderer.interface';
  6. import { ListrEventType } from '../constants/event.constants';
  7. import { Task } from '../lib/task';
  8. import { TaskWrapper } from '../lib/task-wrapper';
  9. import { Listr } from '../listr';
  10. /** Listr Default Context */
  11. export declare type ListrContext = any | undefined;
  12. /**
  13. * ListrTask.
  14. *
  15. * Defines the task, conditions and options to run a specific task in the listr.
  16. */
  17. export interface ListrTask<Ctx = ListrContext, Renderer extends ListrRendererFactory = any> {
  18. /**
  19. * Title of the task.
  20. *
  21. * Give this task a title if you want to track it by name in the current renderer.
  22. *
  23. * Tasks without a title will hide in the default renderer and are useful for running a background instance.
  24. * On verbose renderer, state changes from these tasks will log as 'Task without a title.'
  25. */
  26. title?: string;
  27. /**
  28. * The task itself.
  29. *
  30. * Task can be a sync or async function, an Observable, or a Stream.
  31. * Task will be executed, if the certain criteria of the state are met and whenever the time for that specific task has come.
  32. */
  33. task: (ctx: Ctx, task: TaskWrapper<Ctx, Renderer>) => void | ListrTaskResult<Ctx>;
  34. /**
  35. * Skip this task depending on the context.
  36. *
  37. * The function that has been passed in will be evaluated at the runtime when the task tries to initially run.
  38. */
  39. skip?: boolean | string | ((ctx: Ctx) => boolean | string | Promise<boolean | string>);
  40. /**
  41. * Enable a task depending on the context.
  42. *
  43. * The function that has been passed in will be evaluated at the initial creation of the Listr class for rendering purposes,
  44. * as well as re-evaluated when the time for that specific task has come.
  45. */
  46. enabled?: boolean | ((ctx: Ctx) => boolean | Promise<boolean>);
  47. /**
  48. * Adds the given number of retry attempts to the task if the task fails.
  49. */
  50. retry?: number;
  51. /**
  52. * Runs a specific event if the current task or any of the subtasks has failed.
  53. *
  54. * Mostly useful for rollback purposes for subtasks.
  55. * But can also be useful whenever a task is failed and some measures have to be taken to ensure the state is not changed.
  56. */
  57. rollback?: (ctx: Ctx, task: TaskWrapper<Ctx, Renderer>) => void | ListrTaskResult<Ctx>;
  58. /**
  59. * Set exit on the error option from task-level instead of setting it for all the subtasks.
  60. */
  61. exitOnError?: boolean | ((ctx: Ctx) => boolean | Promise<boolean>);
  62. /**
  63. * Per task options, that depends on the selected renderer.
  64. *
  65. * These options depend on the implementation of the selected renderer. If the selected renderer has no options it will
  66. * be displayed as never.
  67. */
  68. options?: ListrGetRendererTaskOptions<Renderer>;
  69. }
  70. /**
  71. * Options to set the behavior of this base task.
  72. */
  73. export interface ListrOptions<Ctx = ListrContext> {
  74. /**
  75. * To inject a context through this options wrapper. Context can also be defined in run time.
  76. *
  77. * @default {}
  78. */
  79. ctx?: Ctx;
  80. /**
  81. * Concurrency sets how many tasks will be run at the same time in parallel.
  82. *
  83. * @default false > Default is to run everything synchronously.
  84. *
  85. * `true` will set it to `Infinity`, `false` will set it to synchronous.
  86. *
  87. * If you pass in a `number` it will limit it to that number.
  88. */
  89. concurrent?: boolean | number;
  90. /**
  91. * Determine the default behavior of exiting on errors.
  92. *
  93. * @default true > exit on any error coming from the tasks.
  94. */
  95. exitOnError?: boolean;
  96. /**
  97. * Determine the behavior of exiting after rollback actions.
  98. *
  99. * This is independent of exitOnError, since failure of a rollback can be a more critical operation comparing to
  100. * failing a single task.
  101. *
  102. * @default true > exit after rolling back tasks
  103. */
  104. exitAfterRollback?: boolean;
  105. /**
  106. * By default, Listr2 will track SIGINIT signal to update the renderer one last time before completely failing.
  107. *
  108. * @default true
  109. */
  110. registerSignalListeners?: boolean;
  111. /**
  112. * Determine the certain condition required to use the non-TTY renderer.
  113. *
  114. * @default null > handled internally
  115. */
  116. rendererFallback?: boolean | (() => boolean);
  117. /**
  118. * Determine the certain condition required to use the silent renderer.
  119. *
  120. * @default null > handled internally
  121. */
  122. rendererSilent?: boolean | (() => boolean);
  123. /**
  124. * Disabling the color, useful for tests and such.
  125. *
  126. * @default false
  127. */
  128. disableColor?: boolean;
  129. /**
  130. * Inject data directly to TaskWrapper.
  131. */
  132. injectWrapper?: {
  133. enquirer?: Enquirer<object>;
  134. };
  135. }
  136. /**
  137. * Task can be set of sync or async function, an Observable or a stream.
  138. */
  139. export declare type ListrTaskResult<Ctx> = string | Promise<any> | Listr<Ctx, ListrRendererValue, any> | Readable | NodeJS.ReadableStream | Observable<any>;
  140. /**
  141. * Parent class options.
  142. *
  143. * Parent class has more options where you can also select the and set renderer and non-tty renderer.
  144. *
  145. * Any subtasks will respect those options so they will be stripped of that properties.
  146. */
  147. export declare type ListrBaseClassOptions<Ctx = ListrContext, Renderer extends ListrRendererValue = ListrDefaultRendererValue, FallbackRenderer extends ListrRendererValue = ListrFallbackRendererValue> = ListrOptions<Ctx> & ListrDefaultRendererOptions<Renderer> & ListrDefaultNonTTYRendererOptions<FallbackRenderer>;
  148. /**
  149. * Sub class options.
  150. *
  151. * Subtasks has reduced set options where the missing ones are explicitly set by the base class.
  152. */
  153. export declare type ListrSubClassOptions<Ctx = ListrContext, Renderer extends ListrRendererValue = ListrDefaultRendererValue> = ListrOptions<Ctx> & Omit<ListrDefaultRendererOptions<Renderer>, 'renderer'>;
  154. /** The internal communication event. */
  155. export declare type ListrEvent = {
  156. type: Exclude<ListrEventType, 'MESSAGE' | 'DATA'>;
  157. data?: string | boolean;
  158. } | {
  159. type: ListrEventType.DATA;
  160. data: string;
  161. } | {
  162. type: ListrEventType.MESSAGE;
  163. data: Task<any, any>['message'];
  164. };
  165. /**
  166. * Used to match event.type to ListrEvent permutations
  167. */
  168. export declare type ListrEventFromType<T extends ListrEventType, E = ListrEvent> = E extends {
  169. type: infer U;
  170. } ? T extends U ? E : never : never;