Socket
Socket
Sign inDemoInstall

listr2

Package Overview
Dependencies
67
Maintainers
1
Versions
231
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    listr2

Terminal task list reborn!


Version published
Weekly downloads
15M
increased by0.94%
Maintainers
1
Install size
7.46 MB
Created
Weekly downloads
 

Package description

What is listr2?

The listr2 npm package is a powerful utility for creating beautiful and interactive command-line interfaces (CLI) with tasks lists. It allows developers to manage and execute multiple tasks in a structured and visually appealing way. It supports concurrent tasks, nested tasks, and customizable rendering, making it suitable for complex CLI applications.

What are listr2's main functionalities?

Basic Task Execution

This feature allows for the execution of a series of tasks, each represented by a title and a task function. The tasks are executed sequentially, and the progress is visually represented in the CLI.

const { Listr } = require('listr2');

const tasks = new Listr([
  {
    title: 'Task 1',
    task: () => Promise.resolve('First task result')
  },
  {
    title: 'Task 2',
    task: () => Promise.resolve('Second task result')
  }
]);

tasks.run().catch(err => console.error(err));

Concurrent Tasks

This feature demonstrates how to run tasks concurrently. By setting the 'concurrent' option to true, tasks within the same group are executed in parallel, improving performance for independent tasks.

const { Listr } = require('listr2');

const tasks = new Listr([
  {
    title: 'Concurrent Tasks',
    task: () => {
      return new Listr([
        {
          title: 'Task 1',
          task: () => Promise.resolve('First concurrent task result')
        },
        {
          title: 'Task 2',
          task: () => Promise.resolve('Second concurrent task result')
        }
      ], { concurrent: true });
    }
  }
]);

tasks.run().catch(err => console.error(err));

Nested Tasks

This feature showcases the ability to nest tasks within other tasks. It is useful for structuring complex workflows where tasks have sub-tasks, allowing for better organization and readability.

const { Listr } = require('listr2');

const tasks = new Listr([
  {
    title: 'Parent Task',
    task: () => {
      return new Listr([
        {
          title: 'Nested Task 1',
          task: () => Promise.resolve('First nested task result')
        },
        {
          title: 'Nested Task 2',
          task: () => Promise.resolve('Second nested task result')
        }
      ]);
    }
  }
]);

tasks.run().catch(err => console.error(err));

Other packages similar to listr2

Readme

Source

Listr2

Version Downloads/week Build Status

This is the expanded and re-written in Typescript version of the beautiful plugin by Sam Verschueren called Listr. Fully backwards compatible with the Listr itself but with more features.

Demo

Navigation

How to use

Check out example.ts in the root of the repository for the code in demo.

import { Listr } from 'listr2'

interface ListrCtx {
  injectedContext: boolean
}

const tasks = new Listr<ListrCtx>(
  [
    {
      // a title
      title: 'Hello I am a title',
      // an sync, async or observable function
      task: async (ctx, task): Promise<void> => {}
    }
  ],
  {
    // throw in some options, all have a default
    showSubtasks: true,
    concurrent: false,
    exitOnError: true,
    bottomBarItems: 3,
    ctx: someOtherContextObject
  }
)

// and done!
const ctx = await tasks.run()

Extra Features

Task Manager

I have added a task manager kind of thing that is utilizing the Listr to the mix since I usually use it this kind of way. This task manager consists of three execution steps as described below.

import { Manager } from 'listr2'

This enables user to push the tasks in a queue and execute them when needed. In my opinion this provides a more clean code interface, just to add everything to queue and execute at the end of the script, thats why I included it.

Create A New Task Manager

  • You can inject your type through the initial generation.
  • The only option for manager is the show run time, which shows the run time off only the middle async part.
private manager: Manager<Ctx> = new Manager<Ctx>()

Initial Tasks

  • Initial tasks will be executed in order at the beginning.
  • You can also inject your type through add initial interface if initiating it via defining a type is not possible. manager.addInitial<Ctx>([])
manager.addInitial([])

Parallel Tasks

  • This tasks will be run as async.
  • If showRuntime option enabled while generating manager, it will show the run time of async part.
  • You can also inject your type through add initial interface if initiating it via defining a type is not possible. manager.add<Ctx>([])
manager.add([])

Final Tasks

  • Final tasks will be executed in order at the end.
  • You can also inject your type through add initial interface if initiating it via defining a type is not possible. manager.addFinal<Ctx>([])
manager.addFinal([])

Run Tasks that are in Queue

  • This will also return the context object back.
const ctx = await manager.runAll()

Input Module

Input module uses the beautiful enquirer. So with running a task.prompt function, you first select which kind of prompt that you will use and second one is the enquirer object which you can see more in detail in the designated npm page.

To get a input you can assign the task a new prompt in an async function and write the response to the context. It is not advisable to run prompts in a concurrent task because they will class and overwrite each others console output and when you do keyboard movements it will apply to the both.

Prompts can either have a title or not but they will always be rendered at the end of the current console while using the default renderer.

new Listr<ListrCtx>([
    {
      task: async (ctx, task): Promise<any> => ctx.testInput = await task.prompt('Input', { message: 'test' })
    },
    {
      title: 'Dump prompt.',
      task: (ctx,task): void => {
        task.output = ctx.testInput
      }
    }
  ])

Inject Context

Context which is the object that is being used while executing the actions in the Listr can now be enjected to the next Listr through using the custom options.

Bottom Bar For More Information

Default renderer now supports a bottom bar to dump the output if desired. The output lenght can be limited through options of the Listr class.

Tasks without Titles

Tasks can be created without titles, and if any output is dumped it will be dumped to the bottom bar instead. If a task with no title is returns a new Listr task, it can be used to change the parallel task count and execute those particular tasks in order or in parallel. The subtasks of the untitled tasks will drop down one indentation level to be consistent.

Multi-Line Renderer

The default update renderer now supports multi-line rendering. Therefore implementations like pushing through multi-line data now works properly.

Fully-Typed

You can download the types if you are starting a new Typescript project.

Keywords

FAQs

Last updated on 02 Mar 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc