Skip to content

Commit 9dc9c54

Browse files
refactor: rename stopOnError to onError and change to a literal union
so we can expand the possible values later
1 parent c5acba2 commit 9dc9c54

File tree

2 files changed

+58
-25
lines changed

2 files changed

+58
-25
lines changed

plugins/parallel/readme.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,18 @@ commands:
5151
watch: true
5252
~~~
5353

54-
##### `stopOnError`
54+
##### `onError`
5555

56-
An optional `boolean`, defaulting to `false`. If `true`, when one of the parallel tasks throws an error, `Parallel` will stop the other tasks. This is useful for long-running tasks, e.g. a Node server and Webpack in `watch` mode, where one task erroring means you don't have everything running that you'd expect. If you're using `Parallel` to run shorter tasks in parallel as an optimisation, keep this as the default `false` so every task runs to completion and Tool Kit can show the final results of all the tasks.
56+
_optional_
57+
58+
| Value | Description |
59+
|-|-|
60+
| `'wait-for-others'` (default) | If any task errors, wait for the other tasks to complete, and print all the errors at the end. |
61+
| `'stop-all'` | If any task errors, immediately stop the other tasks, and print the error. |
62+
63+
For long-running tasks, e.g. a Node server and Webpack in `watch` mode, it's possible for one task to error, but its error logging to be buried by the other tasks' output, meaning you might have missed an error and aren't aware that not everything you expect to be running is still running. In these cases, set `onError: stop-all`; Tool Kit will exit every `Parallel` task if one of them errors, so you're always in a consistent state and don't miss any errors.
64+
65+
If you're using `Parallel` to run shorter tasks in parallel as an optimisation, keep this as the default `wait-for-others` so every task runs to completion and Tool Kit can show the final results of all the tasks.
5766

5867
<!-- hide autogenerated schema docs -->
5968

plugins/parallel/src/tasks/parallel.ts

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { z } from 'zod'
44
import { type ErrorSummary, handleTaskErrors, loadTasks } from 'dotcom-tool-kit/lib/tasks'
55
import { ValidConfig } from '@dotcom-tool-kit/config'
66
import type { WritableDeep } from 'type-fest'
7+
import { ToolKitError } from '@dotcom-tool-kit/error'
78

89
const ParallelSchema = z.object({
910
tasks: z.array(z.record(z.record(z.unknown()))),
10-
stopOnError: z.boolean().default(false)
11+
onError: z.union([z.literal('stop-all'), z.literal('wait-for-others')]).default('wait-for-others')
1112
}).describe(`Run Tool Kit tasks in parallel
1213
1314
### Task options
@@ -29,9 +30,18 @@ commands:
2930
watch: true
3031
~~~
3132
32-
#### \`stopOnError\`
33+
#### \`onError\`
3334
34-
An optional \`boolean\`, defaulting to \`false\`. If \`true\`, when one of the parallel tasks throws an error, \`Parallel\` will stop the other tasks. This is useful for long-running tasks, e.g. a Node server and Webpack in \`watch\` mode, where one task erroring means you don't have everything running that you'd expect. If you're using \`Parallel\` to run shorter tasks in parallel as an optimisation, keep this as the default \`false\` so every task runs to completion and Tool Kit can show the final results of all the tasks.
35+
_optional_
36+
37+
| Value | Description |
38+
|-|-|
39+
| \`'wait-for-others'\` (default) | If any task errors, wait for the other tasks to complete, and print all the errors at the end. |
40+
| \`'stop-all'\` | If any task errors, immediately stop the other tasks, and print the error. |
41+
42+
For long-running tasks, e.g. a Node server and Webpack in \`watch\` mode, it's possible for one task to error, but its error logging to be buried by the other tasks' output, meaning you might have missed an error and aren't aware that not everything you expect to be running is still running. In these cases, set \`onError: stop-all\`; Tool Kit will exit every \`Parallel\` task if one of them errors, so you're always in a consistent state and don't miss any errors.
43+
44+
If you're using \`Parallel\` to run shorter tasks in parallel as an optimisation, keep this as the default \`wait-for-others\` so every task runs to completion and Tool Kit can show the final results of all the tasks.
3545
3646
<!-- hide autogenerated schema docs -->
3747
`)
@@ -62,30 +72,38 @@ ${tasks
6272
await loadTasks(this.logger, tasks, context.config as WritableDeep<ValidConfig>)
6373
).unwrap('tasks are invalid!')
6474

65-
if (this.options.stopOnError) {
66-
try {
67-
await Promise.all(this.taskInstances.map((task) => task.run(context)))
68-
} catch (error) {
69-
this.stop()
70-
71-
throw error
75+
switch (this.options.onError) {
76+
case 'stop-all': {
77+
try {
78+
await Promise.all(this.taskInstances.map((task) => task.run(context)))
79+
} catch (error) {
80+
this.stop()
81+
throw error
82+
}
83+
break
7284
}
73-
} else {
74-
const errors: ErrorSummary[] = []
75-
76-
await Promise.all(
77-
this.taskInstances.map((task) =>
78-
task.run(context).catch((error: Error) => {
79-
errors.push({
80-
task: task.id,
81-
error
85+
case 'wait-for-others': {
86+
const errors: ErrorSummary[] = []
87+
88+
await Promise.all(
89+
this.taskInstances.map((task) =>
90+
task.run(context).catch((error: Error) => {
91+
errors.push({
92+
task: task.id,
93+
error
94+
})
8295
})
83-
})
96+
)
8497
)
85-
)
8698

87-
if (errors.length > 0) {
88-
handleTaskErrors(errors, context.command)
99+
if (errors.length > 0) {
100+
handleTaskErrors(errors, context.command)
101+
}
102+
103+
break
104+
}
105+
default: {
106+
this.assertUnreachable('Parallel.options.onError', this.options.onError)
89107
}
90108
}
91109
}
@@ -100,4 +118,10 @@ ${error.message}`)
100118
)
101119
)
102120
}
121+
122+
assertUnreachable(label: string, value: never): asserts value is never {
123+
const error = new ToolKitError(`Unexpected value for ${label}, received ${value}`)
124+
error.details = 'This should never happen! Talk to #cp-platforms-team, something weird af is going on'
125+
throw error
126+
}
103127
}

0 commit comments

Comments
 (0)