Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use prompts.override in a few places #814

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 29 additions & 38 deletions packages/api/src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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`)',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.');
}
Expand Down Expand Up @@ -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 {
Expand Down
30 changes: 16 additions & 14 deletions packages/api/src/commands/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();

Expand Down