import fs from 'fs' import yargs from 'yargs' import * as prompts from '@clack/prompts' import { hideBin } from 'yargs/helpers' import { Features, features } from './features.js' import { templatesPath } from './config.js' interface ShellArgs { name: string skipFeatures: boolean skipInstall: boolean skipGit: boolean } interface Prompts { name: string | symbol template: string | symbol features: Features[] | symbol install: boolean | symbol git: boolean | symbol } type TemplateOpts = { value: string; label: string; hint?: string } type FeatureOpts = { value: Features; label: Features; hint?: string } const getArgs = () => yargs(hideBin(process.argv)).string('name').boolean(['skipFeatures', 'skipInstall', 'skipGit']) .argv as ShellArgs export const createQuestions = () => { const shellArgs = getArgs() const templates = fs.readdirSync(templatesPath) return prompts.group( { name: () => prompts.text({ message: 'What is the project name?', defaultValue: shellArgs.name, }), template: () => prompts.select({ message: 'What template would you like to use?', options: templates.map((t) => ({ label: t, value: t })), }), ...(!shellArgs.skipFeatures && { features: ({ results }) => { if (results.template !== 'base') { return Promise.resolve([]) } return prompts.multiselect({ message: 'What features would you like this project to include?', options: features.map((f) => ({ label: f, value: f })), required: false, }) }, }), ...(!shellArgs.skipInstall && { install: () => prompts.confirm({ message: 'Install dependencies?', initialValue: true, }), }), ...(!shellArgs.skipGit && { git: () => prompts.confirm({ message: 'Initialize git repository?', initialValue: true, }), }), }, { onCancel: () => { prompts.cancel('Operation cancelled.') process.exit(0) }, } ) }