Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add password prompt #366

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Example: `consola.info('Message')`

#### `await prompt(message, { type, cancel })`

Show an input prompt. Type can either of `text`, `confirm`, `select` or `multiselect`.
Show an input prompt. Type can either of `text`, `password`, `confirm`, `select` or `multiselect`.

If prompt is canceled by user (with Ctrol+C), default value will be resolved by default. This strategy can be configured by setting `{ cancel: "..." }` option:

Expand Down
4 changes: 4 additions & 0 deletions examples/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
initial: ["eslint", "prettier"],
});

const password = await consola.prompt("Enter your password.", {
type: "password",
});

Check warning on line 36 in examples/prompt.ts

View check run for this annotation

Codecov / codecov/patch

examples/prompt.ts#L34-L36

Added lines #L34 - L36 were not covered by tests

consola.start("Creating project...");
await new Promise((resolve) => setTimeout(resolve, 1000));
consola.success("Project created!");
Expand Down
39 changes: 35 additions & 4 deletions src/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { text, confirm, select, multiselect } from "@clack/prompts";
import { text, confirm, select, multiselect, password } from "@clack/prompts";

type SelectOption = {
label: string;
Expand Down Expand Up @@ -103,14 +103,34 @@
required?: boolean;
};

export type PasswordPromptOptions = PromptCommonOptions & {
/**
* Specifies the prompt type as password.
*/
type: "password";

/**
* The mask character for the password prompt.
* @optional
*/
mask?: string;

/**
* The validation function for the password prompt.
* @optional
*/
validate?: (value: string) => string | Error | undefined;
};

/**
* Defines a combined type for all prompt options.
*/
export type PromptOptions =
| TextPromptOptions
| ConfirmPromptOptions
| SelectPromptOptions
| MultiSelectOptions;
| MultiSelectOptions
| PasswordPromptOptions;

type inferPromptReturnType<T extends PromptOptions> =
T extends TextPromptOptions
Expand All @@ -123,7 +143,9 @@
: T["options"][number]
: T extends MultiSelectOptions
? T["options"]
: unknown;
: T extends PasswordPromptOptions
? string
: unknown;

type inferPromptCancalReturnType<T extends PromptOptions> = T extends {
cancel: "reject";
Expand Down Expand Up @@ -183,7 +205,8 @@
}
default:
case "default": {
return (opts as TextPromptOptions).default ?? opts.initial;
if (opts.type === "password") return undefined;
return (opts as TextPromptOptions).default ?? (opts as any).initial;

Check warning on line 209 in src/prompt.ts

View check run for this annotation

Codecov / codecov/patch

src/prompt.ts#L208-L209

Added lines #L208 - L209 were not covered by tests
}
}
};
Expand Down Expand Up @@ -225,5 +248,13 @@
}).then(handleCancel)) as any;
}

if (opts.type === "password") {
return (await password({
message,
mask: opts.mask ?? "", // default mask does not display characters.
validate: opts.validate,
}).then(handleCancel)) as any;
}

Check warning on line 257 in src/prompt.ts

View check run for this annotation

Codecov / codecov/patch

src/prompt.ts#L251-L257

Added lines #L251 - L257 were not covered by tests

throw new Error(`Unknown prompt type: ${opts.type}`);
}