import confirm from '@inquirer/confirm'; import input from '@inquirer/input'; import select from '@inquirer/select'; import { defaultValues } from './defaultValues'; import { getBranches } from './helpers/git'; import { CliResults } from './helpers/types'; /** * Runs the questionnaire to gather user input for the project creation. * @param cliResults - The initial CLI results object. * @returns A promise that resolves to the updated CLI results object. */ export const runQuestionaire = async ( cliResults: CliResults, ): Promise => { const project: CliResults = { ...cliResults }; if (project.projectName === undefined) { project.projectName = await input({ message: 'Project name', default: defaultValues.projectName, }); } if (project.target === undefined) { project.target = await input({ message: 'Target folder', default: `./${project.projectName}`, }); } if (project.installationType === undefined) { project.installationType = await select({ message: 'Installation type', choices: [ { value: 'stable', name: 'Stable', description: 'Use latest official released version. Recommended', }, { value: 'experimental', name: 'Experimental', description: 'Using latest, untested version', }, { value: 'custom', name: 'Custom', description: 'Select a specific branch', }, ], default: defaultValues.installationType, }); } if (project.installationType === 'custom' && project.branch === undefined) { const branchesOra = (await import('ora')) .default('Fetching branches...') .start(); const { default: autocomplete } = await import( 'inquirer-autocomplete-standalone' ); const branches = (await getBranches()).map((branch) => ({ value: branch })); branchesOra.stop(); project.branch = branches.length > 0 ? await autocomplete({ message: 'Branch name', source: async (input) => { if (input === undefined) { return branches; } return branches.filter((branch) => branch.value.includes(input)); }, }) : await input({ message: 'Branch name', }); } if (project.noGit === undefined) { project.noGit = !(await confirm({ message: 'Initialize a git repository?', default: true, })); } if (project.noInstall === undefined) { project.noInstall = !(await confirm({ message: 'Install dependencies?', default: true, })); } return project; };