import { withLoading } from 'lib/output/with-loading.js' import { promptAutocomplete, withBackHint } from 'lib/prompt.js' export interface ResourceChoice { title: string value: string description?: string } export const interactForResource = async ({ resourceName, fetchResources, toChoice, message = `Select a ${resourceName}:`, initialValue, }: { resourceName: string fetchResources: () => Promise toChoice: (resource: Resource) => ResourceChoice message?: string | undefined /** The resource already chosen, for the list to open on. */ initialValue?: string | undefined }) => { const resources = await withLoading( `Fetching ${resourceName.replace(/_/g, ' ')}s...`, fetchResources, ) return await promptAutocomplete({ // Resource pickers are only reached from the parameter flow, which // returns to its menu when one is dismissed. message: withBackHint(message), choices: resources.map((resource) => { const { title, value, description } = toChoice(resource) return { label: title, value, hint: description } }), initialValue, }) }