diff --git a/.changeset/curvy-lobsters-tell.md b/.changeset/curvy-lobsters-tell.md new file mode 100644 index 00000000..9b04a46d --- /dev/null +++ b/.changeset/curvy-lobsters-tell.md @@ -0,0 +1,5 @@ +--- +'@clack/prompts': minor +--- + +Add tasks function for executing tasks in spinners diff --git a/packages/prompts/README.md b/packages/prompts/README.md index 767be885..9e8b5bf8 100644 --- a/packages/prompts/README.md +++ b/packages/prompts/README.md @@ -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'; + }, + }, +]); +``` diff --git a/packages/prompts/src/index.ts b/packages/prompts/src/index.ts index 3ced5ed0..4a764f32 100644 --- a/packages/prompts/src/index.ts +++ b/packages/prompts/src/index.ts @@ -759,3 +759,33 @@ export const group = async ( return results; }; + +export type Task = { + /** + * Task title + */ + title: string; + /** + * Task function + */ + task: (message: (string: string) => void) => string | Promise | void | Promise; + + /** + * 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); + } +};