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(@clack/core,@clack/prompts): Adding a new warn state to the validate method #198

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
8 changes: 4 additions & 4 deletions examples/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ async function main() {
message: 'Where should we create your project?',
placeholder: './sparkling-solid',
validate: (value) => {
if (!value) return 'Please enter a path.';
if (value[0] !== '.') return 'Please enter a relative path.';
if (!value) return { status: 'error', message: 'Please enter a path.' };
if (value[0] !== '.') return { status: 'warn', message: 'warn: Relative path may not work' };
},
}),
password: () =>
p.password({
message: 'Provide a password',
validate: (value) => {
if (!value) return 'Please enter a password.';
if (value.length < 5) return 'Password should have at least 5 characters.';
if (!value) return { status: 'error', message: 'Please enter a password.' };
if (value.length < 5) return { status: 'warn', message: 'warn: Password security is too low' };
},
}),
type: ({ results }) =>
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export { default as GroupMultiSelectPrompt } from './prompts/group-multiselect';
export { default as MultiSelectPrompt } from './prompts/multi-select';
export { default as PasswordPrompt } from './prompts/password';
export { default as Prompt, isCancel } from './prompts/prompt';
export type { State } from './prompts/prompt';
export type { State, ValidateType } from './prompts/prompt';
export { default as SelectPrompt } from './prompts/select';
export { default as SelectKeyPrompt } from './prompts/select-key';
export { default as TextPrompt } from './prompts/text';
Expand Down
37 changes: 25 additions & 12 deletions packages/core/src/prompts/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ const aliases = new Map([
]);
const keys = new Set(['up', 'down', 'left', 'right', 'space', 'enter']);

export type ValidateType = ((value: any) => ({ status: 'error' | 'warn', message: string } | void));
export interface PromptOptions<Self extends Prompt> {
render(this: Omit<Self, 'prompt'>): string | void;
placeholder?: string;
initialValue?: any;
validate?: ((value: any) => string | void) | undefined;
validate?: ValidateType;
input?: Readable;
output?: Writable;
debug?: boolean;
}

export type State = 'initial' | 'active' | 'cancel' | 'submit' | 'error';
export type State = 'initial' | 'active' | 'cancel' | 'submit' | 'error' | 'warn';

export default class Prompt {
protected input: Readable;
Expand All @@ -62,6 +63,7 @@ export default class Prompt {
public state: State = 'initial';
public value: any;
public error: string = '';
public warn: string = ''

constructor(
{ render, input = stdin, output = stdout, ...opts }: PromptOptions<Prompt>,
Expand Down Expand Up @@ -154,9 +156,10 @@ export default class Prompt {
}

private onKeypress(char: string, key?: Key) {
if (this.state === 'error') {
if ((this.state === 'error' || this.state === 'warn') && key?.name !== 'return') {
this.state = 'active';
}

if (key?.name && !this._track && aliases.has(key.name)) {
this.emit('cursor', aliases.get(key.name));
}
Expand All @@ -176,18 +179,28 @@ export default class Prompt {
this.emit('key', char.toLowerCase());
}


if (key?.name === 'return') {
if (this.opts.validate) {
const problem = this.opts.validate(this.value);
if (problem) {
this.error = problem;
this.state = 'error';
this.rl.write(this.value);
if (this.state === 'warn') {
this.state = 'submit'
} else {
if (this.opts.validate) {
const problem = this.opts.validate(this.value);
if (problem?.status === 'error') {
this.error = problem.message;
this.state = 'error';
this.rl.write(this.value);
} else if (problem?.status === 'warn') {
this.warn = problem.message;
this.state = 'warn';
this.rl.write(this.value);
} else {
this.state = 'submit';
}
} else {
this.state = 'submit';
}
}
if (this.state !== 'error') {
this.state = 'submit';
}
}
if (char === '\x03') {
this.state = 'cancel';
Expand Down
9 changes: 7 additions & 2 deletions packages/prompts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ const meaning = await text({
placeholder: 'Not sure',
initialValue: '42',
validate(value) {
if (value.length === 0) return `Value is required!`;
},
if (value.length === 0) {
return {
status:'error',
message:'Value is required!'
};
}
}
});
```

Expand Down
Loading