Skip to content

Commit

Permalink
add autocomplete to interactive wizard prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
locksten committed Apr 9, 2024
1 parent fb9f073 commit 4d40d1a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 31 deletions.
6 changes: 5 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@
"@rnv/sdk-telemetry": "1.0.0-rc.13",
"chalk": "4.1.0",
"commander": "12.0.0",
"inquirer": "8.2.0"
"inquirer": "8.2.0",
"inquirer-autocomplete-prompt": "2.0.1"
},
"peerDependencies": {
"@rnv/core": "^1.0.0-rc.13"
},
"private": false,
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@types/inquirer-autocomplete-prompt": "^3.0.3"
}
}
4 changes: 4 additions & 0 deletions packages/cli/src/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inquirer from 'inquirer';
import inquirerAutocompletePrompt from 'inquirer-autocomplete-prompt';
import {
chalk,
logWarning,
Expand All @@ -10,6 +11,8 @@ import {
getContext,
} from '@rnv/core';

inquirer.registerPrompt('autocomplete', inquirerAutocompletePrompt);

export const inquirerPrompt = async (params: PromptParams): Promise<Record<string, any>> => {
const c = getContext();

Expand Down Expand Up @@ -48,6 +51,7 @@ export const inquirerPrompt = async (params: PromptParams): Promise<Record<strin
if (type === 'confirm' && !name) params.name = 'confirm';

const resp = inquirer.prompt(params);
if (params.initialValue) resp.ui.rl.input.push(params.initialValue);
return resp;
};

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ export type PromptParams = {
type: string;
message?: string;
choices?: Array<{ name: string; value: any } | string>;
source?: (answersSoFar: any, input: string | undefined) => Promise<any>;
validate?: (i: string) => string | boolean;
logMessage?: string;
warningMessage?: string;
initialValue?: string;
default?: any; // string | boolean | (() => string) | string[] | number | { name: string; value: any };
pageSize?: number;
loop?: boolean;
Expand Down
40 changes: 13 additions & 27 deletions packages/core/src/tasks/wizard.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,37 @@
import { findSuitableTask } from './taskFinder';
import { inquirerPrompt } from '../api';
import { getContext } from '../context/provider';
import { chalk, logInfo } from '../logger';
import { getRegisteredTasks } from './taskRegistry';
import { initializeTask } from './taskExecutors.js';
import { getTaskNameFromCommand } from './taskHelpers';
import { getTaskNameFromCommand, selectPlatformIfRequired } from './taskHelpers';
import { RnvTask } from './types';

type TaskOpt = {
name: string;
value: string;
value: RnvTask;
};

const generateOptionPrompt = async (options: TaskOpt[]) => {
const ctx = getContext();

// const { selectedTask } = await inquirerPrompt({
// type: 'list',
// // default: defaultCmd,
// name: 'selectedTask',
// message: `Pick a command`,
// choices: options,
// pageSize: 15,
// logMessage: 'Welcome to the brave new world...',
// });
// const taskArr = selectedTask.split(' ');
// ctx.command = taskArr[0];
// ctx.subCommand = taskArr[1] || null;

// const initTask = await findSuitableTask();
// return initializeTask(initTask);

const { selectedTask } = await inquirerPrompt({
type: 'list',
// default: defaultCmd,
type: 'autocomplete',
source: async (_, input) => options.filter((o) => o.name.toLowerCase().includes(input?.toLowerCase() ?? '')),
initialValue: ctx.program.args?.join(' '),
name: 'selectedTask',
message: `Pick a command`,
loop: false,
choices: options,
pageSize: 15,
logMessage: 'Welcome to the brave new world...',
});

const taskArr = selectedTask.split(' ');
const taskArr = selectedTask.task.split(' ');
ctx.command = taskArr[0];
ctx.subCommand = taskArr[1] || null;

const initTask = await findSuitableTask();
return initializeTask(initTask);
await selectPlatformIfRequired(selectedTask);
return initializeTask(selectedTask);
};

export const runInteractiveWizardForSubTasks = async () => {
Expand Down Expand Up @@ -75,7 +61,7 @@ export const runInteractiveWizardForSubTasks = async () => {
if (!optionsMap[taskInstance.task]) {
optionsMap[taskInstance.task] = {
name: `${taskInstance.task} ${chalk().gray(taskInstance.description)}`,
value: taskInstance.task,
value: taskInstance,
};
} else {
// If multiple tasks with same name we append ... to indicate there are more options coming
Expand All @@ -85,7 +71,7 @@ export const runInteractiveWizardForSubTasks = async () => {
if (!alternativeOptionsMap[taskInstance.task]) {
alternativeOptionsMap[taskInstance.task] = {
name: `${taskInstance.task} ${chalk().gray(taskInstance.description)}`,
value: taskInstance.task,
value: taskInstance,
};
} else {
// If multiple tasks with same name we append ... to indicate there are more options coming
Expand Down Expand Up @@ -127,7 +113,7 @@ export const runInteractiveWizard = async () => {
Object.values(tasks).forEach((taskInstance) => {
options.push({
name: `${taskInstance.task} ${chalk().gray(taskInstance.description)}`,
value: taskInstance.task,
value: taskInstance,
});
});

Expand Down
39 changes: 36 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4807,6 +4807,21 @@
dependencies:
"@types/node" "*"

"@types/inquirer-autocomplete-prompt@^3.0.3":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/inquirer-autocomplete-prompt/-/inquirer-autocomplete-prompt-3.0.3.tgz#8bbb3095454cb2ac9a26865c694e32fc317d7640"
integrity sha512-OQCW09mEECgvhcppbQRgZSmWskWv58l+WwyUvWB1oxTu3CZj8keYSDZR9U8owUzJ5Zeux5kacN9iVPJLXcoLXg==
dependencies:
"@types/inquirer" "*"

"@types/inquirer@*":
version "9.0.7"
resolved "https://registry.yarnpkg.com/@types/inquirer/-/inquirer-9.0.7.tgz#61bb8d0e42f038b9a1738b08fba7fa98ad9b4b24"
integrity sha512-Q0zyBupO6NxGRZut/JdmqYKOnN95Eg5V8Csg3PGKkP+FnvsUZx1jAyK7fztIszxxMuoBA6E3KXWvdZVXIpx60g==
dependencies:
"@types/through" "*"
rxjs "^7.2.0"

"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1":
version "2.0.6"
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.6.tgz#7739c232a1fee9b4d3ce8985f314c0c6d33549d7"
Expand Down Expand Up @@ -5219,6 +5234,13 @@
dependencies:
"@types/node" "*"

"@types/through@*":
version "0.0.33"
resolved "https://registry.yarnpkg.com/@types/through/-/through-0.0.33.tgz#14ebf599320e1c7851e7d598149af183c6b9ea56"
integrity sha512-HsJ+z3QuETzP3cswwtzt2vEIiHBk/dCcHGhbmG5X3ecnwFD/lPrMpliGXxSCg03L9AhrdwA4Oz/qfspkDW+xGQ==
dependencies:
"@types/node" "*"

"@types/tough-cookie@*":
version "4.0.5"
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.5.tgz#cb6e2a691b70cb177c6e3ae9c1d2e8b2ea8cd304"
Expand Down Expand Up @@ -10537,7 +10559,7 @@ fetch-blob@^3.1.2, fetch-blob@^3.1.4:
node-domexception "^1.0.0"
web-streams-polyfill "^3.0.3"

[email protected], figures@^3.0.0:
[email protected], figures@^3.0.0, figures@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/figures/-/figures-3.2.0.tgz#625c18bd293c604dc4a8ddb2febf0c88341746af"
integrity sha512-yaduQFRKLXYOGgEn6AZau90j3ggSOyiqXU0F9JZfeXYhNa+Jk4X+s45A2zg5jns87GAFa34BBm2kXw4XpNcbdg==
Expand Down Expand Up @@ -12165,6 +12187,17 @@ inline-style-prefixer@^6.0.1:
css-in-js-utils "^3.1.0"
fast-loops "^1.1.3"

[email protected]:
version "2.0.1"
resolved "https://registry.yarnpkg.com/inquirer-autocomplete-prompt/-/inquirer-autocomplete-prompt-2.0.1.tgz#72868aada4d9d36814a6054cbd1ececc63aab0c6"
integrity sha512-jUHrH0btO7j5r8DTQgANf2CBkTZChoVySD8zF/wp5fZCOLIuUbleXhf4ZY5jNBOc1owA3gdfWtfZuppfYBhcUg==
dependencies:
ansi-escapes "^4.3.2"
figures "^3.2.0"
picocolors "^1.0.0"
run-async "^2.4.1"
rxjs "^7.5.4"

[email protected]:
version "8.2.0"
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-8.2.0.tgz#f44f008dd344bbfc4b30031f45d984e034a3ac3a"
Expand Down Expand Up @@ -18823,7 +18856,7 @@ rollup@^2.28.2, rollup@^2.43.1:
optionalDependencies:
fsevents "~2.3.2"

run-async@^2.4.0:
run-async@^2.4.0, run-async@^2.4.1:
version "2.4.1"
resolved "https://registry.yarnpkg.com/run-async/-/run-async-2.4.1.tgz#8440eccf99ea3e70bd409d49aab88e10c189a455"
integrity sha512-tvVnVv01b8c1RrA6Ep7JkStj85Guv/YrMcwqYQnwjsAS2cTmmPGBBjAjpCW7RrSodNSoE2/qg9O4bceNvUuDgQ==
Expand All @@ -18847,7 +18880,7 @@ rxjs@^6.6.0:
dependencies:
tslib "^1.9.0"

rxjs@^7.2.0, rxjs@^7.5.5, rxjs@^7.8.1:
rxjs@^7.2.0, rxjs@^7.5.4, rxjs@^7.5.5, rxjs@^7.8.1:
version "7.8.1"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.8.1.tgz#6f6f3d99ea8044291efd92e7c7fcf562c4057543"
integrity sha512-AA3TVj+0A2iuIoQkWEK/tqFjBq2j+6PO6Y0zJcvzLAFhEFIO3HL0vls9hWLncZbAAbK0mar7oZ4V079I/qPMxg==
Expand Down

0 comments on commit 4d40d1a

Please sign in to comment.