diff --git a/.changeset/curvy-lobsters-tell.md b/.changeset/curvy-lobsters-tell.md new file mode 100644 index 0000000..9b04a46 --- /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/src/index.ts b/packages/prompts/src/index.ts index 3ced5ed..254dcf8 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) { + const s = spinner(); + s.start(task.title); + const result = await task.task(s.message); + s.stop(result || task.title); + } + } +}; -- 2.51.2 From 8ba05324d5c2360a4915176609bca1c84dc55aa4 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Sun, 27 Aug 2023 14:59:09 +0300 Subject: [PATCH 2/3] doc: add Tasks --- packages/prompts/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/prompts/README.md b/packages/prompts/README.md index 767be88..9e8b5bf 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'; + }, + }, +]); +``` -- 2.51.2 From c1c1dda04a2d87c256b6f28f258f00590ecd28f4 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Sun, 27 Aug 2023 18:18:31 +0300 Subject: [PATCH 3/3] refactor: if statement --- packages/prompts/src/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/prompts/src/index.ts b/packages/prompts/src/index.ts index 254dcf8..4a764f3 100644 --- a/packages/prompts/src/index.ts +++ b/packages/prompts/src/index.ts @@ -781,11 +781,11 @@ export type Task = { */ export const tasks = async (tasks: Task[]) => { for (const task of tasks) { - if (task.enabled !== false) { - const s = spinner(); - s.start(task.title); - const result = await task.task(s.message); - s.stop(result || task.title); - } + if (task.enabled === false) continue; + + const s = spinner(); + s.start(task.title); + const result = await task.task(s.message); + s.stop(result || task.title); } };