# @danielkalen/listr 中文文档

规范地址：https://npmcn.com/package/%40danielkalen/listr/

版本：0\.5\.0
npm 元数据更新时间：2022-06-12T16:51:56.013Z
中文译文更新时间：2026-11-11T10:23:31.867Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40danielkalen/listr
原文对照：https://npmcn.com/package/%40danielkalen/listr/?lang=original
项目主页：https://github.com/samverschueren/listr#readme
源码仓库：https://github.com/samverschueren/listr

## 关键信息（来自 npm 元数据）

- TypeScript 类型：未声明
- Node.js 要求：\>=4
- 运行依赖：11 个
- 首次发布：2016-08-31T18:13:34.843Z
- 最新版本发布：2016-08-31T18:13:34.843Z
- 近 30 天下载量：28430（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：1（近 12 个月 0 个）

## 中文简介

终端任务列表( 修改为当同时出现错误时不停止列表: true)

## 安装

```sh
npm install @danielkalen/listr
```

也可以使用：`pnpm add @danielkalen/listr`、`yarn add @danielkalen/listr`、`bun add @danielkalen/listr`

## README

# 列表 [![Build Status](https://travis-ci.org/SamVerschueren/listr.svg?branch=master)](https://travis-ci.org/SamVerschueren/listr)

> 终端任务列表

![](https://npmcn.com/package/%40danielkalen/listr/media/screenshot.gif)

## 安装

```
$ npm install --save listr
```

## 使用量

```js
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);
});
```

### 任务

A 类 `task` 可以返回不同的值。 如果有的话 `task` 返回,这意味着任务已成功完成。 如果任务出错, 任务失败 .

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

#### 承诺

A 类 `task` 也可以通过返回 a 实现同步 `Promise`。如果承诺解决,任务成功完成,它拒绝,任务失败.

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

#### 可观察

![](https://npmcn.com/package/%40danielkalen/listr/media/observable.gif)

A 类 `task` 也可以返回一个 `Observable`。关于可观测值,它可以释放出多个值,并可用于显示输出 任务。 请注意,只提供了产出的最后一行.

```js
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'))
    }
]);
```

#### 流线

也有可能还给 `stream`。溪流将转换为 `Observable` 并处理成这样.

#### 跳过任务

![](https://npmcn.com/package/%40danielkalen/listr/media/skipped.png)

可选择指定 a `skip` 函数来确定是否可以跳过一个任务.

-   如果 `skip` 函数返回真实值或 `Promise` 然后任务就会被跳过.
-   如果返回的值是一个字符串, 它将被显示为跳过任务的原因 .
-   如果 `skip` 函数返回假值或假值 `Promise` 以错误的值解决,那么任务将正常地执行.
-   如果 `skip` 函数丢出或返回 a `Promise` 否认真理者,将失败.

```js
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 密码

### 列表(\[任务\],\[选项\])

#### 任务

类型 : `object[]`

任务列表.

##### 标题

类型 : `string`

任务名称.

##### 任务

类型 : `Function`

任务函数 .

##### 跳过

类型 : `Function`

跳过函数 。 更多信息 [跳过任务](https://npmcn.com/package/%40danielkalen/listr/#readme-skipping-tasks)。 。 。 .

#### 选项

##### 显示子任务

类型 : `boolean`  
默认 : `true`

设定为 `false` 如果您想要禁用子任务的渲染。 如果 其中之一发生了错误.

##### 并行

类型 : `boolean`  
默认 : `false`

设定为 `true` 如果您想要同时运行任务.

### 实例

#### 添加( 任务)

返回实例 .

##### 任务

类型 : `object` `object[]`

任务对象或多个任务对象.

#### 运行( c)

开始执行任务 .

## 相关

-   [轨道](https://github.com/sindresorhus/ora) - 精致的终端旋转器
-   [饼干](https://github.com/sindresorhus/cli-spinners) - 用于终端的自旋器

## 许可证

麻省理工学院 [萨姆·弗斯克伦](https://github.com/SamVerschueren)

## 常见问题

### @danielkalen/listr 是什么？

终端任务列表\( 修改为当同时出现错误时不停止列表: 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 和提交记录判断。

