Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. Run an API (rest) call to get the namespace list, show it to the user as list
  1. When the user choose a specific namespace, I need to do an new rest call to get all the services in this namespace
  2. User choose service

where should I put the logic of each question and pass it to the next question

like image 574
JJD Avatar asked Nov 06 '22 05:11

JJD


1 Answers

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
  2. When the user choose a specific namespace, I need to do an new rest call to get all the services in this namespace
  3. User choose service

For namespace related question, use choices property and return function which would load all namespaces. Similarly, for service related question, use choices property and return function which would load all services in the namespace.

Here is documentation on 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.

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

like image 117
Dipen Shah Avatar answered Nov 12 '22 11:11

Dipen Shah