简体   繁体   English

使用 API 调用创建自耕农生成器

[英]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.我已经创建了一个可以正常工作的 Yeoman 生成器,现在我需要用另外两个问题来扩展它。

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 ie rest call to get back the namespace list ) , and for this namespace I need to add another question .并且用户应该选择一个命名空间(这里的技巧是我需要运行一些逻辑,即 rest 调用以取回命名空间列表),对于这个命名空间,我需要添加另一个问题。 ie 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:我需要做什么:

  1. Run an API (rest) call to get the namespace list, show it to the user as list运行 API (rest) 调用以获取命名空间列表,并将其作为列表显示给用户
  1. When the user choose a specific namespace, I need to do an new rest call to get all the services in this namespace当用户选择一个特定的命名空间时,我需要做一个新的rest调用来获取这个命名空间中的所有服务
  2. User choose service用户选择服务

where should I put the logic of each question and pass it to the next question我应该把每个问题的逻辑放在哪里并将其传递给下一个问题

I do not have any code sample to back the answer but if I were you I would:我没有任何代码示例来支持答案,但如果我是你,我会:

  1. Run an API (rest) call to get the namespace list, show it to the user as list运行 API (rest) 调用以获取命名空间列表,并将其作为列表显示给用户
  2. When the user choose a specific namespace, I need to do an new rest call to get all the services in this namespace当用户选择一个特定的命名空间时,我需要做一个新的rest调用来获取这个命名空间中的所有服务
  3. User choose service用户选择服务

For namespace related question, use choices property and return function which would load all namespaces.对于命名空间相关的问题,使用将加载所有命名空间的choices属性和返回函数。 Similarly, for service related question, use choices property and return function which would load all services in the namespace.同样,对于与服务相关的问题,使用choices属性和返回函数来加载命名空间中的所有服务。

Here is documentation on choices :这是有关choices文档:

choices : (Array|Function) Choices array or a function returning a choices array.选择:(数组|函数)选择数组或返回选择数组的函数。 If defined as a function, the first parameter will be the current inquirer session answers.如果定义为函数,第一个参数将是当前查询器会话的答案。 Array values can be simple numbers, strings, or objects containing a name (to display in list), a value (to save in the answers hash), and a short (to display after selection) properties.数组值可以是简单的数字、字符串或包含名称(显示在列表中)、值(保存在答案散列中)和简短(选择后显示)属性的对象。

In the function, you will get first argument which will contain answers user have given for previous questions.在该函数中,您将获得第一个参数,其中包含用户对先前问题给出的答案。

Additionally, you can use when property in a question to determine if the question should be asked or skipped based on previous answers.此外,您可以在问题中使用when属性来确定是否应根据先前的答案提出或跳过该问题。

Ref: https://github.com/SBoudrias/Inquirer.js/blob/master/README.md参考: https : //github.com/SBoudrias/Inquirer.js/blob/master/README.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM