-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tasks): series and parallel can take a task array or record as a…
… first argument
- Loading branch information
Showing
3 changed files
with
58 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Task } from '../definitions'; | ||
import { context } from '../tasks/transform/context'; | ||
import { Members, Empty, TypeGuard } from 'type-core'; | ||
import { into } from 'pipettes'; | ||
|
||
export function flatten( | ||
task?: Task | Empty | Array<Task | Empty> | Members<Task | Empty>, | ||
...tasks: Array<Task | Empty> | ||
): Task[] { | ||
return into( | ||
null, | ||
() => { | ||
if (!task) return []; | ||
if (!TypeGuard.isObject(task)) return [task]; | ||
return Object.entries(task || {}).map(([key, task]): Task | Empty => { | ||
return task | ||
? context( | ||
(ctx) => ({ | ||
...ctx, | ||
route: ctx.route.concat(key) | ||
}), | ||
task | ||
) | ||
: null; | ||
}); | ||
}, | ||
(arr) => { | ||
return arr | ||
.concat(tasks) | ||
.filter((task): task is Task.Async => Boolean(task)); | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
import { Task, Context } from '../../definitions'; | ||
import { flatten } from '../../helpers/flatten'; | ||
import { isCancelled } from '../../utils/is-cancelled'; | ||
import { run } from '../../utils/run'; | ||
import { log } from '../stdio/log'; | ||
import { Empty } from 'type-core'; | ||
import { Empty, Members } from 'type-core'; | ||
import { into } from 'pipettes'; | ||
|
||
export function series(...tasks: Array<Task | Empty>): Task.Async { | ||
export function series( | ||
task?: Task | Empty | Array<Task | Empty> | Members<Task | Empty>, | ||
...tasks: Array<Task | Empty> | ||
): Task.Async { | ||
const items = flatten(task, ...tasks); | ||
|
||
return async (ctx: Context): Promise<void> => { | ||
into(ctx, log('debug', 'Run tasks in series')); | ||
|
||
for (let i = 0; i < tasks.length; i++) { | ||
for (const task of items) { | ||
if (await isCancelled(ctx)) break; | ||
|
||
const task = tasks[i]; | ||
|
||
if (task) { | ||
await run(task, { ...ctx, route: ctx.route.concat(i) }); | ||
} | ||
await run(task, ctx); | ||
} | ||
}; | ||
} |