Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/create. Command-line for creating projects from templates.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import { 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<void> { const { template, project } = context const variablesList = await resolveCallbackables(template.variables, context) const variables: Record<string, ConfigVariable> = 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 }}