Skip to main content

Inquirer 中文网

常用交互式命令行用户界面的集合。

¥A collection of common interactive command line user interfaces.

在你自己的终端上尝试一下!

¥Give it a try in your own terminal!

npx @inquirer/demo@latest

安装

¥Installation

npm install @inquirer/prompts

[!NOTE] Inquirer 最近从头开始进行了重写,以减小包大小并提高性能。该包的先前版本仍在维护(尽管没有积极开发),并提供了数百个可能未迁移到最新 API 的社区贡献提示。如果这是你要找的,则使用 上一个包在这里

¥[!NOTE] Inquirer recently underwent a rewrite from the ground up to reduce the package size and improve performance. The previous version of the package is still maintained (though not actively developed), and offered hundreds of community contributed prompts that might not have been migrated to the latest API. If this is what you're looking for, the previous package is over here.

用法

¥Usage

import { input } from '@inquirer/prompts';

const answer = await input({ message: 'Enter your name' });

提示

¥Prompts

输入

¥Input

import { input } from '@inquirer/prompts';

查看文档 用于使用示例和选项文档。

¥See documentation for usage example and options documentation.

选择

¥Select

import { select } from '@inquirer/prompts';

查看文档 用于使用示例和选项文档。

¥See documentation for usage example and options documentation.

复选框

¥Checkbox

import { checkbox } from '@inquirer/prompts';

查看文档 用于使用示例和选项文档。

¥See documentation for usage example and options documentation.

确认

¥Confirm

import { confirm } from '@inquirer/prompts';

查看文档 用于使用示例和选项文档。

¥See documentation for usage example and options documentation.

搜索

¥Search

import { search } from '@inquirer/prompts';

查看文档 用于使用示例和选项文档。

¥See documentation for usage example and options documentation.

密码

¥Password

import { password } from '@inquirer/prompts';

查看文档 用于使用示例和选项文档。

¥See documentation for usage example and options documentation.

展开

¥Expand

import { expand } from '@inquirer/prompts';

查看文档 用于使用示例和选项文档。

¥See documentation for usage example and options documentation.

编辑器

¥Editor

在临时文件上启动用户首选编辑器的实例。一旦用户退出编辑器,临时文件的内容将被读取为答案。使用的编辑器通过读取 $VISUAL 或 $EDITOR 环境变量来确定。如果两者都不存在,则使用操作系统默认值(Windows 上的记事本,Mac 或 Linux 上的 vim。)

¥Launches an instance of the users preferred editor on a temporary file. Once the user exits their editor, the content of the temporary file is read as the answer. The editor used is determined by reading the $VISUAL or $EDITOR environment variables. If neither of those are present, the OS default is used (notepad on Windows, vim on Mac or Linux.)

import { editor } from '@inquirer/prompts';

查看文档 用于使用示例和选项文档。

¥See documentation for usage example and options documentation.

数字

¥Number

input 提示非常相似,但具有内置的数字验证配置选项。

¥Very similar to the input prompt, but with built-in number validation configuration option.

import { number } from '@inquirer/prompts';

查看文档 用于使用示例和选项文档。

¥See documentation for usage example and options documentation.

原始列表

¥Raw List

import { rawlist } from '@inquirer/prompts';

查看文档 用于使用示例和选项文档。

¥See documentation for usage example and options documentation.

创建你自己的提示

¥Create your own prompts

API 文档在这里 和我们的 测试实用程序在这里

¥The API documentation is over here, and our testing utilities here.

高级用法

¥Advanced usage

所有查询器提示都是一个带有 2 个参数的函数。第一个参数是提示配置(每个提示都是唯一的)。第二个是提供上下文或运行时配置。

¥All inquirer prompts are a function taking 2 arguments. The first argument is the prompt configuration (unique to each prompt). The second is providing contextual or runtime configuration.

上下文选项为:

¥The context options are:

