@danielkalen/listr

中文文档
0.5.0发布于 2016/09/01MIT近 30 天下载 2.8万

Terminal task list (modified to not stop list upon errors when concurrent:true)

原始使用文档
文档目录8 节
  1. Install
  2. Usage
  3. Task
  4. API
  5. Listr([tasks], [options])
  6. Instance
  7. Related
  8. License

listr Build Status

Terminal task list

Install

$ npm install --save listr

Usage

const execa = require('execa');
const Listr = require('listr');

const tasks = new Listr([
    {
        title: 'Git',
        task: () => {
            return new Listr([
                {
                    title: 'Checking git status',
                    task: () => execa.stdout('git', ['status', '--porcelain']).then(result => {
                        if (result !== '') {
                            throw new Error('Unclean working tree. Commit or stash changes first.');
                        }
                    })
                },
                {
                    title: 'Checking remote history',
                    task: () => execa.stdout('git', ['rev-list', '--count', '--left-only', '@{u}...HEAD']).then(result => {
                        if (result !== '0') {
                            throw new Error('Remote history differ. Please pull changes.');
                        }
                    })
                }
            ], {concurrent: true});
        }
    },
    {
        title: 'Install package dependencies',
        task: () => execa('npm', ['install'])
    },
    {
        title: 'Run tests',
        task: () => execa('npm', ['test'])
    },
    {
        title: 'Publish package',
        task: () => execa('npm', ['publish'])
    }
]);

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

Task

A task can return different values. If a task returns, it means the task was completed successfully. If a task throws an error, the task failed.

const tasks = new Listr([
    {
        title: 'Success',
        task: () => 'Foo'
    },
    {
        title: 'Failure',
        task: () => {
            throw new Error('Bar')
        }
    }
]);

Promises

A task can also be async by returning a Promise. If the promise resolves, the task completed successfully, it it rejects, the task failed.

const tasks = new Listr([
    {
        title: 'Success',
        task: () => Promise.resolve('Foo')
    },
    {
        title: 'Failure',
        task: () => Promise.reject('Bar')
    }
]);

Observable

A task can also return an Observable. The thing about observables is that it can emit multiple values and can be used to show the output of the task. Please note that only the last line of the output is rendered.

const tasks = new Listr([
    {
        title: 'Success',
        task: () => {
            return new Observable(observer => {
                observer.next('Foo');

                setTimeout(() => {
                    observer.next('Bar');
                }, 2000);

                setTimeout(() => {
                    observer.complete();
                }, 4000);
            });
        }
    },
    {
        title: 'Failure',
        task: () => Promise.reject(new Error('Bar'))
    }
]);

Streams

It's also possible to return a stream. The stream will be converted to an Observable and handled as such.

Skipping tasks

Optionally specify a skip function to determine whether a task can be skipped.

  • If the skip function returns a truthy value or a Promise that resolves to a truthy value then the task will be skipped.
  • If the returned value is a string it will be displayed as the reason for skipping the task.
  • If the skip function returns a falsey value or a Promise that resolves to a falsey value then the task will be executed as normal.
  • If the skip function throws or returns a Promise that rejects, the task (and the whole build) will fail.
const tasks = new Listr([
    {
        title: 'Task 1',
        task: () => Promise.resolve('Foo')
    },
    {
        title: 'Can be skipped',
        skip: () => {
            if (Math.random() > 0.5) {
                return 'Reason for skipping';
            }
        },
        task: () => 'Bar'
    },
    {
        title: 'Task 3',
        task: () => Promise.resolve('Bar')
    }
]);

API

Listr([tasks], [options])

tasks

Type: object[]

List of tasks.

title

Type: string

Title of the task.

task

Type: Function

Task function.

skip

Type: Function

Skip function. Read more about skipping tasks.

options

showSubtasks

Type: boolean
Default: true

Set to false if you want to disable the rendering of the subtasks. Subtasks will be rendered if an error occurred in one of them.

concurrent

Type: boolean
Default: false

Set to true if you want tasks to run concurrently.

Instance

add(task)

Returns the instance.

task

Type: object object[]

Task object or multiple task objects.

run()

Start executing the tasks.

  • ora - Elegant terminal spinner
  • cli-spinners - Spinners for use in the terminal

License

MIT © Sam Verschueren

版本与发布记录

@danielkalen/listr 共发布 1 个版本,首次发布于 2016/09/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.5.0latest2016/09/0110 年前

关于 @danielkalen/listr 的常见问题

@danielkalen/listr 是什么?

Terminal task list (modified to not stop list upon errors when concurrent:true)

如何安装 @danielkalen/listr?

在项目目录运行 npm install @danielkalen/listr,或使用 pnpm add / yarn add / bun add。

@danielkalen/listr 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/danielkalen__listr 是否存在。

@danielkalen/listr 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=4。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@danielkalen/listr 可以商用吗?

@danielkalen/listr 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@danielkalen/listr 的下载量有多少?

根据 npm 官方下载统计,@danielkalen/listr 在 2026/10/11 – 2026/11/09 这 30 天内被下载 28,430 次,日均约 948 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@danielkalen/listr 还在维护吗?

最新版本 0.5.0 发布于 2016/09/01(10 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2016/09/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。