[READ-ONLY] Mirror of https://github.com/bombshell-dev/clack. Effortlessly build beautiful command-line apps bomb.sh/docs/clack/basics/getting-started/
cli command-line command-line-app node prompt prompts
5

Configure Feed

Select the types of activity you want to include in your feed.

async validate

Oskar Löfgren (Mar 6, 2023, 12:49 AM +0100) 46163fa1 f44c1042

+11 -6
+1 -1
packages/core/src/index.ts
··· 3 3 export { default as MultiSelectPrompt } from './prompts/multi-select'; 4 4 export { default as PasswordPrompt } from './prompts/password'; 5 5 export { default as Prompt, isCancel } from './prompts/prompt'; 6 - export type { State } from './prompts/prompt'; 6 + export type { State, Validator } from './prompts/prompt'; 7 7 export { default as SelectPrompt } from './prompts/select'; 8 8 export { default as SelectKeyPrompt } from './prompts/select-key'; 9 9 export { default as TextPrompt } from './prompts/text';
+7 -3
packages/core/src/prompts/prompt.ts
··· 38 38 ]); 39 39 const keys = new Set(['up', 'down', 'left', 'right', 'space', 'enter']); 40 40 41 + export interface Validator<Value = any> { 42 + (value: Value): string | void | Promise<string | void>; 43 + } 44 + 41 45 export interface PromptOptions<Self extends Prompt> { 42 46 render(this: Omit<Self, 'prompt'>): string | void; 43 47 placeholder?: string; 44 48 initialValue?: any; 45 - validate?: ((value: any) => string | void) | undefined; 49 + validate?: Validator; 46 50 input?: Readable; 47 51 output?: Writable; 48 52 debug?: boolean; ··· 153 157 this.subscribers.clear(); 154 158 } 155 159 156 - private onKeypress(char: string, key?: Key) { 160 + private async onKeypress(char: string, key?: Key) { 157 161 if (this.state === 'error') { 158 162 this.state = 'active'; 159 163 } ··· 172 176 173 177 if (key?.name === 'return') { 174 178 if (this.opts.validate) { 175 - const problem = this.opts.validate(this.value); 179 + const problem = await this.opts.validate(this.value); 176 180 if (problem) { 177 181 this.error = problem; 178 182 this.state = 'error';
+3 -2
packages/prompts/src/index.ts
··· 9 9 SelectPrompt, 10 10 State, 11 11 TextPrompt, 12 + Validator, 12 13 } from '@clack/core'; 13 14 import isUnicodeSupported from 'is-unicode-supported'; 14 15 import color from 'picocolors'; ··· 63 64 placeholder?: string; 64 65 defaultValue?: string; 65 66 initialValue?: string; 66 - validate?: (value: string) => string | void; 67 + validate?: Validator<string>; 67 68 } 68 69 export const text = (opts: TextOptions) => { 69 70 return new TextPrompt({ ··· 99 100 export interface PasswordOptions { 100 101 message: string; 101 102 mask?: string; 102 - validate?: (value: string) => string | void; 103 + validate?: Validator<string>; 103 104 } 104 105 export const password = (opts: PasswordOptions) => { 105 106 return new PasswordPrompt({