How to use the prompts/lib/elements.SelectPrompt function in prompts

To help you get started, we’ve selected a few prompts examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github chanzo / script-launcher / src / launch-menu.ts View on Github external
function promptMenu(menu: IMenu, pageSize: number, defaults: string[], choice: string[]): Promise & { close: () => void } {
  const choices = createChoices(menu);
  let close = false;

  defaults = [...defaults];

  if (choices.length === 0) {
    console.log('Nothing to do: Menu not available and no action specified.');
    throw new Error('Nothing to do: Menu not available and no action specified.');
  }

  let initialIndex = choices.findIndex((item) => item.title === defaults[0]);

  if (initialIndex === -1) initialIndex = 0;

  const selectMenu = new SelectPrompt(
    {
      type: 'select',
      name: 'value',
      message: 'Select' + (menu.description ? ' ' + menu.description : ''),
      initial: initialIndex,
      choices: choices,
    },
  );
  const menuPromise = toPromise(selectMenu);

  const resultPromise = menuPromise.then((answer) => {
    if (close || answer.length === 0) return null;

    const menuItem = findMenuItem(menu, answer[0]);

    if (menuItem === null) {