【问题标题】:Automate responses to prompts in an npm/npx command line install自动响应 npm/npx 命令行安装中的提示
【发布时间】:2021-02-12 09:22:21
【问题描述】:

我想通过命令行自动安装Redocly(API 文档生成器)。使用 npx (npx create-openapi-repo) 生成一个 Redocly 项目。安装过程会触发四个提示(用引号显示;后面是我需要输入的答案)。有些在两者之间进行处理。

"Do you already have an OpenAPI/Swagger 3.0 definition for your API? (y/N)" y
"Please specify the path to the OpenAPI definition (local file):" test.yml
"API Name:" My API
"The following folders will be created: openapi and docs. You can change them by running create-openapi-repo Proceed? (Y/n)" Y

问题在于,似乎启用脚本文件自动完成的命令并没有通过 Redocly npx 安装启用它,至少通过我的尝试。

失败的尝试,除其他外,包括以下(Windows 命令提示符中的一个,Bash 中的其余部分)。有些只回答了四个提示中的三个,因为我很少通过第二个。

尝试

cmd.cmd:

npx create-openapi-repo

命令提示符(Windows 命令提示符):

C:\Source\api-docs>(echo y && echo test.yml) | cmd.cmd

命令提示符结果:


C:\Source\api-docs>npx create-openapi-repo
Welcome to the OpenAPI-Repo generator!
? Do you already have an OpenAPI/Swagger 3.0 definition for your API? Yes
test.yml
? Please specify the path to the OpenAPI definition (local file):
<ends>

尝试

cmd.sh:

read -n 1 -p "Do you already have an OpenAPI/Swagger 3.0 definition for your API? (y/N)" Y
read -n 1 -p "Please specify the path to the OpenAPI definition (local file):" test.yml
read -n 1 -p "API Name:" My API

命令提示符(Bash):

$ ./cmd.sh

命令提示符结果:

Welcome to the OpenAPI-Repo generator!
? Do you already have an OpenAPI/Swagger 3.0 definition for your API? Yes
? Please specify the path to the OpenAPI definition (local file):

尝试

命令提示符 (Bash)

printf 'y\test.yml\My API' | npx create-openapi-repo 

结果同上。

尝试

printf '%s\n' y test.yml 'My API' | npx create-openapi-repo 

结果同上。

尝试

cmd.txt:

y
test.yml
My API

命令提示符(Bash):

npx create-openapi-repo < cmd.txt

结果同上。

有人知道如何做到这一点吗?如果重要,目标是在 Azure DevOps 管道中执行 Redocly 的部署。该管道提供了在 Bash、Windows 命令提示符或 PowerShell 中运行命令的能力。

【问题讨论】:

  • 查看expect
  • create-openapi-repo co-author here:我们绝对可以添加对传递 JSON 配置和答案的支持。您能否在此处打开一个问题并描述您的用例:github.com/Redocly/create-openapi-repo/issues

标签: bash shell command-line scripting command


【解决方案1】:

第一个选项是按照 Shawn 的建议使用 expect

如果您确定两次提示之间的时间(例如,少于 10 秒),您可以尝试以下操作:

#!/usr/bin/env bash

{
echo y
sleep 10
echo test.yml
sleep 10
echo My API
} | npx create-openapi-repo 

【讨论】:

    猜你喜欢
    • 2019-05-28
    • 2018-12-03
    • 1970-01-01
    • 2013-03-23
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多