Last active
August 29, 2015 14:05
-
-
Save sirlancelot/6920d0d6beb41b2c4283 to your computer and use it in GitHub Desktop.
Run a series of gulp tasks followed by a callback when they are complete. As long as all sub-tasks properly identify themselves as sync, async, stream, or promise, the callback will not execute until all sub-tasks are complete. Standard Gulp principles apply around dependencies as well.
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
var gulp = require("gulp"); | |
module.exports = runTasks; | |
// Run a series of subtasks and execute a callback when they're complete. | |
function runTasks(subTasks, next) { | |
// If no subTasks were provided, just move on. | |
if (!subTasks || !subTasks.length) | |
return void (typeof next === "function" && next()); | |
// Always use an array. | |
if (!Array.isArray(subTasks)) subTasks = [subTasks] | |
// Use Gulp's allDone method and inject our subTasks | |
var allDone = gulp.allDone.bind({ seq: subTasks, tasks: gulp.tasks }) | |
// Listen for task completes | |
gulp.addListener("task_stop", check) | |
gulp.addListener("task_err", check) | |
// Add the subtasks to the sequence. | |
gulp.start(subTasks) | |
function check() { | |
// Bail if our subTasks haven't all completed. | |
if (!allDone()) return; | |
gulp.removeListener("task_stop", check) | |
gulp.removeListener("task_err", check) | |
if (typeof next === "function") next() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment