javascript - Create yeoman generator with API's calls
问题描述
I’ve created a Yeoman generator which works OK, now I need to extend it with additional two questions.
This questions I already have
async prompting() {
const prompts = [
{
name: "appName",
message: "Project name: ",
type: "input",
default: this.props!.appName,
validate(input: string) {
const appName = validateAppName(input);
return !appName[1] ? appName[0] : true;
},
},
{
type: "list",
name: "tech",
message: "Which tech?”,
default: “cloud”,
choices: [{ name: “cloud”}, { name: “onPrem” }}],
},
},
];
Now I need to add additional questions like on which namespace you want to create the project
{
type: “list”,
name: "namespace",
suggestOnly: false,
message: "which namespace: ",
source: Namespace.searchNS,
when: () => !isEmpty(this.namespace!.namespaceInstances),
validate(val: boolean) {
return val
? true
: "choose a namespace where services are provisioned ";
},
},
And the user should choose a namespace (the trick here that I need to run some logic i.e. rest call to get back the namespace list ) , and for this namespace I need to add another question . i.e for user choosen namespace I need to provide service list.
With service list, something like this.
{
name: "serviceInstanceName",
type: "rawlist",
message:
"Choose a service instance ",
default: this.props!.namespace,
choices: srvInstanceList,
when: () =>
srvInstanceList !== undefined && !isEmpty(srvInstanceList),
},
What I need to do:
- Run an API (rest) call to get the namespace list, show it to the user as list
- When the user choose a specific namespace, I need to do an new rest call to get all the services in this namespace
- User choose service
where should I put the logic of each question and pass it to the next question
解决方案
我没有任何代码示例来支持答案,但如果我是你,我会:
- 运行 API (rest) 调用以获取命名空间列表,将其作为列表显示给用户
- 当用户选择一个特定的命名空间时,我需要做一个新的休息调用来获取这个命名空间中的所有服务
- 用户选择服务
对于与命名空间相关的问题,请使用choices
将加载所有命名空间的属性和返回函数。同样,对于与服务相关的问题,使用choices
属性和返回函数将加载命名空间中的所有服务。
这是关于choices
:
选择:(数组|函数)选择数组或返回选择数组的函数。如果定义为函数,则第一个参数将是当前询问者会话答案。数组值可以是简单的数字、字符串或包含名称(显示在列表中)、值(保存在答案哈希中)和简短(选择后显示)属性的对象。
在该函数中,您将获得第一个参数,其中包含用户对先前问题给出的答案。
此外,您可以使用when
问题中的属性来确定是否应该根据先前的答案提出或跳过该问题。
参考:https ://github.com/SBoudrias/Inquirer.js/blob/master/README.md
推荐阅读
- c# - 在 c# 中使用 open xml sdk 读取特定列的 Excel 单元格值
- android - Unity 设备在经纬度中的位置
- multithreading - 性能下降:分叉代码与。openmp 代码
- javascript - 使用带有 chai 的通用测试功能,测试运行失败
- postgresql - 插入查询表锁
- java - 如何在同一行代码中正确使用 instanceof 和 compareTo?
- angular - Angular 6:运行 ng test 时不加载环境
- java - BinarySearchTree 查找介于某些值之间的最接近的值
- java - 两个代码片段中哪一个更适合用于比较器?
- python - 在 dict 中找到一个值然后获取密钥