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 properties of the task options even propagate to the per-task setting, these are pretty limited in form of configuration but should be just enough for you to not wrap everything in subtasks to change behavior.

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)
}