Skip to content

Commit ab24ece

Browse files
authored
perf: use prompts instead of inquirer (#5443)
1 parent 6de0c3e commit ab24ece

File tree

7 files changed

+78
-105
lines changed

7 files changed

+78
-105
lines changed

lib/common/commands/proxy/proxy-set.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { ProxyCommandBase } from "./proxy-base";
44
import { HttpProtocolToPort } from "../../constants";
55
import { parse } from "url";
66
import { platform, EOL } from "os";
7-
import * as prompt from "inquirer";
87
import { IOptions } from "../../../declarations";
98
import {
109
IErrors,
1110
IHostInfo,
1211
IAnalyticsService,
1312
IProxyService,
1413
IProxyLibSettings,
14+
IPrompterQuestion,
1515
} from "../../declarations";
1616
import { IInjector } from "../../definitions/yok";
1717
import { injector } from "../../yok";
@@ -165,9 +165,9 @@ export class ProxySetCommand extends ProxyCommandBase {
165165

166166
private async getPortFromUserInput(): Promise<number> {
167167
const schemaName = "port";
168-
const schema: prompt.Question = {
168+
const schema: IPrompterQuestion = {
169169
message: "Port",
170-
type: "input",
170+
type: "text",
171171
name: schemaName,
172172
validate: (value: any) => {
173173
return !value || !this.isValidPort(value)

lib/common/declarations.d.ts

+17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
IGoogleAnalyticsData,
66
} from "./definitions/google-analytics";
77
import * as child_process from "child_process";
8+
import { Answers } from "prompts";
89

910
// tslint:disable-next-line:interface-name
1011
interface Object {
@@ -829,6 +830,22 @@ interface IPrompterOptions extends IAllowEmpty {
829830
defaultAction?: () => string;
830831
}
831832

833+
interface IPrompterAnswers extends Record<string, any> {}
834+
835+
interface IPrompterQuestion<T extends Answers<any> = Answers<any>> {
836+
type?: string;
837+
name?: string;
838+
message?: string;
839+
default?: any;
840+
prefix?: string;
841+
suffix?: string;
842+
filter?(input: any, answers: T): any;
843+
validate?(
844+
input: any,
845+
answers?: T
846+
): boolean | string | Promise<boolean | string>;
847+
}
848+
832849
interface IAnalyticsSettingsService {
833850
canDoRequest(): Promise<boolean>;
834851
getUserId(): Promise<string>;

lib/common/prompter.ts

+17-22
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
import * as prompt from "inquirer";
1+
import * as prompt from "prompts";
22
import * as helpers from "./helpers";
33
import * as readline from "readline";
44
import { ReadStream } from "tty";
5-
import { IAllowEmpty, IPrompterOptions } from "./declarations";
5+
import {
6+
IAllowEmpty,
7+
IPrompterAnswers,
8+
IPrompterOptions,
9+
IPrompterQuestion,
10+
} from "./declarations";
611
import { injector } from "./yok";
712
const MuteStream = require("mute-stream");
813
import * as _ from "lodash";
914

1015
export class Prompter implements IPrompter {
11-
private descriptionSeparator = "|";
1216
private ctrlcReader: readline.ReadLine;
1317
private muteStreamInstance: any = null;
1418

@@ -18,12 +22,7 @@ export class Prompter implements IPrompter {
1822
}
1923
}
2024

21-
public async get(questions: prompt.Question[]): Promise<any> {
22-
_.each(questions, (q) => {
23-
q.filter = (selection: string) => {
24-
return selection.split(this.descriptionSeparator)[0].trim();
25-
};
26-
});
25+
public async get(questions: IPrompterQuestion[]): Promise<any> {
2726
try {
2827
this.muteStdout();
2928

@@ -55,7 +54,7 @@ export class Prompter implements IPrompter {
5554
message: string,
5655
options?: IAllowEmpty
5756
): Promise<string> {
58-
const schema: prompt.Question = {
57+
const schema: IPrompterQuestion = {
5958
message,
6059
type: "password",
6160
name: "password",
@@ -73,9 +72,9 @@ export class Prompter implements IPrompter {
7372
message: string,
7473
options?: IPrompterOptions
7574
): Promise<string> {
76-
const schema: prompt.Question = {
75+
const schema: IPrompterQuestion = {
7776
message,
78-
type: "input",
77+
type: "text",
7978
name: "inputString",
8079
validate: (value: any) => {
8180
const doesNotAllowEmpty =
@@ -95,9 +94,9 @@ export class Prompter implements IPrompter {
9594
promptMessage: string,
9695
choices: string[]
9796
): Promise<string> {
98-
const schema: prompt.Answers = {
97+
const schema: IPrompterAnswers = {
9998
message: promptMessage,
100-
type: "list",
99+
type: "select",
101100
name: "userAnswer",
102101
choices,
103102
};
@@ -110,21 +109,17 @@ export class Prompter implements IPrompter {
110109
promptMessage: string,
111110
choices: { key: string; description: string }[]
112111
): Promise<string> {
113-
const longestKeyLength = choices.concat().sort(function (a, b) {
114-
return b.key.length - a.key.length;
115-
})[0].key.length;
116112
const inquirerChoices = choices.map((choice) => {
117113
return {
118-
name: `${_.padEnd(choice.key, longestKeyLength)} ${
119-
choice.description ? this.descriptionSeparator : ""
120-
} ${choice.description}`,
121-
short: choice.key,
114+
title: choice.key,
115+
value: choice.key,
116+
description: choice.description,
122117
};
123118
});
124119

125120
const schema: any = {
126121
message: promptMessage,
127-
type: "list",
122+
type: "select",
128123
name: "userAnswer",
129124
choices: inquirerChoices,
130125
};

lib/definitions/prompter.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import * as prompt from "inquirer";
21
import {
32
IPrompterOptions,
43
IAllowEmpty,
54
IDisposable,
5+
IPrompterQuestion,
66
} from "../common/declarations";
77

88
declare global {
99
interface IPrompter extends IDisposable {
10-
get(schemas: prompt.Question[]): Promise<any>;
10+
get(schemas: IPrompterQuestion[]): Promise<any>;
1111
getPassword(prompt: string, options?: IAllowEmpty): Promise<string>;
1212
getString(prompt: string, options?: IPrompterOptions): Promise<string>;
1313
promptForChoice(promptMessage: string, choices: string[]): Promise<string>;

package-lock.json

+35-74
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)