Skip to content

Commit

Permalink
chore: rename builder to workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
orochaa committed Aug 11, 2023
1 parent 3742b91 commit 232c31d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .changeset/red-walls-greet.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@clack/prompts': minor
---

add prompt `builder`
add prompt `workflow` builder
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"basic": "jiti ./basic.ts",
"spinner": "jiti ./spinner.ts",
"changesets": "jiti ./changesets.ts",
"builder": "jiti ./builder.ts"
"workflow": "jiti ./workflow.ts"
},
"devDependencies": {
"jiti": "^1.17.0"
Expand Down
20 changes: 10 additions & 10 deletions examples/builder.ts → examples/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as p from '@clack/prompts';

(async () => {
const results = await p
.builder()
.add('path', () =>
.workflow()
.step('path', () =>
p.text({
message: 'Where should we create your project?',
placeholder: './sparkling-solid',
Expand All @@ -13,7 +13,7 @@ import * as p from '@clack/prompts';
},
})
)
.add('password', () =>
.step('password', () =>
p.password({
message: 'Provide a password',
validate: (value) => {
Expand All @@ -22,7 +22,7 @@ import * as p from '@clack/prompts';
},
})
)
.add('type', ({ results }) =>
.step('type', ({ results }) =>
p.select({
message: `Pick a project type within "${results.path}"`,
initialValue: 'ts',
Expand All @@ -37,7 +37,7 @@ import * as p from '@clack/prompts';
],
})
)
.add('tools', () =>
.step('tools', () =>
p.multiselect({
message: 'Select additional tools.',
initialValues: ['prettier', 'eslint'],
Expand All @@ -49,7 +49,7 @@ import * as p from '@clack/prompts';
],
})
)
.add('install', ({ results }) =>
.step('install', ({ results }) =>
p.confirm({
message: 'Install dependencies?',
initialValue: false,
Expand All @@ -58,11 +58,11 @@ import * as p from '@clack/prompts';
.run();

await p
.builder()
.add('cancel', () => p.text({ message: 'Try cancel prompt (Ctrl + C):' }))
.add('afterCancel', () => p.text({ message: 'This will not appear!' }))
.workflow()
.step('cancel', () => p.text({ message: 'Try cancel prompt (Ctrl + C):' }))
.step('afterCancel', () => p.text({ message: 'This will not appear!' }))
.onCancel(({ results }) => {
p.cancel('Builder canceled');
p.cancel('Workflow canceled');
process.exit(0);
})
.run();
Expand Down
16 changes: 8 additions & 8 deletions packages/prompts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ s.stop('Installed via npm');

## Utilities

### Grouping
### Group

Grouping prompts together is a great way to keep your code organized. This accepts a JSON object with a name that can be used to reference the group later. The second argument is an optional but has a `onCancel` callback that will be called if the user cancels one of the prompts in the group.

Expand Down Expand Up @@ -157,18 +157,18 @@ const group = await p.group(
console.log(group.name, group.age, group.color);
```

### Building
### Workflow

Just like `group`, but on `builder` way, so you can choose which one fits better.
Just like `group`, but on builder way, so you can choose which one fits better.

```js
import * as p from '@clack/prompts';

const results = await p
.builder()
.add('name', () => p.text({ message: 'What is your name?' }))
.add('age', () => p.text({ message: 'What is your age?' }))
.add('color', ({ results }) =>
.workflow()
.step('name', () => p.text({ message: 'What is your name?' }))
.step('age', () => p.text({ message: 'What is your age?' }))
.step('color', ({ results }) =>
p.multiselect({
message: `What is your favorite color ${results.name}?`,
options: [
Expand All @@ -179,7 +179,7 @@ const results = await p
})
)
.onCancel(() => {
p.cancel('Builder canceled');
p.cancel('Workflow canceled');
process.exit(0);
})
.run();
Expand Down
18 changes: 9 additions & 9 deletions packages/prompts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,32 +770,32 @@ export const group = async <T>(
return results;
};

type NextPromptBuilder<
type NextWorkflowBuilder<
TResults extends Record<string, unknown>,
TKey extends string,
TResult
> = PromptBuilder<
> = WorkflowBuilder<
{
[Key in keyof TResults]: Key extends TKey ? TResult : TResults[Key];
} & {
[Key in TKey]: TResult;
}
>;

class PromptBuilder<TResults extends Record<string, unknown> = {}> {
class WorkflowBuilder<TResults extends Record<string, unknown> = {}> {
private results: TResults = {} as TResults;
private prompts: Record<string, PromptWithOptions<TResults, unknown>> = {};
private cancelCallback: PromptWithOptions<Partial<TResults>, void> | undefined;

public add<TKey extends string, TResult>(
public step<TKey extends string, TResult>(
key: TKey extends keyof TResults ? never : TKey,
prompt: PromptWithOptions<TResults, TResult>
): NextPromptBuilder<TResults, TKey, TResult> {
): NextWorkflowBuilder<TResults, TKey, TResult> {
this.prompts[key] = prompt;
return this as NextPromptBuilder<TResults, TKey, TResult>;
return this as NextWorkflowBuilder<TResults, TKey, TResult>;
}

public onCancel(cb: PromptWithOptions<Partial<TResults>, void>): PromptBuilder<TResults> {
public onCancel(cb: PromptWithOptions<Partial<TResults>, void>): WorkflowBuilder<TResults> {
this.cancelCallback = cb;
return this;
}
Expand All @@ -814,6 +814,6 @@ class PromptBuilder<TResults extends Record<string, unknown> = {}> {
}
}

export const builder = () => {
return new PromptBuilder();
export const workflow = () => {
return new WorkflowBuilder();
};

0 comments on commit 232c31d

Please sign in to comment.