Skip to content

Commit 65dfd4f

Browse files
authored
feat(@clack/prompts): add tasks (#154)
2 parents 9d0e0dc + c1c1dda commit 65dfd4f

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

.changeset/curvy-lobsters-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clack/prompts': minor
3+
---
4+
5+
Add tasks function for executing tasks in spinners

packages/prompts/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,19 @@ const group = await p.group(
156156

157157
console.log(group.name, group.age, group.color);
158158
```
159+
160+
### Tasks
161+
162+
Execute multiple tasks in spinners.
163+
164+
```js
165+
await p.tasks([
166+
{
167+
title: 'Installing via npm',
168+
task: async (message) => {
169+
// Do installation here
170+
return 'Installed via npm';
171+
},
172+
},
173+
]);
174+
```

packages/prompts/src/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,3 +774,33 @@ export const group = async <T>(
774774

775775
return results;
776776
};
777+
778+
export type Task = {
779+
/**
780+
* Task title
781+
*/
782+
title: string;
783+
/**
784+
* Task function
785+
*/
786+
task: (message: (string: string) => void) => string | Promise<string> | void | Promise<void>;
787+
788+
/**
789+
* If enabled === false the task will be skipped
790+
*/
791+
enabled?: boolean;
792+
};
793+
794+
/**
795+
* Define a group of tasks to be executed
796+
*/
797+
export const tasks = async (tasks: Task[]) => {
798+
for (const task of tasks) {
799+
if (task.enabled === false) continue;
800+
801+
const s = spinner();
802+
s.start(task.title);
803+
const result = await task.task(s.message);
804+
s.stop(result || task.title);
805+
}
806+
};

0 commit comments

Comments
 (0)