UNPKG

esa-cli

Version:

A CLI for operating Alibaba Cloud ESA EdgeRoutine (Edge Functions).

59 lines (58 loc) 2.81 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import inquirer from 'inquirer'; import logger from '../libs/logger.js'; /** * Perform multi-level selection and return the final selected template path * @param items Array of selection items (including categories and sub-templates) * @param message Initial prompt message * @returns Selected template path, or null if the user exits */ export default function multiLevelSelect(items_1) { return __awaiter(this, arguments, void 0, function* (items, message = 'Select a template:') { let currentItems = items; // Current level options const stack = []; // Stack to store previous level options for back navigation let selectedPath = null; while (selectedPath === null) { const { choice } = yield inquirer.prompt([ { type: 'list', name: 'choice', message, pageSize: 10, choices: [ ...currentItems.map((item) => ({ name: item.label, value: item })), ...(stack.length > 0 ? [{ name: 'Back', value: 'back' }] : []), // Show "Back" if there’s a previous level { name: 'Exit', value: 'exit' } ] } ]); if (choice === 'exit') { logger.log('User canceled the operation.'); return null; } if (choice === 'back') { currentItems = stack.pop(); // Return to the previous level continue; } // If a category with children is selected if (choice.children && choice.children.length > 0) { stack.push(currentItems); // Save the current level currentItems = choice.children; // Move to the next level message = `Select a template under ${choice.label}:`; } else { // A leaf node (no children) is selected, end the selection selectedPath = choice.value; } } return selectedPath; }); }