From 1fd65c6b8650f29281867e33985f1986a37a0ae6 Mon Sep 17 00:00:00 2001 From: Danya H Date: Thu, 13 Jun 2024 19:26:30 +0100 Subject: [PATCH] optional cli arguments against selector --- cli/src/commands/create.ts | 80 +++++++++++++++++++++++++------------- cli/src/index.ts | 1 + 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/cli/src/commands/create.ts b/cli/src/commands/create.ts index 114c613..c99ee9e 100644 --- a/cli/src/commands/create.ts +++ b/cli/src/commands/create.ts @@ -1,33 +1,59 @@ -import type { CommandModule, ArgumentsCamelCase } from 'yargs' +import type { CommandModule, Argv, ArgumentsCamelCase } from 'yargs' import c from 'chalk' import { input, select } from '@inquirer/prompts' import clone from 'git-clone' import path from 'node:path' -// the handler function will be called when our command is executed -// it will receive the command line arguments parsed by yargs -async function handler() { - const folder = await input({ - message: c.gray('Enter project name:'), - default: 'rage-fw', - }) - const framework = await select({ - message: c.gray('Select frontend:'), - default: 'react', - loop: true, - choices: [ - { - name: 'React + TypeScript (Vite)', - value: 'react', - description: 'React + TypeScript (Vite) as a frontend', - }, - // { - // name: 'vue', - // value: 'vue', - // description: 'npm is the most popular package manager', - // }, - ], - }) +function builder(yargs: Argv) { + return yargs + .option('projectName', { + alias: 'p', + description: 'Name of the folder to scaffold a project to', + type: 'string', + demandOption: false, + }) + .option('template', { + alias: 't', + description: 'Frontend framework to use for CEF', + type: 'string', + demandOption: false, + }) +} + +async function handler(args: ArgumentsCamelCase) { + let folder = (args.projectName as string) ?? args.p + let framework = (args.template as string) ?? args.t + + if (!folder) { + folder = await input({ + message: c.gray('Enter project name:'), + default: 'rage-fw', + }) + } else { + console.log(c.gray('Project name:'), folder) + } + + if (!framework) { + framework = await select({ + message: c.gray('Select frontend:'), + default: 'react', + loop: true, + choices: [ + { + name: 'React + TypeScript (Vite)', + value: 'react', + description: 'React + TypeScript (Vite) as a frontend', + }, + // { + // name: 'vue', + // value: 'vue', + // description: 'npm is the most popular package manager', + // }, + ], + }) + } else { + console.log(c.gray('Frontend:'), framework) + } console.log( c.gray('\nScaffolding template project into'), @@ -56,11 +82,11 @@ async function handler() { ) } -// name and description for our command module const init: CommandModule = { - command: 'create [template]', + command: 'create [folderName] [template]', aliases: 'c', describe: 'Scaffold a template project using RageFW', + builder, handler, } diff --git a/cli/src/index.ts b/cli/src/index.ts index 512d938..a2efb24 100644 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -6,5 +6,6 @@ yargs .usage(' [args]') // .scriptName('rage-fw') // .usage('$0 [args]') + // @ts-ignore .command(create) .help().argv