From f320ebab861f5f119d094c7258f3d12f94dfe28d Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Mon, 22 Jul 2024 21:10:47 -0500 Subject: [PATCH] feat(cli): add prompts for select formatter and linter --- packages/cli/package.json | 2 + packages/cli/src/commands/add.ts | 94 +++++++++++++++++------------- packages/cli/src/commands/index.ts | 3 + packages/cli/src/index.ts | 11 ++-- pnpm-lock.yaml | 14 +++++ 5 files changed, 76 insertions(+), 48 deletions(-) create mode 100644 packages/cli/src/commands/index.ts diff --git a/packages/cli/package.json b/packages/cli/package.json index 04b39f7..f8ffc26 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -23,11 +23,13 @@ "@rapidapp/config": "workspace:*", "@rapidapp/scanner": "workspace:*", "@types/node": "20.14.11", + "@types/prompts": "2.4.9", "@vercel/ncc": "0.38.1", "commander": "12.1.0", "conf": "13.0.1", "ora": "8.0.1", "picocolors": "1.0.1", + "prompts": "2.4.2", "typescript": "5.5.4", "update-check": "1.5.4", "vitest": "2.0.4" diff --git a/packages/cli/src/commands/add.ts b/packages/cli/src/commands/add.ts index 6ca00e4..2a65d77 100644 --- a/packages/cli/src/commands/add.ts +++ b/packages/cli/src/commands/add.ts @@ -1,44 +1,54 @@ -import { cyan, green } from 'picocolors' -import { program } from '../' - -export function addCommand(name: string) { - const projectPath = name - const options = this.opts() - - if (!projectPath) { - console.log( - `\nPlease specify the project directory:\n ${cyan(program.name())} ${cyan('add')} ${green( - '', - )} ${cyan('[tools]')}\nFor example:\n ${cyan(program.name())} ${cyan('add')} ${green('website')} ${cyan( - '--linter eslint', - )}\n\nRun ${cyan(`${program.name()} help add`)} to see all options.`, - ) - - process.exit(1) - } - - // TODO: Show a prompt to select the tool to add - if (Object.keys(options).length === 0) { - console.log( - `\nPlease specify the tool to add:\n ${cyan(program.name())} ${cyan('add ')} ${green( - '[tools]', - )}\nFor example:\n ${cyan(program.name())} ${cyan('add website')} ${green( - '--linter eslint --formatter prettier', - )}\n\nRun ${cyan(`${program.name()} help add`)} to see all options.`, - ) - - process.exit(1) - } - - if (options.L != null && options.L !== true) { - console.log(options.L) - } - - if (options.F != null && options.F !== true) { - console.log(options.F) - } - - if (options.Gh != null && options.Gh !== true) { - console.log(options.Gh) +import prompts from 'prompts' + +export async function addCommand(tools: string[]) { + const projectPath = process.cwd() + + if (tools.length === 0) { + const { type } = await prompts([ + { + type: 'multiselect', + name: 'type', + message: 'Select type of tools to add', + choices: [ + { title: 'Linter', value: 'linter' }, + { title: 'Formatter', value: 'formatter' }, + ], + }, + ]) + + if (!Array.isArray(type)) { + return + } + + // TODO: if select biome, ask if want to formatter + if (type.includes('linter')) { + const { linter } = await prompts([ + { + type: 'select', + name: 'linter', + message: 'Select linter to add', + choices: [ + { title: 'Biome', value: 'biome' }, + { title: 'ESLint', value: 'eslint' }, + ], + }, + ]) + } + + if (type.includes('formatter')) { + const { formatter } = await prompts([ + { + type: 'select', + name: 'formatter', + message: 'Select linter to add', + choices: [ + { title: 'Biome', value: 'biome' }, + { title: 'Prettier', value: 'prettier' }, + ], + }, + ]) + } + + return } } diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts new file mode 100644 index 0000000..a84f7e6 --- /dev/null +++ b/packages/cli/src/commands/index.ts @@ -0,0 +1,3 @@ +export * from './add' +export * from './config' +export * from './scan' diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 26b8835..7740524 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -5,8 +5,7 @@ import Conf from 'conf' import { green } from 'picocolors' import packageJson from '../package.json' -import { configCommand } from './commands/config' -import { scannerCommand } from './commands/scan' +import { addCommand, configCommand, scannerCommand } from './commands' import { checkUpdates } from './utils/checkUpdates' export const conf = new Conf({ @@ -48,10 +47,10 @@ program // TODO: support git hooks // TODO: support oxc // TODO: add description -// program -// .command('add') -// .addArgument(new Argument('[tools...]').choices(['biome', 'prettier', 'eslint'])) -// .action(addCommand) +program + .command('add') + .addArgument(new Argument('[tools...]').choices(['biome', 'prettier', 'eslint'])) + .action(addCommand) // TODO: add option --output -o support csv, json yaml html table // TODO: add description diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2c4277e..3b27aa2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -49,6 +49,9 @@ importers: '@types/node': specifier: 20.14.11 version: 20.14.11 + '@types/prompts': + specifier: 2.4.9 + version: 2.4.9 '@vercel/ncc': specifier: 0.38.1 version: 0.38.1 @@ -64,6 +67,9 @@ importers: picocolors: specifier: 1.0.1 version: 1.0.1 + prompts: + specifier: 2.4.2 + version: 2.4.2 typescript: specifier: 5.5.4 version: 5.5.4 @@ -1031,6 +1037,9 @@ packages: '@types/parse-gitignore@1.0.2': resolution: {integrity: sha512-AQwj+lNTWI7y1kkMe8qLByiToXoXs/du70qGFIHJZaJUVrF5jB8QzvWmLyR1VWYqRagpY8ABrqAjs7uHsJnVBQ==} + '@types/prompts@2.4.9': + resolution: {integrity: sha512-qTxFi6Buiu8+50/+3DGIWLHM6QuWsEKugJnnP6iv2Mc4ncxE4A/OJkjuVOA+5X0X1S/nq5VJRa8Lu+nwcvbrKA==} + '@types/sax@1.2.7': resolution: {integrity: sha512-rO73L89PJxeYM3s3pPPjiPgVVcymqU490g0YO5n5By0k2Erzj6tay/4lr1CHAAU4JyOWd1rpQ8bCf6cZfHU96A==} @@ -4164,6 +4173,11 @@ snapshots: dependencies: '@types/node': 20.14.11 + '@types/prompts@2.4.9': + dependencies: + '@types/node': 20.14.11 + kleur: 3.0.3 + '@types/sax@1.2.7': dependencies: '@types/node': 20.14.11