import { isCancel, select, text } from '@clack/prompts' import { CliError, resolveCallbackable, resolveCallbackables, } from '../utils.ts' import type { Choice, ConfigVariable, Context } from '../types.ts' export async function variable(context: Context): Promise { const { template, project } = context const variablesList = await resolveCallbackables(template.variables, context) const variables: Record = Object.assign( {}, ...variablesList, ) for (const [key, variable] of Object.entries(variables)) { let value: string | symbol | undefined if (variable.type === 'text') { const placeholder = await resolveCallbackable( variable.placeholder, context, ) value = await text({ message: variable.message, initialValue: variable.initial, placeholder, validate: (value) => { if (variable.required && !value) return 'This field is required.' return }, }) } else { value = await select({ message: variable.message, initialValue: variable.initial, options: variable.choices.map((choice): Choice => typeof choice === 'string' ? { value: choice } : choice, ), }) } if (isCancel(value)) throw new CliError( 'Variable input canceled. Please run the command again.', ) project.variables[key] = value } }