Skip to content

Task Options

listr2 can have global or per-task options to change the behavior of how a task or the whole set of tasks in subtask behaves.

Per Listr

Listr task list can be configured how to behave globally by using the second argument of the prototype with given properties.

Per Subtask

This behavior can be further expanded, if the subtask requires a different approach, in this case, these options are generated depending on the current renderer with the given properties.

Naturally, subtasks options are a subset of the general options, since some options are needed to be set only one time, and do not make sense to change per task.

Per Task

Some of the task options propagate down to the per-task level. These per-task options are limited in scope, but usually enough to change a single task's behavior without wrapping it in a subtask.

Adding Task Options

Task options can be added as follows.

ts
import { Listr, PRESET_TIMER } from 'listr2'

interface Ctx {
  /* some variables for internal use */
}

const tasks = new Listr<Ctx>(
  [
    {
      title: 'This task will execute.',
      task: (_, task): Listr<Ctx> =>
        task.newListr(
          [
            {
              title: 'This task will execute.',
              task: async(ctx): Promise<void> => {
                // perform some operations
              }
            },

            {
              title: 'This task will execute.',
              task: async(ctx): Promise<void> => {
                // perform some operations
              },
              exitOnError: false,
              rendererOptions: { timer: PRESET_TIMER },
              fallbackRendererOptions: { timer: PRESET_TIMER }
            }
          ],
          {
            concurrent: true,
            collectErrors: false,
            rendererOptions: { collapseSubtasks: false }
          }
        )
    }
  ],
  {
    exitOnError: true,
    concurrent: false
  }
)

try {
  await tasks.run()
} catch(e) {
  console.error(e)
}