Tuesday 22 September 2020

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


from Create yeoman generator with API's calls

No comments:

Post a Comment