diff --git a/packages/api/src/commands/install.ts b/packages/api/src/commands/install.ts index a9dd51f6..1e32e447 100644 --- a/packages/api/src/commands/install.ts +++ b/packages/api/src/commands/install.ts @@ -6,6 +6,7 @@ import { emphasize } from 'emphasize'; import figures from 'figures'; import Oas from 'oas'; import ora from 'ora'; +import prompts from 'prompts'; import uslug from 'uslug'; import { SupportedLanguages, codegenFactory } from '../codegen/factory.js'; @@ -20,36 +21,26 @@ interface Options { yes?: boolean; } -async function getLanguage(options: Options) { - let language: SupportedLanguage; - if (options.lang) { - language = options.lang; - } else { - ({ value: language } = await promptTerminal({ - type: 'select', - name: 'value', - message: 'What language would you like to generate an SDK for?', - choices: [{ title: 'JavaScript', value: SupportedLanguages.JS }], - initial: 1, - })); - } +async function getLanguage() { + const { lang }: { lang: SupportedLanguage } = await promptTerminal({ + type: 'select', + name: 'lang', + message: 'What language would you like to generate an SDK for?', + choices: [{ title: 'JavaScript', value: SupportedLanguages.JS }], + initial: 1, + }); - return language; + return lang; } -async function getIdentifier(oas: Oas, uri: string, options: Options) { +async function getIdentifier(oas: Oas, uri: string) { let identifier; - if (options.identifier) { - // `Storage.isIdentifierValid` will throw an exception if an identifier is invalid. - if (Storage.isIdentifierValid(options.identifier)) { - identifier = options.identifier; - } - } else if (Fetcher.isAPIRegistryUUID(uri)) { + if (Fetcher.isAPIRegistryUUID(uri)) { identifier = Fetcher.getProjectPrefixFromRegistryUUID(uri); } else { - ({ value: identifier } = await promptTerminal({ + ({ identifier } = await promptTerminal({ type: 'text', - name: 'value', + name: 'identifier', initial: oas.api?.info?.title ? uslug(oas.api.info.title, { lower: true }) : undefined, message: 'What would you like to identify this API as? This will be how you use the SDK. (e.g. entering `petstore` would result in `@api/petstore`)', @@ -82,7 +73,9 @@ cmd ) .addOption(new Option('-y, --yes', 'Automatically answer "yes" to any prompts printed')) .action(async (uri: string, options: Options) => { - const language = await getLanguage(options); + prompts.override(options); + + const language = await getLanguage(); // @todo let them know that we're going to be creating a `.api/ directory // @todo detect if they have a gitigore and .npmignore and if .api woudl be ignored by that @@ -113,7 +106,7 @@ cmd throw err; }); - const identifier = await getIdentifier(oas, uri, options); + const identifier = await getIdentifier(oas, uri); if (!identifier) { throw new Error('You must tell us what you would like to identify this API as in order to install it.'); } @@ -161,19 +154,17 @@ cmd logger(msg); }); - if (!options.yes) { - await promptTerminal({ - type: 'confirm', - name: 'value', - message: 'OK to proceed with package installation?', - initial: true, - }).then(({ value }) => { - if (!value) { - // @todo cleanup installed files - throw new Error('Installation cancelled.'); - } - }); - } + await promptTerminal({ + type: 'confirm', + name: 'yes', + message: 'OK to proceed with package installation?', + initial: true, + }).then(({ yes }) => { + if (!yes) { + // @todo cleanup installed files + throw new Error('Installation cancelled.'); + } + }); spinner = ora({ text: 'Installing required packages', ...oraOptions() }).start(); try { diff --git a/packages/api/src/commands/uninstall.ts b/packages/api/src/commands/uninstall.ts index 08eddf27..daf5331e 100644 --- a/packages/api/src/commands/uninstall.ts +++ b/packages/api/src/commands/uninstall.ts @@ -3,6 +3,7 @@ import path from 'node:path'; import chalk from 'chalk'; import { Command, Option } from 'commander'; import ora from 'ora'; +import prompts from 'prompts'; import { SupportedLanguages, uninstallerFactory } from '../codegen/factory.js'; import promptTerminal from '../lib/prompt.js'; @@ -35,20 +36,21 @@ cmd storage.setIdentifier(identifier); const directory = path.relative(process.cwd(), storage.getIdentifierStorageDir()); - if (!options.yes) { - await promptTerminal({ - type: 'confirm', - name: 'value', - message: `Are you sure you want to uninstall ${chalk.yellow(identifier)}? This will delete the ${chalk.yellow( - directory, - )} directory and potentially any changes you may have made there.`, - initial: true, - }).then(({ value }) => { - if (!value) { - throw new Error('Uninstallation cancelled.'); - } - }); - } + + // funnels `--yes` option into prompt + prompts.override(options); + await promptTerminal({ + type: 'confirm', + name: 'yes', + message: `Are you sure you want to uninstall ${chalk.yellow(identifier)}? This will delete the ${chalk.yellow( + directory, + )} directory and potentially any changes you may have made there.`, + initial: true, + }).then(({ yes }) => { + if (!yes) { + throw new Error('Uninstallation cancelled.'); + } + }); let spinner = ora({ text: `Uninstalling ${chalk.grey(identifier)}`, ...oraOptions() }).start();