属性类型必填描述
inputNodeJS.ReadableStreamnostdin 流(默认为 process.stdin
outputNodeJS.WritableStreamnostdout 流(默认为 process.stdout
clearPromptOnDonebooleanno如果为真,我们将在提示得到回答后清除屏幕
signalAbortSignalno用于异步取消提示的 AbortSignal

示例:

¥Example:

import { confirm } from '@inquirer/prompts';

const allowEmail = await confirm(
{ message: 'Do you allow us to send you email?' },
{
output: new Stream.Writable({
write(chunk, _encoding, next) {
// Do something
next();
},
}),
clearPromptOnDone: true,
},
);

取消提示

¥Canceling prompt

最好使用 AbortControllerAbortSignal 来完成。

¥This can preferably be done with either an AbortController or AbortSignal.

// Example 1: using built-in AbortSignal utilities
import { confirm } from '@inquirer/prompts';

const answer = await confirm({ ... }, { signal: AbortSignal.timeout(5000) });
// Example 1: implementing custom cancellation logic
import { confirm } from '@inquirer/prompts';

const controller = new AbortController();
setTimeout(() => {
controller.abort(); // This will reject the promise
}, 5000);

const answer = await confirm({ ... }, { signal: controller.signal });

或者,所有提示函数都返回一个可取消的 promise。此特殊的 promise 类型具有 cancel 方法,它将取消并清理提示。

¥Alternatively, all prompt functions are returning a cancelable promise. This special promise type has a cancel method that'll cancel and cleanup the prompt.

在调用 cancel 时,答案 promise 将被拒绝。

¥On calling cancel, the answer promise will become rejected.

import { confirm } from '@inquirer/prompts';

const promise = confirm(...); // Warning: for this pattern to work, `await` cannot be used.

promise.cancel();

秘诀

¥Recipes

在对象中获取答案

¥Get answers in an object

当问很多问题时,你可能不想在每个答案中都保留一个变量。在这种情况下,你可以将答案放在对象。

¥When asking many questions, you might not want to keep one variable per answer everywhere. In which case, you can put the answer inside an object.

import { input, confirm } from '@inquirer/prompts';

const answers = {
firstName: await input({ message: "What's your first name?" }),
allowEmail: await confirm({ message: 'Do you allow us to send you email?' }),
};

console.log(answers.firstName);

有条件地提问

¥Ask a question conditionally

也许有些问题取决于其他问题的答案。

¥Maybe some questions depend on some other question's answer.

import { input, confirm } from '@inquirer/prompts';

const allowEmail = await confirm({ message: 'Do you allow us to send you email?' });

let email;
if (allowEmail) {
email = await input({ message: 'What is your email address' });
}

超时后获取默认值

¥Get default value after timeout

import { input } from '@inquirer/prompts';

const answer = await input(
{ message: 'Enter a value (timing out in 5 seconds)' },
{ signal: AbortSignal.timeout(5000) },
).catch((error) => {
if (error.name === 'AbortPromptError') {
return 'Default value';
}

throw error;
});

用作预提交/git 钩子或脚本

¥Using as pre-commit/git hooks, or scripts

默认情况下,从 husky/lint-staged 等工具运行的脚本可能无法在交互式 shell 中运行。在非交互式 shell 中,Inquirer 无法运行,用户无法向进程发送按键事件。

¥By default scripts ran from tools like husky/lint-staged might not run inside an interactive shell. In non-interactive shell, Inquirer cannot run, and users cannot send keypress events to the process.

为了使其工作,你必须确保启动 tty(或 "interactive" 输​​入流)。

¥For it to work, you must make sure you start a tty (or "interactive" input stream.)

如果这些脚本在你的 package.json 中设置,你可以像这样定义流:

¥If those scripts are set within your package.json, you can define the stream like so:

  "precommit": "my-script < /dev/tty"

或者如果在 shell 脚本文件中,你可以这样做:(在 Windows 上,这可能是你唯一的选择)

¥Or if in a shell script file, you'll do it like so: (on Windows that's likely your only option)

#!/bin/sh
exec < /dev/tty

node my-script.js

与 nodemon 一起使用

¥Using with nodemon

当将 inquirer 提示与 nodemon 一起使用时,你需要传递 --no-stdin 标志才能使一切按预期工作。

¥When using inquirer prompts with nodemon, you need to pass the --no-stdin flag for everything to work as expected.

npx nodemon ./packages/demo/demos/password.mjs --no-stdin

请注意,对于大多数人来说,你将能够使用 Node 内置的新监视模式。此模式可与 inquirer 一起开箱即用。

¥Note that for most of you, you'll be able to use the new watch-mode built-in Node. This mode works out of the box with inquirer.

## One of depending on your need
node --watch script.js
node --watch-path=packages/ packages/demo/

等待配置

¥Wait for config

也许某些问题配置需要等待一个值。

¥Maybe some question configuration require to await a value.

import { confirm } from '@inquirer/prompts';

const answer = await confirm({ message: await getMessage() });

社区提示

¥Community prompts

如果你创建了一个很酷的提示,请将 向我们发送 PR 将其添加 添加到下面的列表中!

¥If you created a cool prompt, send us a PR adding it to the list below!

交互式列表提示
使用箭头键 + Enter 或按与选项关联的键选择一个选项。

¥Interactive List Prompt
Select a choice either with arrow keys + Enter or by pressing a key associated with a choice.

? Choose an option:
> Run command (D)
Quit (Q)

操作选择提示
从列表中选择一个项目,然后按下一个键选择要执行的操作。

¥Action Select Prompt
Choose an item from a list and choose an action to take by pressing a key.

? Choose a file Open <O> Edit <E> Delete <X>
❯ image.png
audio.mp3
code.py

表格多选提示
从表格显示中选择多个答案。

¥Table Multiple Prompt
Select multiple answer from a table display.

Choose between choices? (Press <space> to select, <Up and Down> to move rows,
<Left and Right> to move columns)

┌──────────┬───────┬───────┐
│ 1-2 of 2 │ Yes? │ No? |
├──────────┼───────┼───────┤
│ Choice 1 │ [ ◯ ] │ ◯ |
├──────────┼───────┼───────┤
│ Choice 2 │ ◯ │ ◯ |
└──────────┴───────┴───────┘

切换提示
使用切换确认。使用箭头键 + Enter 选择一个选项。

¥Toggle Prompt
Confirm with a toggle. Select a choice with arrow keys + Enter.

? Do you want to continue? no / yes

可排序复选框提示
与内置复选框提示相同,但​​也允许使用 ctrl+up/down 重新排序选项。

¥Sortable Checkbox Prompt
The same as built-in checkbox prompt, but also allowing to reorder choices using ctrl+up/down.

? Which PRs and in what order would you like to merge? (Press <space> to select, <a> to toggle all, <i> to invert selection, <ctrl+up> to move item up, <ctrl+down> to move item down, and <enter> to proceed)
❯ ◯ PR 1
◯ PR 2
◯ PR 3

多选提示

¥Multi Select Prompt

支持多项选择和过滤/搜索的查询器选择。

¥An inquirer select that supports multiple selections and filtering/searching.

? Choose your OS, IDE, PL, etc. (Press <tab> to select/deselect, <backspace> to remove selected
option, <enter> to select option)
>> vue
>[ ] vue
[ ] vuejs
[ ] fuelphp
[ ] venv
[ ] vercel
(Use arrow keys to reveal more options)

文件选择器提示
文件选择器,你可以在目录之间自由导航,选择要允许的文件类型,并且它是完全可定制的。

¥File Selector Prompt
A file selector, you can navigate freely between directories, choose what type of files you want to allow and it is fully customizable.

? Select a file:
/main/path/
├── folder1/
├── folder2/
├── folder3/
├── file1.txt
├── file2.pdf
└── file3.jpg (not allowed)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Use ↑↓ to navigate through the list
Press <esc> to navigate to the parent directory
Press <enter> to select a file or navigate to a directory

许可证

¥License

版权所有 (c) 2023 Simon Boudrias (推特:@vaxilart)
根据 MIT 许可证授权。

¥Copyright (c) 2023 Simon Boudrias (twitter: @vaxilart)
Licensed under the MIT license.