import * as inquirer from "@inquirer/prompts"; import { FirebaseError } from "./error"; export { Separator } from "@inquirer/prompts"; let globalNonInteractive = false; /** * Set the global non-interactive override. */ export function setNonInteractive(value: boolean): void { globalNonInteractive = value; } /** * Get the global non-interactive override state. */ export function isNonInteractive(): boolean { return globalNonInteractive; } /** * Common options for all prompts. */ export interface BasicOptions { message: string; default?: T; force?: boolean; nonInteractive?: boolean; } /** * Guard function to check if the prompt should return a default value or throw an error. * This is used to prevent prompts from being shown in non-interactive mode. * * @param opts - The options for the prompt. * @returns An object indicating whether to return a value or not. */ export function guard( opts: BasicOptions, ): { shouldReturn: true; value: T } | { shouldReturn: false; value: undefined } { if (!opts.nonInteractive && !isNonInteractive()) { return { shouldReturn: false, value: undefined }; } if (typeof opts.default !== "undefined") { return { shouldReturn: true, value: opts.default }; } throw new FirebaseError( `Question "${opts.message}" does not have a default and cannot be answered in non-interactive mode`, ); } /** * Options for the Input function. * * Exported because Inqurier does not export its own input configs anymore. Some * unused options are missing, such as theme. */ export type InputConfig = BasicOptions & { transformer?: ( value: string, { isFinal, }: { isFinal: boolean; }, ) => string; validate?: (value: string) => boolean | string | Promise; }; /** * Prompt for a string input. */ export async function input(message: string): Promise; /** * Prompt for a string input. */ export async function input(opts: InputConfig): Promise; export async function input(opts: InputConfig | string): Promise { if (typeof opts === "string") { opts = { message: opts }; } else { const { shouldReturn, value } = guard(opts); if (shouldReturn) { return value; } } return inquirer.input(opts); } /** * Options for the confirm function. * * Exported because Inquirer does not export its own input configs anymore. Some unused * options are missing, such as theme. */ export type ConfirmConfig = BasicOptions & { transformer?: (value: boolean) => string; }; /** * Prompt a user to confirm a selection */ export async function confirm(message: string): Promise; /** * Prompt a user to confirm a selection * Will abort if nonInteractive and not force */ export async function confirm(opts: ConfirmConfig): Promise; export async function confirm(opts: string | ConfirmConfig) { if (typeof opts === "string") { opts = { message: opts }; } else { if (opts.force) { // TODO: Should we print what we've forced? return true; } const { shouldReturn, value } = guard(opts); if (shouldReturn) { return value; } } return inquirer.confirm(opts); } /** * A choice in a checkbox prompt. * Strongly typed to allow enum propagation */ export type Choice = { value: Value; name?: string; description?: string; short?: string; disabled?: boolean | string; checked?: boolean; type?: never; }; // Personal hack deviating from inquirer to allow string values to propagate // as strings or enum arrays without needing an explicit type at the call stie. type MaybeLiteral = Value extends string ? Value : never; /** * Options for the checkbox function. * * Exported because Inquirer does not export its own input configs anymore. Some unused * options are missing, such as theme. Some options are missing to promote consistency * within the CLI. */ export type CheckboxOptions = BasicOptions & { message: string; choices: | readonly (MaybeLiteral | inquirer.Separator)[] | readonly (inquirer.Separator | Choice)[]; validate?: | ((choices: readonly Choice[]) => boolean | string | Promise) | undefined; pageSize?: number; instructions?: string | boolean; }; /** * Prompt a user for one or more of many options. * Can accept a generic type for enum values. */ export async function checkbox(opts: CheckboxOptions): Promise { let optsWithDefault = opts; if (opts.default === undefined && Array.isArray(opts.choices)) { const checkedValues = (opts.choices as Choice[]) .filter((c) => c && typeof c === "object" && "checked" in c && c.checked) .map((c) => c.value); if (checkedValues.length > 0) { optsWithDefault = { ...opts, default: checkedValues }; } } const { shouldReturn, value } = guard(optsWithDefault); if (shouldReturn) { return value; } return inquirer.checkbox({ ...opts, loop: true, }); } /** * Options for the checkbox function. * * Exported because Inquirer does not export its own input configs anymore. Some unused * options are missing, such as theme. Some options are missing to promote consistency * within the CLI. * TODO: Had difficulty coalescing literals using Choice[]. Look into it. */ export type SelectOptions = BasicOptions & { choices: | readonly (MaybeLiteral | inquirer.Separator)[] | readonly (inquirer.Separator | Choice)[]; pageSize?: number; instructions?: string | { navigation: string; pager: string }; }; /** * Prompt a user to make a choice amongst a list. */ export async function select(opts: SelectOptions): Promise { const { shouldReturn, value } = guard(opts); if (shouldReturn) { return value; } const instructions = typeof opts.instructions === "string" ? { navigation: opts.instructions, pager: opts.instructions } : opts.instructions; return inquirer.select({ ...opts, instructions, loop: false, }); } /** * Options for the number function. * * Exported because Inquirer does not export its own input configs anymore. Some unused * options are missing, such as theme. Some options are missing to promote consistency * within the CLI. */ export type NumberOptions = BasicOptions & { min?: number; max?: number; step?: number | "any"; validate?: (value: number | undefined) => boolean | string | Promise; }; /** * Prompt a user for a number. */ export async function number(message: string): Promise; /** * Prompt a user for a number. */ export async function number(opts: NumberOptions): Promise; /** * Prompt a user for an optional number. */ export async function number( opts: NumberOptions & { required: false }, ): Promise; export async function number(opts: string | NumberOptions): Promise { if (typeof opts === "string") { opts = { message: opts }; } else { const { shouldReturn, value } = guard(opts); if (shouldReturn) { return value; } } return await inquirer.number({ required: true, ...opts }); } /** * Options for the checkbox function. * * Exported because Inquirer does not export its own input configs anymore. Some unused * options are missing, such as theme. Some options are missing to promote consistency * within the CLI. */ type PasswordOptions = Omit, "default"> & { validate?: (value: string) => boolean | string | Promise; }; /** * Prompt for a password; input is hidden. */ export async function password(message: string): Promise; /** * Prompt for a password; input is hidden. */ export async function password(opts: PasswordOptions): Promise; export async function password(opts: string | PasswordOptions): Promise { if (typeof opts === "string") { opts = { message: opts }; } else { // Note, without default can basically only throw guard(opts); } return inquirer.password({ ...opts, mask: "", }); } /** * Options for the search function. * * Exported because Inquirer does not export its own input configs anymore. Some unused * options are missing, such as theme. Some options are missing to promote consistency * within the CLI. */ export type SearchOptions = BasicOptions & { source: ( term: string | undefined, opt: { signal: AbortSignal; }, ) => | readonly (string | inquirer.Separator)[] | readonly (inquirer.Separator | Choice)[] | Promise | Promise)[]>; validate?: ((value: Value) => boolean | string | Promise) | undefined; pageSize?: number | undefined; }; /** Search for a value given a sorce callback. */ export async function search(opts: SearchOptions): Promise { const { shouldReturn, value } = guard(opts); if (shouldReturn) { return value; } return inquirer.search(opts); }