async questions()

in src/generator/index.ts [81:168]


  async questions() {
    if (process.env.TEST_QUESTIONS) {
      return JSON.parse(process.env.TEST_QUESTIONS);
    }

    const url = await new Input({
      name: 'url',
      message: 'Enter Elastic Kibana URL or Cloud ID',
      required: true,
      validate(value) {
        try {
          if (!IS_URL.test(value)) {
            value = cloudIDToKibanaURL(value);
          }
          new URL(value);
          return true;
        } catch (e) {
          return 'Invalid URL or Cloud ID';
        }
      },
      result(value: string) {
        if (!IS_URL.test(value)) {
          value = cloudIDToKibanaURL(value);
        }
        return value;
      },
    }).run();

    const auth = await new Password({
      name: 'auth',
      header: yellow(
        `Generate API key from Kibana ${getProjectApiKeyURL(url)}`
      ),
      required: true,
      message: 'What is your API key',
    }).run();

    const allLocations = await getLocations({ url, auth });
    const { allLocations: locChoices } = formatLocations(allLocations);

    if (locChoices.length === 0) {
      throw 'Follow the docs to set up your first private locations - https://www.elastic.co/guide/en/observability/current/uptime-set-up-choose-agent.html#private-locations';
    }

    const monitorQues = [
      {
        type: 'select',
        name: 'locations',
        hint: '(Use <space> to select, <return> to submit)',
        message: 'Select the locations where you want to run monitors',
        choices: locChoices,
        multiple: true,
        validate(value) {
          return value.length === 0 ? `Select at least one option.` : true;
        },
      },
      {
        type: 'select',
        name: 'schedule',
        message: 'Set default schedule in minutes for all monitors',
        initial: '10', // set default schedule to 10 minutes
        choices: ALLOWED_SCHEDULES.map(String),
        required: true,
      },
      {
        type: 'input',
        name: 'id',
        message: 'Choose project id to logically group monitors',
        initial: basename(this.projectDir),
      },
      {
        type: 'input',
        name: 'space',
        message: 'Choose the target Kibana space',
        initial: 'default',
      },
    ];

    // Split and group private and public locations from the answered list.
    const answers = await prompt<PromptOptions>(monitorQues);
    // Casting the schedule value via the result() prompt option from enquirer
    // causes a misbehaviour in certain circumstances. That's why we perform
    // the casting here. Please see https://github.com/elastic/synthetics/pull/771#issuecomment-1551519148 for more context
    answers.schedule = Number(answers.schedule);

    const { locations, privateLocations } = groupLocations(answers.locations);
    return { ...answers, url, locations, privateLocations };
  }