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/prompts): add tasks #154

Merged
merged 3 commits into from
Aug 29, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/curvy-lobsters-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/prompts': minor
---

Add tasks function for executing tasks in spinners
16 changes: 16 additions & 0 deletions packages/prompts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,19 @@ const group = await p.group(

console.log(group.name, group.age, group.color);
```

### Tasks

Execute multiple tasks in spinners.

```js
await p.tasks([
{
title: 'Installing via npm',
task: async (message) => {
// Do installation here
return 'Installed via npm';
},
},
]);
```
30 changes: 30 additions & 0 deletions packages/prompts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,3 +759,33 @@ export const group = async <T>(

return results;
};

export type Task = {
/**
* Task title
*/
title: string;
/**
* Task function
*/
task: (message: (string: string) => void) => string | Promise<string> | void | Promise<void>;

/**
* If enabled === false the task will be skipped
*/
enabled?: boolean;
};

/**
* Define a group of tasks to be executed
*/
export const tasks = async (tasks: Task[]) => {
for (const task of tasks) {
if (task.enabled === false) continue;

const s = spinner();
s.start(task.title);
const result = await task.task(s.message);
s.stop(result || task.title);
}
};
Loading