Skip to content

Commit

Permalink
feat(@clack/prompts): add tasks (bombshell-dev#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpreston321 committed Aug 29, 2023
2 parents 9d0e0dc + c1c1dda commit 65dfd4f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
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 @@ -774,3 +774,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);
}
};

0 comments on commit 65dfd4f

Please sign in to comment.