Skip to content

Commit

Permalink
feat: handle better canceling prompts (#2138)
Browse files Browse the repository at this point in the history
* feat: handle better aborting prompts

* fix: remove useless packages

* fix(test): expect for relevant function
  • Loading branch information
szymonrybczak authored Oct 30, 2023
1 parent 8e068ce commit 17387d5
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 27 deletions.
3 changes: 1 addition & 2 deletions packages/cli-clean/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"dependencies": {
"@react-native-community/cli-tools": "12.0.0-alpha.18",
"chalk": "^4.1.2",
"execa": "^5.0.0",
"prompts": "^2.4.2"
"execa": "^5.0.0"
},
"files": [
"build",
Expand Down
5 changes: 2 additions & 3 deletions packages/cli-clean/src/clean.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {getLoader} from '@react-native-community/cli-tools';
import {getLoader, prompt} from '@react-native-community/cli-tools';
import type {Config as CLIConfig} from '@react-native-community/cli-types';
import chalk from 'chalk';
import execa from 'execa';
import {existsSync as fileExists, rmdir} from 'fs';
import os from 'os';
import path from 'path';
import prompts from 'prompts';
import {promisify} from 'util';

type Args = {
Expand Down Expand Up @@ -53,7 +52,7 @@ function findPath(startPath: string, files: string[]): string | undefined {
async function promptForCaches(
groups: CleanGroups,
): Promise<string[] | undefined> {
const {caches} = await prompts({
const {caches} = await prompt({
type: 'multiselect',
name: 'caches',
message: 'Select all caches to clean',
Expand Down
1 change: 0 additions & 1 deletion packages/cli-doctor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"ip": "^1.1.5",
"node-stream-zip": "^1.9.1",
"ora": "^5.4.1",
"prompts": "^2.4.2",
"semver": "^7.5.2",
"strip-ansi": "^5.2.0",
"wcwidth": "^1.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
parseTasksFromGradleFile,
promptForTaskSelection,
} from '../listAndroidTasks';

import tools from '@react-native-community/cli-tools';
const gradleTaskOutput = `
> Task :tasks
Expand Down Expand Up @@ -111,9 +111,11 @@ describe('promptForTaskSelection', () => {
}),
);

const promptSpy = jest.spyOn(tools, 'prompt');

promptForTaskSelection('install', 'sourceDir');

expect(prompts).toHaveBeenCalledWith({
expect(promptSpy).toHaveBeenCalledWith({
choices: tasksList.map((t) => ({
title: `${chalk.bold(t.task)} - ${t.description}`,
value: t.task,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import getAdbPath from './getAdbPath';
import {getEmulators} from './tryLaunchEmulator';
import {toPascalCase} from './toPascalCase';
import os from 'os';
import prompts from 'prompts';
import chalk from 'chalk';
import {CLIError} from '@react-native-community/cli-tools';
import {CLIError, prompt} from '@react-native-community/cli-tools';

type DeviceData = {
deviceId: string | undefined;
Expand Down Expand Up @@ -56,7 +55,7 @@ async function promptForDeviceSelection(
'No devices and/or emulators connected. Please create emulator with Android Studio or connect Android device.',
);
}
const {device} = await prompts({
const {device} = await prompt({
type: 'select',
name: 'device',
message: 'Select the device / emulator you want to use',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {CLIError} from '@react-native-community/cli-tools';
import {CLIError, prompt} from '@react-native-community/cli-tools';
import chalk from 'chalk';
import execa from 'execa';
import prompts from 'prompts';

type GradleTask = {
task: string;
Expand Down Expand Up @@ -48,7 +47,7 @@ export const promptForTaskSelection = async (
if (!tasks.length) {
throw new CLIError(`No actionable ${taskType} tasks were found...`);
}
const {task}: {task: string} = await prompts({
const {task}: {task: string} = await prompt({
type: 'select',
name: 'task',
message: `Select ${taskType} task you want to perform`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {logger} from '@react-native-community/cli-tools';
import execa from 'execa';
import prompts from 'prompts';
import {logger, prompt} from '@react-native-community/cli-tools';

type User = {
id: string;
Expand Down Expand Up @@ -42,7 +41,7 @@ export function checkUsers(device: string, adbPath: string) {
}

export async function promptForUser(users: User[]) {
const {selectedUser}: {selectedUser: User} = await prompts({
const {selectedUser}: {selectedUser: User} = await prompt({
type: 'select',
name: 'selectedUser',
message: 'Which profile would you like to launch your app into?',
Expand Down
5 changes: 2 additions & 3 deletions packages/cli-platform-ios/src/commands/logIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import {spawnSync} from 'child_process';
import os from 'os';
import path from 'path';
import {logger} from '@react-native-community/cli-tools';
import {logger, prompt} from '@react-native-community/cli-tools';
import listIOSDevices from '../../tools/listIOSDevices';
import getSimulators from '../../tools/getSimulators';
import {Config} from '@react-native-community/cli-types';
import prompts from 'prompts';

/**
* Starts iOS device syslog tail
Expand Down Expand Up @@ -49,7 +48,7 @@ async function logIOS(_argv: Array<string>, _ctx: Config, args: Args) {
}

if (args.interactive && bootedAndAvailableSimulators.length > 1) {
const {udid} = await prompts({
const {udid} = await prompt({
type: 'select',
name: 'udid',
message: 'Select iOS simulators to tail logs from',
Expand Down
8 changes: 4 additions & 4 deletions packages/cli-platform-ios/src/tools/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import chalk from 'chalk';
import prompts from 'prompts';
import {Device} from '../types';
import {prompt} from '@react-native-community/cli-tools';

export async function promptForSchemeSelection(
schemes: string[],
): Promise<string> {
const {scheme} = await prompts({
const {scheme} = await prompt({
name: 'scheme',
type: 'select',
message: 'Select the scheme you want to use',
Expand All @@ -21,7 +21,7 @@ export async function promptForSchemeSelection(
export async function promptForConfigurationSelection(
configurations: string[],
): Promise<string> {
const {configuration} = await prompts({
const {configuration} = await prompt({
name: 'configuration',
type: 'select',
message: 'Select the configuration you want to use',
Expand All @@ -37,7 +37,7 @@ export async function promptForConfigurationSelection(
export async function promptForDeviceSelection(
availableDevices: Device[],
): Promise<Device | undefined> {
const {device} = await prompts({
const {device} = await prompt({
type: 'select',
name: 'device',
message: 'Select the device you want to use',
Expand Down
4 changes: 2 additions & 2 deletions packages/cli-tools/src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ type InteractionCallback = (options: InteractionOptions) => void;
/** Interaction observers for detecting when keystroke tracking should pause/resume. */
const listeners: InteractionCallback[] = [];

export async function prompt(
export async function prompt<T extends string>(
question: PromptObject,
options: PromptOptions = {},
) {
): Promise<prompts.Answers<T>> {
pauseInteractions();
try {
const results = await prompts(question, {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import os from 'os';
import path from 'path';
import fs from 'fs-extra';
import {validateProjectName} from './validate';
import {prompt} from 'prompts';
import chalk from 'chalk';
import DirectoryAlreadyExistsError from './errors/DirectoryAlreadyExistsError';
import printRunInstructions from './printRunInstructions';
Expand All @@ -12,6 +11,7 @@ import {
getLoader,
Loader,
cacheManager,
prompt,
} from '@react-native-community/cli-tools';
import {installPods} from '@react-native-community/cli-platform-ios';
import {
Expand Down

0 comments on commit 17387d5

Please sign in to comment.