import { Interface as ReadlineInterface } from "readline"; import { Observable } from "rxjs"; import { ThroughStream } from "through"; import Choice from "./lib/objects/choice.js"; import Choices from "./lib/objects/choices.js"; import Separator from "./lib/objects/separator.js"; import "./lib/prompts/base.js"; import CheckboxPrompt from "./lib/prompts/checkbox.js"; import ConfirmPrompt from "./lib/prompts/confirm.js"; import EditorPrompt from "./lib/prompts/editor.js"; import ExpandPrompt from "./lib/prompts/expand.js"; import InputPrompt from "./lib/prompts/input.js"; import ListPrompt from "./lib/prompts/list.js"; import NumberPrompt from "./lib/prompts/number.js"; import PasswordPrompt from "./lib/prompts/password.js"; import RawListPrompt from "./lib/prompts/rawlist.js"; import UI from "./lib/ui/baseUI.js"; import "./lib/ui/bottom-bar.js"; import "./lib/ui/prompt.js"; import "./lib/utils/events.js"; import "./lib/utils/paginator.js"; import "./lib/utils/readline.js"; import "./lib/utils/screen-manager.js"; /** * Represents a union which preserves autocompletion. * * @template T * The keys which are available for autocompletion. * * @template F * The fallback-type. */ type LiteralUnion = T | (F & {}); /** * Represents a function for prompting questions to the user. */ export interface PromptFunction { /** * Prompts the questions to the user. */ (questions: QuestionCollection, initialAnswers?: Partial): Promise; } /** * Provides prompts for answering questions. */ export interface PromptModuleBase extends PromptFunction { /** * Registers a new prompt-type. * * @param name * The name of the prompt. * * @param prompt * The constructor of the prompt. */ registerPrompt(name: string, prompt: inquirer.prompts.PromptConstructor): void; /** * Registers the default prompts. */ restoreDefaultPrompts(): void; } /** * Represents a function for registering a prompt. */ type RegisterFunction = PromptModuleBase["registerPrompt"]; /** * Represents a function for restoring a prompt. */ type RestoreFunction = PromptModuleBase["restoreDefaultPrompts"]; /** * Represents a list-based question. * * @template T * The type of the answers. * * @template TChoiceMap * The valid choices for the question. */ interface ListQuestionOptionsBase extends Question { /** * The choices of the prompt. */ choices?: AsyncDynamicQuestionProperty>, T> | undefined; /** * The number of elements to show on each page. */ pageSize?: number | undefined; } /** * Creates a prompt-module. * * @param opt * The streams for the prompt-module. * * @returns * The new prompt-module. */ export function createPromptModule(opt?: StreamOptions): PromptModule; /** * A component for creating {@link PromptModule `PromptModule`}s. */ type PromptModuleCreator = typeof createPromptModule; /** * Represents either a key of {@link T `T`} or a {@link String `string`}. * * @template T * The type of the keys to suggest. */ export type KeyUnion = LiteralUnion>; /** * Converts the specified union-type {@link U `U`} to an intersection-type. * * @template U * The union to convert to an intersection. */ export type UnionToIntersection = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; /** * A set of answers. */ export interface Answers extends Record {} /** * Provides the functionality to validate answers. * * @template T * The type of the answers. */ export type Validator = Question["validate"]; /** * Provides the functionality to transform an answer. * * @template T * The type of the answers. */ export type Transformer = InputQuestionOptions["transformer"]; /** * Represents a dynamic property for a question. * * @template T * The type of the property. * * @template TAnswers * The type of the answers. */ export type DynamicQuestionProperty = T | ((answers: TAnswers) => T); /** * Represents a dynamic property for a question which can be fetched asynchronously. * * @template T * The type of the property. * * @template TAnswers * The type of the answers. */ export type AsyncDynamicQuestionProperty = DynamicQuestionProperty< T | Promise, TAnswers >; /** * Represents a choice-item. */ export interface ChoiceBase { /** * The type of the choice. */ type?: string | undefined; } /** * Provides options for a choice of the {@link ListPrompt `ListPrompt`}. * * @template T * The type of the answers. */ export interface ListChoiceOptions extends ChoiceOptions { /** * A value indicating whether the choice is disabled. */ disabled?: DynamicQuestionProperty | undefined; } /** * Provides options for a choice of the {@link CheckboxPrompt `CheckboxPrompt`}. * * @template T * The type of the answers. */ export interface CheckboxChoiceOptions extends ListChoiceOptions { /** * A value indicating whether the choice should be initially checked. */ checked?: boolean | undefined; } /** * Provides options for a choice of the {@link ExpandPrompt `ExpandPrompt`}. */ export interface ExpandChoiceOptions extends ChoiceOptions { /** * The key to press for selecting the choice. */ key?: string | undefined; } /** * Represents a separator. */ export interface SeparatorOptions extends ChoiceBase { /** * Gets the type of the choice. */ type: "separator"; /** * Gets or sets the text of the separator. */ line?: string | undefined; } /** * Provides options for a choice. */ export interface ChoiceOptions extends ChoiceBase { /** * @inheritdoc */ type?: "choice" | undefined; /** * The name of the choice to show to the user. */ name?: string | undefined; /** * The value of the choice. */ value?: any; /** * The short form of the name of the choice. */ short?: string | undefined; /** * The extra properties of the choice. */ extra?: any; } /** * Provides all valid choice-types for any kind of question. * * @template T * The type of the answers. */ export interface BaseChoiceMap { Choice: Choice; ChoiceOptions: ChoiceOptions; Separator: Separator; SeparatorOptions: SeparatorOptions; } /** * Provides all valid choice-types for the {@link ListQuestion `ListQuestion`}. * * @template T * The type of the answers. */ export interface ListChoiceMap extends BaseChoiceMap { ListChoiceOptions: ListChoiceOptions; } /** * Provides all valid choice-types for the {@link CheckboxQuestion `CheckboxQuestion`}. * * @template T * The type of the answers. */ export interface CheckboxChoiceMap extends BaseChoiceMap { CheckboxChoiceOptions: CheckboxChoiceOptions; } /** * Provides all valid choice-types for the {@link ExpandQuestion `ExpandQuestion`}. * * @template T * The type of the answers. */ export interface ExpandChoiceMap extends BaseChoiceMap { ExpandChoiceOptions: ExpandChoiceOptions; } /** * Provides all valid choice-types. * * @template T * The type of the answers. */ export interface AllChoiceMap { BaseChoiceMap: BaseChoiceMap[keyof BaseChoiceMap]; ListChoiceMap: ListChoiceMap[keyof ListChoiceMap]; CheckboxChoiceMap: CheckboxChoiceMap[keyof CheckboxChoiceMap]; ExpandChoiceMap: ExpandChoiceMap[keyof ExpandChoiceMap]; } /** * Provides valid choices for the question of the {@link TChoiceMap `TChoiceMap`}. * * @template TAnswers * The type of the answers. * * @template TChoiceMap * The choice-types to provide. */ export type DistinctChoice> = | string | TChoiceMap[keyof TChoiceMap]; /** * Represents a set of choices. * * @template T * The type of the answers. */ export type ChoiceCollection = Array>>; /** * Provides options for a question. * * @template T * The type of the answers. */ export interface Question { /** * The type of the question. */ type?: string | undefined; /** * The key to save the answer to the answers-hash. */ name?: KeyUnion | undefined; /** * The message to show to the user. */ message?: AsyncDynamicQuestionProperty | undefined; /** * The default value of the question. */ default?: AsyncDynamicQuestionProperty | undefined; /** * The prefix of the {@link message `message`}. */ prefix?: string | undefined; /** * The suffix of the {@link message `message`}. */ suffix?: string | undefined; /** * Post-processes the answer. * * @param input * The answer provided by the user. * * @param answers * The answers provided by the user. */ filter?(input: any, answers: T): any; /** * A value indicating whether the question should be prompted. */ when?: AsyncDynamicQuestionProperty | undefined; /** * Validates the integrity of the answer. * * @param input * The answer provided by the user. * * @param answers * The answers provided by the user. * * @returns * Either a value indicating whether the answer is valid or a {@link String `string`} which describes the error. */ validate?(input: any, answers?: T): boolean | string | Promise; /** * Force to prompt the question if the answer already exists. */ askAnswered?: boolean; } /** * Represents the possible answers of each question in the prompt */ export type QuestionAnswer = { [K in keyof T]: { name: K; answer: T[K]; }; }[keyof T]; /** * Provides options for a question for the {@link InputPrompt `InputPrompt`}. * * @template T * The type of the answers. */ export interface InputQuestionOptions extends Question { /** * Transforms the value to display to the user. * * @param input * The input provided by the user. * * @param answers * The answers provided by the users. * * @param flags * Additional information about the value. * * @returns * The value to display to the user. */ transformer?(input: any, answers: T, flags: { isFinal?: boolean | undefined }): string | Promise; } /** * Provides options for a question for the {@link InputPrompt `InputPrompt`}. * * @template T * The type of the answers. */ export interface InputQuestion extends InputQuestionOptions { /** * @inheritdoc */ type?: "input" | undefined; } /** * Provides options for a question for the {@link NumberPrompt `NumberPrompt`}. * * @template T * The type of the answers. */ export interface NumberQuestionOptions extends InputQuestionOptions {} /** * Provides options for a question for the {@link NumberPrompt `NumberPrompt`}. * * @template T * The type of the answers. */ export interface NumberQuestion extends NumberQuestionOptions { /** * @inheritdoc */ type: "number"; } /** * Provides options for a question for the {@link PasswordPrompt `PasswordPrompt`}. * * @template T * The type of the answers. */ export interface PasswordQuestionOptions extends InputQuestionOptions { /** * The character to replace the user-input. */ mask?: string | undefined; } /** * Provides options for a question for the {@link PasswordPrompt `PasswordPrompt`}. * * @template T * The type of the answers. */ export interface PasswordQuestion extends PasswordQuestionOptions { /** * @inheritdoc */ type: "password"; } /** * Represents a list-based question that can loop. * * @template T * The type of the answers. * * @template TChoiceMap * The valid choices for the question. */ interface LoopableListQuestionOptionsBase extends ListQuestionOptionsBase { /** * A value indicating whether choices in a list should be looped. */ loop?: boolean | undefined; } /** * Provides options for a question for the {@link ListPrompt `ListPrompt`}. * * @template T * The type of the answers. */ export interface ListQuestionOptions extends LoopableListQuestionOptionsBase> {} /** * Provides options for a question for the {@link ListPrompt `ListPrompt`}. * * @template T * The type of the answers. */ export interface ListQuestion extends ListQuestionOptions { /** * @inheritdoc */ type: "list"; } /** * Provides options for a question for the {@link RawListPrompt `RawListPrompt`}. * * @template T * The type of the answers. */ export interface RawListQuestionOptions extends ListQuestionOptions {} /** * Provides options for a question for the {@link RawListPrompt `RawListPrompt`}. * * @template T * The type of the answers. */ export interface RawListQuestion extends RawListQuestionOptions { /** * @inheritdoc */ type: "rawlist"; } /** * Provides options for a question for the {@link ExpandPrompt `ExpandPrompt`}. * * @template T * The type of the answers. */ export interface ExpandQuestionOptions extends ListQuestionOptionsBase> {} /** * Provides options for a question for the {@link ExpandPrompt `ExpandPrompt`}. * * @template T * The type of the answers. */ export interface ExpandQuestion extends ExpandQuestionOptions { /** * @inheritdoc */ type: "expand"; } /** * Provides options for a question for the {@link CheckboxPrompt `CheckboxPrompt`}. * * @template T * The type of the answers. */ export interface CheckboxQuestionOptions extends LoopableListQuestionOptionsBase> {} /** * Provides options for a question for the {@link CheckboxPrompt `CheckboxPrompt`}. * * @template T * The type of the answers. */ export interface CheckboxQuestion extends CheckboxQuestionOptions { /** * @inheritdoc */ type: "checkbox"; } /** * Provides options for a question for the {@link ConfirmPrompt `ConfirmPrompt`}. * * @template T * The type of the answers. */ export interface ConfirmQuestionOptions extends Question {} /** * Provides options for a question for the {@link ConfirmPrompt `ConfirmPrompt`}. * * @template T * The type of the answers. */ export interface ConfirmQuestion extends ConfirmQuestionOptions { /** * @inheritdoc */ type: "confirm"; } /** * Provides options for a question for the {@link EditorPrompt `EditorPrompt`}. * * @template T * The type of the answers. */ export interface EditorQuestionOptions extends Question { /** * The postfix of the file being edited. * * Adding this will add color highlighting to the file content in most editors. */ postfix?: string; } /** * Provides options for a question for the {@link EditorPrompt `EditorPrompt`}. * * @template T * The type of the answers. */ export interface EditorQuestion extends EditorQuestionOptions { /** * @inheritdoc */ type: "editor"; } /** * Provides the available question-types. * * @template T * The type of the answers. */ export interface QuestionMap { /** * The {@link InputQuestion `InputQuestion`} type. */ input: InputQuestion; /** * The {@link NumberQuestion `NumberQuestion`} type. */ number: NumberQuestion; /** * The {@link PasswordQuestion `PasswordQuestion`} type. */ password: PasswordQuestion; /** * The {@link ListQuestion `ListQuestion`} type. */ list: ListQuestion; /** * The {@link RawListQuestion `RawListQuestion`} type. */ rawList: RawListQuestion; /** * The {@link ExpandQuestion `ExpandQuestion`} type. */ expand: ExpandQuestion; /** * The {@link CheckboxQuestion `CheckboxQuestion`} type. */ checkbox: CheckboxQuestion; /** * The {@link ConfirmQuestion `ConfirmQuestion`} type. */ confirm: ConfirmQuestion; /** * The {@link EditorQuestion `EditorQuestion`} type. */ editor: EditorQuestion; } /** * Represents one of the available questions. * * @template T * The type of the answers. */ export type DistinctQuestion = QuestionMap[keyof QuestionMap]; /** * Indicates the type of a question */ export type QuestionTypeName = DistinctQuestion["type"]; /** * Represents a collection of questions. * * @template T * The type of the answers. */ export type QuestionCollection = | DistinctQuestion | ReadonlyArray> | Observable> | { [P in KeyUnion]?: DistinctQuestion & { name?: never } }; /** * Provides an input and an output-stream. */ export interface StreamOptions { /** * A stream to read the input from. */ input?: NodeJS.ReadStream | undefined; /** * A stream to write the output to. */ output?: NodeJS.WriteStream | undefined; /** * Whether to display prompts if input is not a TTY. */ skipTTYChecks?: boolean | undefined; } /** * Provides the functionality to prompt questions to the user. */ export interface PromptModule extends PromptModuleBase { /** * The prompts of the prompt-module. */ prompts: inquirer.prompts.PromptCollection; /** * Prompts the questions to the user. */ ( questions: QuestionCollection, initialAnswers?: Partial, ): Promise & { ui: inquirer.ui.Prompt }; /** * Registers a new prompt-type. * * @param name * The name of the prompt. * * @param prompt * The constructor of the prompt. */ registerPrompt(name: string, prompt: inquirer.prompts.PromptConstructor): this; } /** * Provides the functionality to prompt questions. */ declare namespace inquirer { /** * Provides components for the prompts. */ namespace prompts { /** * Provides options for a prompt. * * @template T * The type of the answers. */ type PromptOptions = T & { /** * The choices of the prompt. */ choices: Choices; }; /** * Represents the state of a prompt. */ type PromptState = LiteralUnion<"pending" | "idle" | "loading" | "answered" | "done">; /** * Represents a prompt. */ interface PromptBase { /** * Gets or sets a string which represents the state of the prompt. */ status: PromptState; /** * Runs the prompt. * * @returns * The result of the prompt. */ run(): Promise; } /** * Provides the functionality to initialize new prompts. */ interface PromptConstructor { /** * Initializes a new instance of a prompt. * * @param question * The question to prompt. * * @param readLine * An object for reading from the command-line. * * @param answers * The answers provided by the user. */ new(question: any, readLine: ReadlineInterface, answers: Answers): PromptBase; } /** * Provides a set of prompt-constructors. */ type PromptCollection = Record; /** * Provides data about the state of a prompt. */ interface PromptStateData { /** * Either a {@link String `string`} which describes the error of the prompt or a {@link Boolean `boolean`} indicating whether the prompt-value is valid. */ isValid: string | boolean; } /** * Provides data about the successful state of a prompt. * * @param T * The type of the answer. */ interface SuccessfulPromptStateData extends PromptStateData { /** * @inheritdoc */ isValid: true; /** * The value of the prompt. */ value: T; } /** * Provides data about the failed state of a prompt. */ interface FailedPromptStateData extends PromptStateData { /** * @inheritdoc */ isValid: false | string; } /** * Provides pipes for handling events of a prompt. * * @param T * The type of the answer. */ interface PromptEventPipes { /** * A pipeline for successful inputs. */ success: Observable>; /** * An object representing an error. */ error: Observable; } } /** * Provides components for the ui. */ namespace ui { /** * Represents the bottom-bar UI. */ class BottomBar extends UI { /** * Gets or sets a stream to write logs to. */ log: ThroughStream; /** * Initializes a new instance of the {@link BottomBar `BottomBar`} class. * * @param options * Provides options for the bottom-bar ui. */ constructor(options?: BottomBarOptions); /** * Renders the specified {@link text `text`} to the bottom bar. * * @param text * The text to print to the bottom bar. */ updateBottomBar(text: string): this; /** * Renders the bottom bar. */ protected render(): this; /** * Cleans the bottom bar. */ protected clean(): this; /** * Writes a message to the bottom bar. * * @param message * The message to write. */ protected write(message: string): void; /** * Writes the specified {@link data `data`} to the log-zone. * * @param data * The data to write to the log-zone. */ protected writeLog(data: any): this; /** * Fixes the new-line characters of the specified {@link text `text`}. * * @param text * The text to process. */ protected enforceLF(text: string): string; } /** * Represents the prompt ui. */ class Prompt extends UI { /** * Gets or sets the prompts of the ui. */ prompts: prompts.PromptCollection; /** * Gets or sets the answers provided by the user. */ answers: T; /** * Gets or sets the event-flow of the process. */ process: Observable>; /** * Initializes a new instance of the {@link Prompt `Prompt`} class. * * @param prompts * The prompts for the ui. * * @param options * The input- and output-stream of the ui. */ constructor(prompts: prompts.PromptCollection, options?: StreamOptions); /** * Runs the prompt-UI. * * @param questions * The questions to prompt the user to answer. * * @returns * The answers provided by the user. */ run(questions: Array>): Promise; /** * Finishes the process. */ protected onCompletion(): T; /** * Processes a question. * * @param question * The question to process. * * @returns * The answer to the question. */ protected processQuestion( question: DistinctQuestion, ): Observable; /** * Fetches the answer to a question. * * @param question * The question to fetch the answer for. * * @returns * The answer to the question. */ protected fetchAnswer( question: FetchedQuestion, ): Observable; /** * Sets the type of the question if no question-type is specified. * * @param question * The question to set the default type for. * * @returns * The processed question. */ protected setDefaultType( question: DistinctQuestion, ): Observable>; /** * Filters the question if it is runnable. * * @param question * The question to filter. * * @returns * Either the event-flow of the question if it is runnable or an empty event-flow. */ protected filterIfRunnable( question: DistinctQuestion, ): Observable>; } /** * Provides options for the bottom-bar UI. */ interface BottomBarOptions extends StreamOptions { /** * The initial text to display. */ bottomBar?: string | undefined; } /** * Represents a fetched answer. * * @template T * The type of the answers. */ type FetchedQuestion = DistinctQuestion & { /** * The type of the question. */ type: string; /** * The message to show to the user. */ message: string; /** * The default value of the question. */ default: any; /** * The choices of the question. */ choices: ChoiceCollection; }; /** * Represents a fetched answer. */ interface FetchedAnswer { /** * The name of the answer. */ name: string; /** * The value of the answer. */ answer: any; } } /** * Represents a choice-item separator. */ class Separator implements SeparatorOptions { /** * @inheritdoc */ readonly type: "separator"; /** * @inheritdoc */ line: string; /** * Initializes a new instance of the {@link Separator `Separator`} class. * * @param line * The text of the separator. */ constructor(line?: string); /** * Checks whether the specified {@link item `item`} is not a separator. * * @param item * The item to check. * * @returns * A value indicating whether the item is not a separator. */ static exclude(item: any): boolean; } /** * Registers a new prompt-type. * * @param name * The name of the prompt. * * @param prompt * The constructor of the prompt. */ let registerPrompt: RegisterFunction; /** * Registers the default prompts. */ let restoreDefaultPrompts: RestoreFunction; /** * Creates a prompt-module. * * @param opt * The streams for the prompt-module. * * @returns * The new prompt-module. */ let createPromptModule: PromptModuleCreator; /** * The default prompt-module. */ let prompt: PromptModule; } export default inquirer;