|
10 | 10 | * case explicitly. |
11 | 11 | */ |
12 | 12 |
|
| 13 | +import { PasswordPrompt } from '@clack/core'; |
| 14 | + |
13 | 15 | import { isInteractive } from './env.js'; |
14 | 16 | import { CLIError } from '../errors/base.js'; |
15 | 17 | import { ExitCode } from '../errors/codes'; |
@@ -59,13 +61,35 @@ export async function promptConfirm(options: { |
59 | 61 | return val as boolean; |
60 | 62 | } |
61 | 63 |
|
62 | | -export async function promptPassword(options: { |
63 | | - message: string; |
64 | | -}): Promise<string | undefined> { |
| 64 | +const PASSWORD_MASK_PREVIEW_LENGTH = 12; |
| 65 | +const PROMPT_THEME = '\x1b[38;2;248;103;58m'; // #F8673A |
| 66 | +const RESET_COLOR = '\x1b[0m'; |
| 67 | + |
| 68 | +function themed(value: string): string { |
| 69 | + return process.env.NO_COLOR ? value : `${PROMPT_THEME}${value}${RESET_COLOR}`; |
| 70 | +} |
| 71 | + |
| 72 | +export function passwordMaskPreview(length: number): string { |
| 73 | + const mask = '•'.repeat(Math.min(length, PASSWORD_MASK_PREVIEW_LENGTH)); |
| 74 | + return length > PASSWORD_MASK_PREVIEW_LENGTH ? `${mask}… (${length} chars)` : mask; |
| 75 | +} |
| 76 | + |
| 77 | +export async function promptPassword(options: { message: string }): Promise<string | undefined> { |
65 | 78 | if (!isInteractive()) return undefined; |
66 | 79 |
|
67 | | - const inquirer = await import('@clack/prompts'); |
68 | | - const val = await inquirer.password({ message: options.message, mask: '•' }); |
| 80 | + const val = await new PasswordPrompt({ |
| 81 | + mask: '•', |
| 82 | + render() { |
| 83 | + const length = typeof this.value === 'string' ? this.value.length : 0; |
| 84 | + const preview = passwordMaskPreview(length); |
| 85 | + if (this.state === 'submit') { |
| 86 | + return `${themed('│')}\n${themed('◇')} ${options.message}\n${themed('│')} ${preview}`; |
| 87 | + } |
| 88 | + if (this.state === 'cancel') return `${themed('│')}\n■ ${options.message}\n│ Cancelled`; |
| 89 | + return `${themed('│')}\n${themed('◆')} ${options.message}\n` |
| 90 | + + `${themed('│')} ${preview}${themed('_')}\n${themed('└')}`; |
| 91 | + }, |
| 92 | + }).prompt(); |
69 | 93 | if (typeof val === 'symbol') return undefined; |
70 | 94 | return val; |
71 | 95 | } |
|
0 commit comments