index.d.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. import { Observable, Subject } from 'rxjs';
  2. import Enquirer from 'enquirer';
  3. import { Readable, Writable } from 'stream';
  4. import { WriteStream } from 'fs';
  5. /** Type of listr internal events. */
  6. declare enum ListrEventType {
  7. TITLE = "TITLE",
  8. STATE = "STATE",
  9. ENABLED = "ENABLED",
  10. SUBTASK = "SUBTASK",
  11. DATA = "DATA",
  12. MESSAGE = "MESSAGE"
  13. }
  14. /** Listr Default Context */
  15. declare type ListrContext = any | undefined;
  16. /**
  17. * ListrTask.
  18. *
  19. * Defines the task, conditions and options to run a specific task in the listr.
  20. */
  21. interface ListrTask<Ctx = ListrContext, Renderer extends ListrRendererFactory = any> {
  22. /**
  23. * Title of the task.
  24. *
  25. * Give this task a title if you want to track it by name in the current renderer.
  26. *
  27. * Tasks without a title will hide in the default renderer and are useful for running a background instance.
  28. * On verbose renderer, state changes from these tasks will log as 'Task without a title.'
  29. */
  30. title?: string;
  31. /**
  32. * The task itself.
  33. *
  34. * Task can be a sync or async function, an Observable, or a Stream.
  35. * Task will be executed, if the certain criteria of the state are met and whenever the time for that specific task has come.
  36. */
  37. task: (ctx: Ctx, task: TaskWrapper<Ctx, Renderer>) => void | ListrTaskResult<Ctx>;
  38. /**
  39. * Skip this task depending on the context.
  40. *
  41. * The function that has been passed in will be evaluated at the runtime when the task tries to initially run.
  42. */
  43. skip?: boolean | string | ((ctx: Ctx) => boolean | string | Promise<boolean | string>);
  44. /**
  45. * Enable a task depending on the context.
  46. *
  47. * The function that has been passed in will be evaluated at the initial creation of the Listr class for rendering purposes,
  48. * as well as re-evaluated when the time for that specific task has come.
  49. */
  50. enabled?: boolean | ((ctx: Ctx) => boolean | Promise<boolean>);
  51. /**
  52. * Adds the given number of retry attempts to the task if the task fails.
  53. */
  54. retry?: number;
  55. /**
  56. * Runs a specific event if the current task or any of the subtasks has failed.
  57. *
  58. * Mostly useful for rollback purposes for subtasks.
  59. * But can also be useful whenever a task is failed and some measures have to be taken to ensure the state is not changed.
  60. */
  61. rollback?: (ctx: Ctx, task: TaskWrapper<Ctx, Renderer>) => void | ListrTaskResult<Ctx>;
  62. /**
  63. * Set exit on the error option from task-level instead of setting it for all the subtasks.
  64. */
  65. exitOnError?: boolean | ((ctx: Ctx) => boolean | Promise<boolean>);
  66. /**
  67. * Per task options, that depends on the selected renderer.
  68. *
  69. * These options depend on the implementation of the selected renderer. If the selected renderer has no options it will
  70. * be displayed as never.
  71. */
  72. options?: ListrGetRendererTaskOptions<Renderer>;
  73. }
  74. /**
  75. * Options to set the behavior of this base task.
  76. */
  77. interface ListrOptions<Ctx = ListrContext> {
  78. /**
  79. * To inject a context through this options wrapper. Context can also be defined in run time.
  80. *
  81. * @default {}
  82. */
  83. ctx?: Ctx;
  84. /**
  85. * Concurrency sets how many tasks will be run at the same time in parallel.
  86. *
  87. * @default false > Default is to run everything synchronously.
  88. *
  89. * `true` will set it to `Infinity`, `false` will set it to synchronous.
  90. *
  91. * If you pass in a `number` it will limit it to that number.
  92. */
  93. concurrent?: boolean | number;
  94. /**
  95. * Determine the default behavior of exiting on errors.
  96. *
  97. * @default true > exit on any error coming from the tasks.
  98. */
  99. exitOnError?: boolean;
  100. /**
  101. * Determine the behavior of exiting after rollback actions.
  102. *
  103. * This is independent of exitOnError, since failure of a rollback can be a more critical operation comparing to
  104. * failing a single task.
  105. *
  106. * @default true > exit after rolling back tasks
  107. */
  108. exitAfterRollback?: boolean;
  109. /**
  110. * Collects errors to `ListrInstance.errors`
  111. *
  112. * This can take up a lot of memory, so disabling it can fix out-of-memory errors
  113. *
  114. * - 'full' will clone the current context and task in to the error instance
  115. * - 'minimal' will only collect the error message and the location
  116. * - false will collect no errors
  117. *
  118. * @default 'minimal'
  119. */
  120. collectErrors?: false | 'minimal' | 'full';
  121. /**
  122. * By default, Listr2 will track SIGINIT signal to update the renderer one last time before completely failing.
  123. *
  124. * @default true
  125. */
  126. registerSignalListeners?: boolean;
  127. /**
  128. * Determine the certain condition required to use the non-TTY renderer.
  129. *
  130. * @default null > handled internally
  131. */
  132. rendererFallback?: boolean | (() => boolean);
  133. /**
  134. * Determine the certain condition required to use the silent renderer.
  135. *
  136. * @default null > handled internally
  137. */
  138. rendererSilent?: boolean | (() => boolean);
  139. /**
  140. * Disabling the color, useful for tests and such.
  141. *
  142. * @default false
  143. */
  144. disableColor?: boolean;
  145. /**
  146. * Inject data directly to TaskWrapper.
  147. */
  148. injectWrapper?: {
  149. enquirer?: Enquirer<object>;
  150. };
  151. }
  152. /**
  153. * Task can be set of sync or async function, an Observable or a stream.
  154. */
  155. declare type ListrTaskResult<Ctx> = string | Promise<any> | Listr<Ctx, ListrRendererValue, any> | Readable | NodeJS.ReadableStream | Observable<any>;
  156. /**
  157. * Parent class options.
  158. *
  159. * Parent class has more options where you can also select the and set renderer and non-tty renderer.
  160. *
  161. * Any subtasks will respect those options so they will be stripped of that properties.
  162. */
  163. declare type ListrBaseClassOptions<Ctx = ListrContext, Renderer extends ListrRendererValue = ListrDefaultRendererValue, FallbackRenderer extends ListrRendererValue = ListrFallbackRendererValue> = ListrOptions<Ctx> & ListrDefaultRendererOptions<Renderer> & ListrDefaultNonTTYRendererOptions<FallbackRenderer>;
  164. /**
  165. * Sub class options.
  166. *
  167. * Subtasks has reduced set options where the missing ones are explicitly set by the base class.
  168. */
  169. declare type ListrSubClassOptions<Ctx = ListrContext, Renderer extends ListrRendererValue = ListrDefaultRendererValue> = ListrOptions<Ctx> & Omit<ListrDefaultRendererOptions<Renderer>, 'renderer'>;
  170. /** The internal communication event. */
  171. declare type ListrEvent = {
  172. type: Exclude<ListrEventType, 'MESSAGE' | 'DATA'>;
  173. data?: string | boolean;
  174. } | {
  175. type: ListrEventType.DATA;
  176. data: string;
  177. } | {
  178. type: ListrEventType.MESSAGE;
  179. data: Task<any, any>['message'];
  180. };
  181. /**
  182. * Used to match event.type to ListrEvent permutations
  183. */
  184. declare type ListrEventFromType<T extends ListrEventType, E = ListrEvent> = E extends {
  185. type: infer U;
  186. } ? T extends U ? E : never : never;
  187. interface BasePromptOptions {
  188. message: string | (() => string) | (() => Promise<string>);
  189. initial?: boolean | number | number[] | string | (() => string) | (() => Promise<string>);
  190. required?: boolean;
  191. stdin?: NodeJS.ReadStream;
  192. stdout?: NodeJS.WriteStream;
  193. header?: string;
  194. footer?: string;
  195. skip?: (value: any) => boolean | Promise<boolean>;
  196. format?: (value: any) => any | Promise<any>;
  197. result?: (value: any) => any | Promise<any>;
  198. validate?: (value: any, state: any) => boolean | Promise<boolean> | string | Promise<string> | Promise<string | boolean>;
  199. onSubmit?: (name: any, value: any, prompt: Enquirer.Prompt) => boolean | Promise<boolean>;
  200. onCancel?: (name: any, value: any, prompt: Enquirer.Prompt) => boolean | Promise<boolean>;
  201. }
  202. interface BasePromptOptionsWithName extends BasePromptOptions {
  203. name: string | (() => string);
  204. }
  205. interface ArrayPromptOptions extends BasePromptOptions {
  206. choices: string[] | BasePromptOptionsWithName[];
  207. maxChoices?: number;
  208. multiple?: boolean;
  209. initial?: number | number[];
  210. delay?: number;
  211. separator?: boolean;
  212. sort?: boolean;
  213. linebreak?: boolean;
  214. edgeLength?: number;
  215. align?: 'left' | 'right';
  216. scroll?: boolean;
  217. hint?: string;
  218. }
  219. interface BooleanPromptOptions extends BasePromptOptions {
  220. initial?: boolean | (() => string) | (() => Promise<string>);
  221. }
  222. interface StringPromptOptions extends BasePromptOptions {
  223. initial?: string;
  224. multiline?: boolean;
  225. }
  226. interface ScalePromptOptions extends ArrayPromptOptions {
  227. scale: StringPromptOptions[];
  228. margin?: [number, number, number, number];
  229. }
  230. interface NumberPromptOptions extends BasePromptOptions {
  231. min?: number;
  232. max?: number;
  233. delay?: number;
  234. float?: boolean;
  235. round?: boolean;
  236. major?: number;
  237. minor?: number;
  238. initial?: number;
  239. }
  240. interface SnippetPromptOptions extends BasePromptOptions {
  241. newline?: string;
  242. fields: Partial<BasePromptOptionsWithName>[];
  243. template: string;
  244. }
  245. interface SortPromptOptions extends BasePromptOptions {
  246. hint?: string;
  247. drag?: boolean;
  248. numbered?: boolean;
  249. }
  250. interface SurveyPromptOptions extends ArrayPromptOptions {
  251. scale: BasePromptOptionsWithName[];
  252. margin: [number, number, number, number];
  253. }
  254. interface QuizPromptOptions extends ArrayPromptOptions {
  255. correctChoice: number;
  256. }
  257. interface TogglePromptOptions extends BasePromptOptions {
  258. enabled?: string;
  259. disabled?: string;
  260. }
  261. /** Returns all the prompt options depending on the type selected. */
  262. declare type PromptOptions<T extends boolean = false> = Unionize<{
  263. [K in PromptTypes]-?: T extends true ? {
  264. type: K;
  265. } & PromptOptionsType<K> & {
  266. name: string | (() => string);
  267. } : {
  268. type: K;
  269. } & PromptOptionsType<K>;
  270. }> | ({
  271. type: string;
  272. } & T extends true ? PromptOptionsType<string> & {
  273. name: string | (() => string);
  274. } : PromptOptionsType<string>);
  275. declare type Unionize<T extends Record<PropertyKey, unknown>> = {
  276. [P in keyof T]: T[P];
  277. }[keyof T];
  278. declare type PromptTypes = 'AutoComplete' | 'BasicAuth' | 'Confirm' | 'Editable' | 'Form' | 'Input' | 'Invisible' | 'List' | 'MultiSelect' | 'Numeral' | 'Password' | 'Quiz' | 'Scale' | 'Select' | 'Snippet' | 'Sort' | 'Survey' | 'Text' | 'Toggle';
  279. declare type PromptOptionsType<T> = T extends keyof PromptOptionsMap ? PromptOptionsMap[T] : T extends string ? BasePromptOptions & Record<PropertyKey, unknown> : any;
  280. declare class PromptOptionsMap implements Record<PromptTypes, Record<PropertyKey, any>> {
  281. AutoComplete: ArrayPromptOptions;
  282. BasicAuth: StringPromptOptions;
  283. Confirm: BooleanPromptOptions;
  284. Editable: ArrayPromptOptions;
  285. Form: ArrayPromptOptions;
  286. Input: StringPromptOptions;
  287. Invisible: StringPromptOptions;
  288. List: ArrayPromptOptions;
  289. MultiSelect: ArrayPromptOptions;
  290. Numeral: NumberPromptOptions;
  291. Password: StringPromptOptions;
  292. Quiz: QuizPromptOptions;
  293. Scale: ScalePromptOptions;
  294. Select: ArrayPromptOptions;
  295. Snippet: SnippetPromptOptions;
  296. Sort: SortPromptOptions;
  297. Survey: SurveyPromptOptions;
  298. Text: StringPromptOptions;
  299. Toggle: TogglePromptOptions;
  300. }
  301. interface PromptSettings {
  302. error?: boolean;
  303. cancelCallback?: (settings?: PromptSettings) => string | Error | PromptError | void;
  304. stdout?: WriteStream | Writable;
  305. enquirer?: Enquirer;
  306. }
  307. interface PromptInstance extends Omit<BasePromptOptions, 'onCancel' | 'onSubmit'> {
  308. submit: () => void;
  309. cancel: (err?: string) => void;
  310. }
  311. /**
  312. * Extend the task to have more functionality while accesing from the outside.
  313. */
  314. declare class TaskWrapper<Ctx, Renderer extends ListrRendererFactory> {
  315. task: Task<Ctx, ListrRendererFactory>;
  316. errors: ListrError<Ctx>[];
  317. private options;
  318. constructor(task: Task<Ctx, ListrRendererFactory>, errors: ListrError<Ctx>[], options: ListrBaseClassOptions<Ctx, any, any>);
  319. /** Get the title of the current task. */
  320. get title(): string;
  321. /** Change the title of the current task. */
  322. set title(data: string);
  323. /** Get the output from the output channel. */
  324. get output(): string;
  325. /** Send a output to the output channel. */
  326. set output(data: string);
  327. /** Create a new subtask with given renderer selection from the parent task. */
  328. newListr<NewCtx = Ctx>(task: ListrTask<NewCtx, Renderer> | ListrTask<NewCtx, Renderer>[] | ((parent: Omit<this, 'skip' | 'enabled'>) => ListrTask<NewCtx, Renderer> | ListrTask<NewCtx, Renderer>[]), options?: ListrSubClassOptions<NewCtx, Renderer>): Listr<NewCtx, any, any>;
  329. /** Report a error in process for error collection. */
  330. report(error: Error, type: ListrErrorTypes): void;
  331. /** Skip current task. */
  332. skip(message?: string): void;
  333. /** Get the number of retrying, else returns false */
  334. isRetrying(): Task<Ctx, Renderer>['retry'];
  335. /**
  336. * Create a new Enquirer prompt using prompt options.
  337. *
  338. * Since process.stdout is controlled by Listr, this will passthrough all Enquirer data through internal stdout.
  339. */
  340. prompt<T = any>(options: PromptOptions | PromptOptions<true>[]): Promise<T>;
  341. /** Cancels the current prompt attach to this task. */
  342. cancelPrompt(throwError?: boolean): void;
  343. /**
  344. * Pass stream of data to internal stdout.
  345. *
  346. * Since Listr2 takes control of process.stdout utilizing the default renderer, any data outputted to process.stdout
  347. * will corupt its looks.
  348. *
  349. * This returns a fake stream to pass any stream inside Listr as task data.
  350. */
  351. stdout(): NodeJS.WriteStream & NodeJS.WritableStream;
  352. /** Run this task. */
  353. run(ctx: Ctx): Promise<void>;
  354. }
  355. /** Available task states. */
  356. declare enum ListrTaskState {
  357. PENDING = "PENDING",
  358. COMPLETED = "COMPLETED",
  359. FAILED = "FAILED",
  360. SKIPPED = "SKIPPED",
  361. ROLLING_BACK = "ROLLING_BACK",
  362. ROLLED_BACK = "ROLLED_BACK",
  363. RETRY = "RETRY"
  364. }
  365. /**
  366. * Create a task from the given set of variables and make it runnable.
  367. */
  368. declare class Task<Ctx, Renderer extends ListrRendererFactory> extends Subject<ListrEvent> {
  369. listr: Listr<Ctx, any, any>;
  370. tasks: ListrTask<Ctx, any>;
  371. options: ListrOptions;
  372. rendererOptions: ListrGetRendererOptions<Renderer>;
  373. /** Unique id per task, randomly generated in the uuid v4 format */
  374. id: string;
  375. /** The current state of the task. */
  376. state: string;
  377. /** The task object itself, to further utilize it. */
  378. task: (ctx: Ctx, task: TaskWrapper<Ctx, Renderer>) => void | ListrTaskResult<Ctx>;
  379. /** Extend current task with multiple subtasks. */
  380. subtasks: Task<Ctx, any>[];
  381. /** Title of the task */
  382. title?: string;
  383. /** Untouched unchanged title of the task */
  384. initialTitle?: string;
  385. /** Output data from the task. */
  386. output?: string;
  387. /** Skip current task. */
  388. skip: boolean | string | ((ctx: Ctx) => boolean | string | Promise<boolean | string>);
  389. /** Current retry number of the task if retrying */
  390. retry?: {
  391. count: number;
  392. withError?: any;
  393. };
  394. /**
  395. * A channel for messages.
  396. *
  397. * This requires a separate channel for messages like error, skip or runtime messages to further utilize in the renderers.
  398. */
  399. message: {
  400. /** Run time of the task, if it has been successfully resolved. */
  401. duration?: number;
  402. /** Error message of the task, if it has been failed. */
  403. error?: string;
  404. /** Skip message of the task, if it has been skipped. */
  405. skip?: string;
  406. /** Rollback message of the task, if the rollback finishes */
  407. rollback?: string;
  408. /** Retry messages */
  409. retry?: {
  410. count: number;
  411. withError?: any;
  412. };
  413. };
  414. /** Per task options for the current renderer of the task. */
  415. rendererTaskOptions: ListrGetRendererTaskOptions<Renderer>;
  416. /** This will be triggered each time a new render should happen. */
  417. renderHook$: Subject<void>;
  418. prompt: undefined | PromptInstance | PromptError;
  419. private enabled;
  420. private enabledFn;
  421. constructor(listr: Listr<Ctx, any, any>, tasks: ListrTask<Ctx, any>, options: ListrOptions, rendererOptions: ListrGetRendererOptions<Renderer>);
  422. set state$(state: ListrTaskState);
  423. set output$(data: string);
  424. set message$(data: Task<Ctx, Renderer>['message']);
  425. set title$(title: string);
  426. /**
  427. * A function to check whether this task should run at all via enable.
  428. */
  429. check(ctx: Ctx): Promise<void>;
  430. /** Returns whether this task has subtasks. */
  431. hasSubtasks(): boolean;
  432. /** Returns whether this task is in progress. */
  433. isPending(): boolean;
  434. /** Returns whether this task is skipped. */
  435. isSkipped(): boolean;
  436. /** Returns whether this task has been completed. */
  437. isCompleted(): boolean;
  438. /** Returns whether this task has been failed. */
  439. hasFailed(): boolean;
  440. /** Returns whether this task has an active rollback task going on. */
  441. isRollingBack(): boolean;
  442. /** Returns whether the rollback action was successful. */
  443. hasRolledBack(): boolean;
  444. /** Returns whether this task has an actively retrying task going on. */
  445. isRetrying(): boolean;
  446. /** Returns whether enabled function resolves to true. */
  447. isEnabled(): boolean;
  448. /** Returns whether this task actually has a title. */
  449. hasTitle(): boolean;
  450. /** Returns whether this task has a prompt inside. */
  451. isPrompt(): boolean;
  452. /** Run the current task. */
  453. run(context: Ctx, wrapper: TaskWrapper<Ctx, Renderer>): Promise<void>;
  454. }
  455. /** Default updating renderer for Listr2 */
  456. declare class DefaultRenderer implements ListrRenderer {
  457. tasks: Task<any, typeof DefaultRenderer>[];
  458. options: typeof DefaultRenderer['rendererOptions'];
  459. renderHook$?: Task<any, any>['renderHook$'];
  460. /** designates whether this renderer can output to a non-tty console */
  461. static nonTTY: boolean;
  462. /** renderer options for the defauult renderer */
  463. static rendererOptions: {
  464. /**
  465. * indentation per level of subtask
  466. *
  467. * @default 2
  468. */
  469. indentation?: number;
  470. /**
  471. * clear all the output generated by the renderer when the task finishes its execution
  472. *
  473. * @default false
  474. * @global global option that can not be temperated with subtasks
  475. */
  476. clearOutput?: boolean;
  477. /**
  478. * show the subtasks of the current task
  479. *
  480. * @default true
  481. */
  482. showSubtasks?: boolean;
  483. /**
  484. * collapse subtasks after current task completes its execution
  485. *
  486. * @default true
  487. */
  488. collapse?: boolean;
  489. /**
  490. * show skip messages or show the original title of the task, this will also disable collapseSkips mode
  491. *
  492. * You can disable showing the skip messages, even though you passed in a message by settings this option,
  493. * if you want to keep the original task title intact.
  494. *
  495. * @default true
  496. */
  497. showSkipMessage?: boolean;
  498. /**
  499. * collapse skip messages into a single message and overwrite the task title
  500. *
  501. * @default true
  502. */
  503. collapseSkips?: boolean;
  504. /**
  505. * suffix skip messages with [SKIPPED] when in collapseSkips mode
  506. *
  507. * @default true
  508. */
  509. suffixSkips?: boolean;
  510. /**
  511. * shows the thrown error message or show the original title of the task, this will also disable collapseErrors mode
  512. * You can disable showing the error messages, even though you passed in a message by settings this option,
  513. * if you want to keep the original task title intact.
  514. *
  515. * @default true
  516. */
  517. showErrorMessage?: boolean;
  518. /**
  519. * collapse error messages into a single message and overwrite the task title
  520. *
  521. * @default true
  522. */
  523. collapseErrors?: boolean;
  524. /**
  525. * suffix retry messages with [RETRY-${COUNT}] when retry is enabled for a task
  526. *
  527. * @default true
  528. */
  529. suffixRetries?: boolean;
  530. /**
  531. * only update through triggers from renderhook
  532. *
  533. * useful for tests and stuff. this will disable showing spinner and only update the screen if something else has
  534. * happened in the task worthy to show
  535. *
  536. * @default false
  537. * @global global option that can not be temperated with subtasks
  538. */
  539. lazy?: boolean;
  540. /**
  541. * show duration for all tasks
  542. *
  543. * @default false
  544. * @global global option that can not be temperated with subtasks
  545. */
  546. showTimer?: boolean;
  547. /**
  548. * removes empty lines from the data output
  549. *
  550. * @default true
  551. */
  552. removeEmptyLines?: boolean;
  553. /**
  554. * formats data output depending on your requirements.
  555. *
  556. * @default 'truncate'
  557. * @global global option that can not be temperated with subtasks
  558. */
  559. formatOutput?: 'truncate' | 'wrap';
  560. };
  561. /** per task options for the default renderer */
  562. static rendererTaskOptions: {
  563. /**
  564. * write task output to the bottom bar instead of the gap under the task title itself.
  565. * useful for a stream of data.
  566. * @default false
  567. *
  568. * `true` only keep 1 line of the latest data outputted by the task.
  569. * `false` only keep 1 line of the latest data outputted by the task.
  570. * `number` will keep designated data of the latest data outputted by the task.
  571. */
  572. bottomBar?: boolean | number;
  573. /**
  574. * keep output after task finishes
  575. * @default false
  576. *
  577. * works both for the bottom bar and the default behavior
  578. */
  579. persistentOutput?: boolean;
  580. /**
  581. * show the task time if it was successful
  582. */
  583. showTimer?: boolean;
  584. };
  585. private id?;
  586. private bottomBar;
  587. private promptBar;
  588. private readonly spinner;
  589. private spinnerPosition;
  590. constructor(tasks: Task<any, typeof DefaultRenderer>[], options: typeof DefaultRenderer['rendererOptions'], renderHook$?: Task<any, any>['renderHook$']);
  591. getTaskOptions(task: Task<any, typeof DefaultRenderer>): typeof DefaultRenderer['rendererTaskOptions'];
  592. isBottomBar(task: Task<any, typeof DefaultRenderer>): boolean;
  593. hasPersistentOutput(task: Task<any, typeof DefaultRenderer>): boolean;
  594. hasTimer(task: Task<any, typeof DefaultRenderer>): boolean;
  595. getSelfOrParentOption<T extends keyof typeof DefaultRenderer['rendererOptions']>(task: Task<any, typeof DefaultRenderer>, key: T): typeof DefaultRenderer['rendererOptions'][T];
  596. getTaskTime(task: Task<any, typeof DefaultRenderer>): string;
  597. createRender(options?: {
  598. tasks?: boolean;
  599. bottomBar?: boolean;
  600. prompt?: boolean;
  601. }): string;
  602. render(): void;
  603. end(): void;
  604. private multiLineRenderer;
  605. private renderBottomBar;
  606. private renderPrompt;
  607. private dumpData;
  608. private formatString;
  609. private indentMultilineOutput;
  610. private getSymbol;
  611. private addSuffixToMessage;
  612. }
  613. declare class SilentRenderer implements ListrRenderer {
  614. tasks: Task<any, typeof SilentRenderer>[];
  615. options: typeof SilentRenderer['rendererOptions'];
  616. /** designates whether this renderer can output to a non-tty console */
  617. static nonTTY: boolean;
  618. /** renderer options for the silent renderer */
  619. static rendererOptions: never;
  620. /** per task options for the silent renderer */
  621. static rendererTaskOptions: never;
  622. constructor(tasks: Task<any, typeof SilentRenderer>[], options: typeof SilentRenderer['rendererOptions']);
  623. render(): void;
  624. end(): void;
  625. }
  626. /**
  627. * This is the default renderer which is neither verbose or updating.
  628. * It provides short output like update renderer, but does not disturb
  629. * stdin during execution of listr tasks
  630. */
  631. declare class SimpleRenderer implements ListrRenderer {
  632. readonly tasks: Task<any, typeof SimpleRenderer>[];
  633. options: typeof SimpleRenderer['rendererOptions'];
  634. static nonTTY: boolean;
  635. static rendererOptions: {
  636. /**
  637. * if true this will add
  638. * timestamp at the begin of the rendered line
  639. *
  640. * @example
  641. *
  642. * ```bash
  643. * [12:33:44] ✔ Do something important
  644. * ```
  645. *
  646. * @default false
  647. */
  648. prefixWithTimestamp?: boolean;
  649. /**
  650. * choose between process.stdout and process.stderr
  651. *
  652. * @default stdout
  653. */
  654. output?: 'stdout' | 'stderr';
  655. };
  656. static rendererTaskOptions: never;
  657. /**
  658. * Event type renderer map contains functions to process different task events
  659. */
  660. eventTypeRendererMap: Partial<{
  661. [P in ListrEventType]: (t: Task<any, typeof SimpleRenderer>, event: ListrEventFromType<P>) => void;
  662. }>;
  663. constructor(tasks: Task<any, typeof SimpleRenderer>[], options: typeof SimpleRenderer['rendererOptions']);
  664. static now(): Date;
  665. static formatTitle(task?: Task<any, typeof SimpleRenderer>): string;
  666. log(output?: string): void;
  667. end(): void;
  668. render(tasks?: Task<any, typeof SimpleRenderer>[]): void;
  669. }
  670. /** Default loglevels for the logger */
  671. declare enum LogLevels {
  672. SILENT = "SILENT",
  673. FAILED = "FAILED",
  674. SKIPPED = "SKIPPED",
  675. SUCCESS = "SUCCESS",
  676. DATA = "DATA",
  677. STARTED = "STARTED",
  678. TITLE = "TITLE",
  679. RETRY = "RETRY",
  680. ROLLBACK = "ROLLBACK"
  681. }
  682. /**
  683. * Options for the logger
  684. */
  685. interface LoggerOptions {
  686. useIcons: boolean;
  687. }
  688. /**
  689. * A internal logger for using in the verbose renderer mostly.
  690. */
  691. declare class Logger {
  692. private options?;
  693. constructor(options?: LoggerOptions);
  694. fail(message: string): void;
  695. skip(message: string): void;
  696. success(message: string): void;
  697. data(message: string): void;
  698. start(message: string): void;
  699. title(message: string): void;
  700. retry(message: string): void;
  701. rollback(message: string): void;
  702. protected parseMessage(level: LogLevels, message: string): string;
  703. protected logColoring({ level, message }: {
  704. level: LogLevels;
  705. message: string;
  706. }): string;
  707. private wrapInBrackets;
  708. }
  709. declare class VerboseRenderer implements ListrRenderer {
  710. tasks: Task<any, typeof VerboseRenderer>[];
  711. options: typeof VerboseRenderer['rendererOptions'];
  712. /** designates whether this renderer can output to a non-tty console */
  713. static nonTTY: boolean;
  714. /** renderer options for the verbose renderer */
  715. static rendererOptions: ({
  716. /**
  717. * useIcons instead of text for log level
  718. * @default false
  719. */
  720. useIcons?: boolean;
  721. /**
  722. * log tasks with empty titles
  723. * @default true
  724. */
  725. logEmptyTitle?: boolean;
  726. /**
  727. * log title changes
  728. * @default true
  729. */
  730. logTitleChange?: boolean;
  731. /**
  732. * show duration for all tasks
  733. */
  734. showTimer?: boolean;
  735. } & {
  736. /**
  737. * inject a custom logger
  738. */
  739. logger?: new (...args: any) => Logger;
  740. /**
  741. * inject options to custom logger
  742. */
  743. options?: any;
  744. });
  745. /** per task options for the verbose renderer */
  746. static rendererTaskOptions: never;
  747. private logger;
  748. constructor(tasks: Task<any, typeof VerboseRenderer>[], options: typeof VerboseRenderer['rendererOptions']);
  749. render(): void;
  750. end(): void;
  751. private verboseRenderer;
  752. }
  753. /** The default renderer value used in Listr2 applications */
  754. declare type ListrDefaultRendererValue = 'default';
  755. /** Type of default renderer */
  756. declare type ListrDefaultRenderer = typeof DefaultRenderer;
  757. /** Name of default fallback renderer */
  758. declare type ListrFallbackRendererValue = 'verbose';
  759. /** Type of default fallback renderer */
  760. declare type ListrFallbackRenderer = typeof VerboseRenderer;
  761. /** Silent rendere for internal usage */
  762. declare type ListrSilentRendererValue = 'silent';
  763. /** Typeof silent renderer */
  764. declare type ListrSilentRenderer = typeof SilentRenderer;
  765. /** Simple renderer that simplifies things */
  766. declare type ListrSimpleRendererValue = 'simple';
  767. /** Typeof simple renderer */
  768. declare type ListrSimpleRenderer = typeof SimpleRenderer;
  769. /**
  770. * Listr2 can process either the integrated renderers as string aliases,
  771. * or utilize a compatible style renderer that extends the ListrRenderer abstract class.
  772. */
  773. declare type ListrRendererValue = ListrSilentRendererValue | ListrDefaultRendererValue | ListrSimpleRendererValue | ListrFallbackRendererValue | ListrRendererFactory;
  774. /**
  775. * Returns the class type from friendly names of the renderers.
  776. */
  777. declare type ListrGetRendererClassFromValue<T extends ListrRendererValue> = T extends ListrDefaultRendererValue ? ListrDefaultRenderer : T extends ListrSimpleRendererValue ? ListrSimpleRenderer : T extends ListrFallbackRendererValue ? ListrFallbackRenderer : T extends ListrSilentRenderer ? ListrSilentRenderer : T extends ListrRendererFactory ? T : never;
  778. /**
  779. * Returns the friendly names from the type of renderer classes.
  780. */
  781. declare type ListrGetRendererValueFromClass<T extends ListrRendererFactory> = T extends DefaultRenderer ? ListrDefaultRendererValue : T extends SimpleRenderer ? ListrSimpleRendererValue : T extends VerboseRenderer ? ListrFallbackRendererValue : T extends SilentRenderer ? ListrSilentRenderer : T extends ListrRendererFactory ? T : never;
  782. /**
  783. * Returns renderer global options depending on the renderer type.
  784. */
  785. declare type ListrGetRendererOptions<T extends ListrRendererValue> = T extends ListrDefaultRendererValue ? ListrDefaultRenderer['rendererOptions'] : T extends ListrSimpleRendererValue ? ListrSimpleRenderer['rendererOptions'] : T extends ListrFallbackRendererValue ? ListrFallbackRenderer['rendererOptions'] : T extends ListrSilentRenderer ? ListrSilentRenderer['rendererOptions'] : T extends ListrRendererFactory ? T['rendererOptions'] : never;
  786. /**
  787. * Returns renderer per task options depending on the renderer type.
  788. */
  789. declare type ListrGetRendererTaskOptions<T extends ListrRendererValue> = T extends ListrDefaultRendererValue ? ListrDefaultRenderer['rendererTaskOptions'] : T extends ListrSimpleRendererValue ? ListrSimpleRenderer : T extends ListrFallbackRendererValue ? ListrFallbackRenderer['rendererTaskOptions'] : T extends ListrSilentRenderer ? ListrSilentRenderer['rendererTaskOptions'] : T extends ListrRendererFactory ? T['rendererTaskOptions'] : never;
  790. /** Select renderer as default renderer */
  791. interface ListrDefaultRendererOptions<T extends ListrRendererValue> {
  792. /** the default renderer */
  793. renderer?: T;
  794. /** Renderer options depending on the current renderer */
  795. rendererOptions?: ListrGetRendererOptions<T>;
  796. }
  797. /** Select a fallback renderer to fallback to in non-tty conditions */
  798. interface ListrDefaultNonTTYRendererOptions<T extends ListrRendererValue> {
  799. /** the fallback renderer to fallback to on non-tty conditions */
  800. nonTTYRenderer?: T;
  801. /** Renderer options depending on the current renderer */
  802. nonTTYRendererOptions?: ListrGetRendererOptions<T>;
  803. }
  804. /** Renderer options for the base class, including setup for selecting default and fallback renderers. */
  805. declare type ListrRendererOptions<Renderer extends ListrRendererValue, FallbackRenderer extends ListrRendererValue> = ListrDefaultRendererOptions<Renderer> & ListrDefaultNonTTYRendererOptions<FallbackRenderer>;
  806. /** The bones of a listr renderer. */
  807. declare class ListrRenderer {
  808. /** designate renderer global options that is specific to the current renderer */
  809. static rendererOptions: Record<PropertyKey, any>;
  810. /** designate renderer per task options that is specific to the current renderer */
  811. static rendererTaskOptions: Record<PropertyKey, any>;
  812. /** designate whether this renderer can work in non-tty environments */
  813. static nonTTY: boolean;
  814. /** A function to what to do on render */
  815. render: () => void;
  816. /** A function to what to do on end of the render */
  817. end: (err?: Error) => void;
  818. /** create a new renderer */
  819. constructor(tasks: readonly Task<any, ListrRendererFactory>[], options: typeof ListrRenderer.rendererOptions, renderHook$?: Subject<void>);
  820. }
  821. /** Exported for javascript applications to extend the base renderer */
  822. declare class ListrBaseRenderer implements ListrRenderer {
  823. static rendererOptions: Record<PropertyKey, any>;
  824. static rendererTaskOptions: Record<PropertyKey, any>;
  825. static nonTTY: boolean;
  826. tasks: Task<any, typeof ListrBaseRenderer>[];
  827. options: typeof ListrBaseRenderer.rendererOptions;
  828. render: () => void;
  829. end: (err?: Error) => void;
  830. constructor(tasks: Task<any, typeof ListrBaseRenderer>[], options: typeof ListrBaseRenderer.rendererOptions);
  831. }
  832. /** A renderer factory from the current type */
  833. declare type ListrRendererFactory = typeof ListrRenderer;
  834. /** Supported type of renderers for each type in the listr. */
  835. interface SupportedRenderer {
  836. renderer: ListrRendererFactory;
  837. nonTTY: boolean;
  838. }
  839. /** The internal error handling mechanism.. */
  840. declare class ListrError<Ctx extends Record<PropertyKey, any> = Record<PropertyKey, any>> extends Error {
  841. error: Error;
  842. type: ListrErrorTypes;
  843. task: Task<Ctx, ListrRendererFactory>;
  844. path: string;
  845. ctx: Ctx;
  846. constructor(error: Error, type: ListrErrorTypes, task: Task<Ctx, ListrRendererFactory>);
  847. }
  848. /**
  849. * The actual error type that is collected and to help identify where the error is triggered from.
  850. */
  851. declare enum ListrErrorTypes {
  852. /** Task has failed and will try to retry. */
  853. WILL_RETRY = "WILL_RETRY",
  854. /** Task has failed and will try to rollback. */
  855. WILL_ROLLBACK = "WILL_ROLLBACK",
  856. /** Task has failed, ran the rollback action but the rollback action itself has failed. */
  857. HAS_FAILED_TO_ROLLBACK = "HAS_FAILED_TO_ROLLBACK",
  858. /** Task has failed. */
  859. HAS_FAILED = "HAS_FAILED",
  860. /** Task has failed, but exitOnError is set to false, so will ignore this error. */
  861. HAS_FAILED_WITHOUT_ERROR = "HAS_FAILED_WITHOUT_ERROR"
  862. }
  863. /** The internal error handling mechanism for prompts only. */
  864. declare class PromptError extends Error {
  865. constructor(message: string);
  866. }
  867. /**
  868. * Creates a new set of Listr2 task list.
  869. */
  870. declare class Listr<Ctx = ListrContext, Renderer extends ListrRendererValue = ListrDefaultRendererValue, FallbackRenderer extends ListrRendererValue = ListrFallbackRendererValue> {
  871. task: ListrTask<Ctx, ListrGetRendererClassFromValue<Renderer>> | ListrTask<Ctx, ListrGetRendererClassFromValue<Renderer>>[];
  872. options?: ListrBaseClassOptions<Ctx, Renderer, FallbackRenderer>;
  873. parentTask?: Task<any, any>;
  874. tasks: Task<Ctx, ListrGetRendererClassFromValue<Renderer>>[];
  875. err: ListrError<Ctx>[];
  876. ctx: Ctx;
  877. rendererClass: ListrRendererFactory;
  878. rendererClassOptions: ListrGetRendererOptions<ListrRendererFactory>;
  879. renderHook$: Task<any, any>['renderHook$'];
  880. path: string[];
  881. private concurrency;
  882. private renderer;
  883. constructor(task: ListrTask<Ctx, ListrGetRendererClassFromValue<Renderer>> | ListrTask<Ctx, ListrGetRendererClassFromValue<Renderer>>[], options?: ListrBaseClassOptions<Ctx, Renderer, FallbackRenderer>, parentTask?: Task<any, any>);
  884. add(task: ListrTask<Ctx, ListrGetRendererClassFromValue<Renderer>> | ListrTask<Ctx, ListrGetRendererClassFromValue<Renderer>>[]): void;
  885. run(context?: Ctx): Promise<Ctx>;
  886. private checkAll;
  887. private runTask;
  888. }
  889. /**
  890. * Creates a new Listr2 task manager.
  891. *
  892. * Useful for creating a single instace of Listr2 with pre-set settings.
  893. */
  894. declare class Manager<Ctx = ListrContext, Renderer extends ListrRendererValue = 'default', FallbackRenderer extends ListrRendererValue = 'verbose'> {
  895. options?: ListrBaseClassOptions<Ctx, Renderer, FallbackRenderer>;
  896. err: ListrError[];
  897. private tasks;
  898. constructor(options?: ListrBaseClassOptions<Ctx, Renderer, FallbackRenderer>);
  899. set ctx(ctx: Ctx);
  900. add<InjectCtx = Ctx>(tasks: ListrTask<InjectCtx, ListrGetRendererClassFromValue<Renderer>>[] | ((ctx?: InjectCtx) => ListrTask<InjectCtx, ListrGetRendererClassFromValue<Renderer>>[]), options?: ListrSubClassOptions<InjectCtx, Renderer>): void;
  901. runAll<InjectCtx = Ctx>(options?: ListrBaseClassOptions<InjectCtx, Renderer, FallbackRenderer>): Promise<InjectCtx>;
  902. newListr<InjectCtx, InjectRenderer extends ListrRendererValue = Renderer, InjectFallbackRenderer extends ListrRendererValue = FallbackRenderer>(tasks: ListrTask<InjectCtx, ListrGetRendererClassFromValue<InjectRenderer>>[], options?: ListrBaseClassOptions<InjectCtx, InjectRenderer, InjectFallbackRenderer>): Listr<InjectCtx, InjectRenderer, InjectFallbackRenderer>;
  903. indent<InjectCtx = Ctx>(tasks: ListrTask<InjectCtx, ListrGetRendererClassFromValue<Renderer>>[] | ((ctx?: InjectCtx) => ListrTask<InjectCtx, ListrGetRendererClassFromValue<Renderer>>[]), options?: ListrBaseClassOptions<InjectCtx, Renderer, FallbackRenderer>, taskOptions?: Omit<ListrTask<InjectCtx, ListrGetRendererClassFromValue<Renderer>>, 'task'>): ListrTask<InjectCtx, ListrGetRendererClassFromValue<Renderer>>;
  904. run<InjectCtx = Ctx>(tasks: ListrTask<InjectCtx, ListrGetRendererClassFromValue<Renderer>>[], options?: ListrBaseClassOptions<InjectCtx, Renderer, FallbackRenderer>): Promise<InjectCtx>;
  905. getRuntime(pipetime: number): string;
  906. }
  907. /**
  908. * Create a new prompt with Enquirer externally.
  909. * This extends enquirer so you dont have to give a name to single prompts and such so it is also
  910. * useful to use externally.
  911. * @param this
  912. * @param options
  913. * @param settings
  914. */
  915. declare function createPrompt(this: any, options: PromptOptions | PromptOptions<true>[], settings?: PromptSettings): Promise<any>;
  916. declare function destroyPrompt(this: TaskWrapper<any, any>, throwError?: boolean): void;
  917. declare const figures: {
  918. warning: string;
  919. cross: string;
  920. arrowDown: string;
  921. tick: string;
  922. arrowRight: string;
  923. pointer: string;
  924. checkboxOn: string;
  925. arrowLeft: string;
  926. squareSmallFilled: string;
  927. pointerSmall: string;
  928. };
  929. export { Listr, ListrBaseClassOptions, ListrBaseRenderer, ListrContext, ListrDefaultNonTTYRendererOptions, ListrDefaultRenderer, ListrDefaultRendererOptions, ListrDefaultRendererValue, ListrError, ListrErrorTypes, ListrEvent, ListrEventFromType, ListrEventType, ListrFallbackRenderer, ListrFallbackRendererValue, ListrGetRendererClassFromValue, ListrGetRendererOptions, ListrGetRendererTaskOptions, ListrGetRendererValueFromClass, ListrOptions, ListrRenderer, ListrRendererFactory, ListrRendererOptions, ListrRendererValue, ListrSilentRenderer, ListrSilentRendererValue, ListrSimpleRenderer, ListrSimpleRendererValue, ListrSubClassOptions, ListrTask, Task as ListrTaskObject, ListrTaskResult, ListrTaskState, TaskWrapper as ListrTaskWrapper, LogLevels, Logger, Manager, PromptError, PromptInstance, PromptOptions, PromptOptionsMap, PromptOptionsType, PromptSettings, PromptTypes, SupportedRenderer, Unionize, createPrompt, destroyPrompt, figures };