tasuku 1.0.2 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  <p align="center">
3
2
  <img src=".github/tasuku.svg">
4
3
  <br>
@@ -11,7 +10,7 @@
11
10
  - Unopinionated
12
11
  - Type-safe
13
12
 
14
- → [Try it out online](https://stackblitz.com/edit/tasuku-demo?file=index.js)
13
+ → [Try it out online](https://stackblitz.com/edit/tasuku-demo?file=index.js&devtoolsheight=50&view=editor)
15
14
 
16
15
  <sub>Support this project by starring and sharing it. [Follow me](https://github.com/privatenumber) to see what other cool projects I'm working on.</sub>
17
16
 
@@ -20,6 +19,26 @@
20
19
  npm i tasuku
21
20
  ```
22
21
 
22
+ ## About
23
+ タスク (Tasuku) is a minimal task runner for Node.js. You can use it to label any task/function so that its loading, success, and error states are rendered in the terminal.
24
+
25
+ For example, here's a simple script that copies a file from path A to B.
26
+
27
+ ```ts
28
+ import { copyFile } from 'fs/promises'
29
+ import task from 'tasuku'
30
+
31
+ task('Copying file from path A to B', async ({ setTitle }) => {
32
+ await copyFile('/path/A', '/path/B')
33
+
34
+ setTitle('Successfully copied file from path A to B!')
35
+ })
36
+ ```
37
+
38
+ Running the script will look like this in the terminal:
39
+
40
+ <img src=".github/example-0.gif">
41
+
23
42
  ## Usage
24
43
  ### Task list
25
44
  Call `task(taskTitle, taskFunction)` to start a task and display it in a task list in the terminal.
@@ -64,11 +83,11 @@ task('Task 1', async () => {
64
83
  await someAsyncTask()
65
84
  })
66
85
 
67
- ...
86
+ // ...
68
87
 
69
88
  someOtherCode()
70
89
 
71
- ...
90
+ // ...
72
91
 
73
92
  task('Task 2', async ({ setTitle }) => {
74
93
  await someAsyncTask()
@@ -149,12 +168,12 @@ const groupedTasks = await task.group(task => [
149
168
  setTitle('Task 2 complete')
150
169
 
151
170
  return 'two'
152
- }),
171
+ })
153
172
 
154
- ...
173
+ // ...
155
174
  ])
156
175
 
157
- console.log(groupedTasks.results) // ['one', 'two']
176
+ console.log(groupedTasks) // [{ result: 'one' }, { result: 'two' }]
158
177
  ```
159
178
 
160
179
  <img src=".github/example-5.gif">
@@ -166,14 +185,15 @@ You can run tasks in parallel by passing in `{ concurrency: n }` as the second a
166
185
  const api = await task.group(task => [
167
186
  task(
168
187
  'Task 1',
169
- async () => await someAsyncTask(),
188
+ async () => await someAsyncTask()
170
189
  ),
171
-
190
+
172
191
  task(
173
192
  'Task 2',
174
- async () => await someAsyncTask(),
175
- ),
176
- ...
193
+ async () => await someAsyncTask()
194
+ )
195
+
196
+ // ...
177
197
  ], {
178
198
  concurrency: 2 // Number of tasks to run at a time
179
199
  })
@@ -190,14 +210,15 @@ Alternatively, you can also use the native `Promise.all()` if you prefer. The ad
190
210
  await Promise.all([
191
211
  task(
192
212
  'Task 1',
193
- async () => await someAsyncTask(),
213
+ async () => await someAsyncTask()
194
214
  ),
195
215
 
196
216
  task(
197
217
  'Task 2',
198
- async () => await someAsyncTask(),
199
- ),
200
- ...
218
+ async () => await someAsyncTask()
219
+ )
220
+
221
+ // ...
201
222
  ])
202
223
  ```
203
224
 
@@ -207,12 +228,15 @@ await Promise.all([
207
228
 
208
229
  Returns a Promise that resolves with object:
209
230
  ```ts
210
- {
211
- // The result from taskFunction
212
- result: any,
231
+ type TaskAPI = {
232
+ // Result from taskFunction
233
+ result: any
234
+
235
+ // State of the task
236
+ state: 'error' | 'warning' | 'success'
213
237
 
214
238
  // Invoke to clear the results from the terminal
215
- clear: () => void,
239
+ clear: () => void
216
240
  }
217
241
  ```
218
242
 
@@ -226,13 +250,13 @@ The name of the task displayed.
226
250
  #### taskFunction
227
251
  Type:
228
252
  ```ts
229
- ({
230
- task: taskFunction,
231
- setTitle: (title: string) => void,
232
- setStatus: (status: string) => void,
233
- setOutput: (error: string | { message: string }) => void,
234
- setWarning: (warning: Error | string) => void,
235
- setError: (error: Error | string) => void,
253
+ type TaskFunction = (taskInnerApi: {
254
+ task: createTask
255
+ setTitle(title: string): void
256
+ setStatus(status?: string): void
257
+ setOutput(output: string | { message: string }): void
258
+ setWarning(warning: Error | string): void
259
+ setError(error: Error | string): void
236
260
  }) => Promise<any>
237
261
  ```
238
262
 
@@ -265,12 +289,20 @@ Call with a string or Error instance to put the task in an error state. Tasks au
265
289
  ### task.group(createTaskFunctions, options)
266
290
  Returns a Promise that resolves with object:
267
291
  ```ts
268
- {
269
- // The results from the taskFunctions
270
- results: any[],
292
+ // The results from the taskFunctions
293
+ type TaskGroupAPI = {
294
+ // Result from taskFunction
295
+ result: any
271
296
 
272
- // Invoke to clear the results from the terminal
273
- clear: () => void,
297
+ // State of the task
298
+ state: 'error' | 'warning' | 'success'
299
+
300
+ // Invoke to clear the task result
301
+ clear: () => void
302
+ }[] & {
303
+
304
+ // Invoke to clear ALL results
305
+ clear: () => void
274
306
  }
275
307
  ```
276
308