Skip to content

Instantly share code, notes, and snippets.

@MadLittleMods
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MadLittleMods/d4083d2ba35e2f850161 to your computer and use it in GitHub Desktop.
Save MadLittleMods/d4083d2ba35e2f850161 to your computer and use it in GitHub Desktop.
Made for this SO question: http://stackoverflow.com/q/28334314/796832
// Run the given tasks and returns their streams
// This will also take care of any task dependencies
//
// This is basically a custom gulp task orchestartor.
//
// Written for this SO question: http://stackoverflow.com/q/28334314/796832
// Gist: https://gist.github.com/MadLittleMods/d4083d2ba35e2f850161
//
// Usage:
// gulp.task('all-txt', function() {
// return es.merge.apply(null, runTasksAndGetStreams(['file1-txt', 'file2-txt']))
// .pipe(concat('all-text.txt'))
// .pipe(gulp.dest('dest'));
// });
//
// Params:
// taskNames: string or array of strings of task names
// debugLog: *optional* boolean to print some debug information to the console
function gulpRunTasksAndGetStreams(taskNames, /*optional*/debugLog) {
// You can pass in a single task or an array of tasks to complete
taskNames = [].concat(taskNames);
// We polyfill the pieces of `gulp-util` that we use in case some one wants to use it without installing `gulp-util`
var gutil;
try {
gutil = require('gulp-util');
}
catch(err) {
gutil = {
log: console.log,
colors: {
cyan: function(str) {
return str;
},
magenta: function(str) {
return str;
}
}
};
}
var resultantTaskInfo = [];
var taskMap = gulp.tasks;
// Satisfy all of the task dependencies, create a placeholder stream, and collect the func
// to make the real stream to feed in later when the dependencies are done `mergedDepStream.on('end')`
var mergedDepStream = null;
taskNames.forEach(function(taskName) {
var task = taskMap[taskName];
if(debugLog) {
gutil.log('root task:', gutil.colors.cyan(taskName), 'started working');
}
// Run any dependencies first
var depStreamResult = runDependenciesRecursivelyForTask(taskName, taskMap);
if(depStreamResult) {
mergedDepStream = mergedDepStream ? es.merge(mergedDepStream, depStreamResult) : depStreamResult;
}
if(debugLog) {
if(depStreamResult) {
depStreamResult.on('end', function() {
gutil.log('root task:', gutil.colors.cyan(taskName), 'deps done');
});
}
else {
gutil.log('root task:', gutil.colors.cyan(taskName), 'no deps present');
}
}
// Then push the task itself onto the list
resultantTaskInfo.push({
stream: es.through(),
fn: task.fn
});
});
// Once all of the dependencies have completed
mergedDepStream.on('end', function() {
if(debugLog) {
gutil.log('All dependencies done, piping in real root tasks');
}
// Pipe the actual task into our placeholder
resultantTaskInfo.forEach(function(taskInfo) {
var actualTaskStream = taskInfo.fn();
actualTaskStream.pipe(taskInfo.stream);
});
});
// Recursively gets all of dependencies for a task in order
function runDependenciesRecursivelyForTask(taskName, taskMap, mergedDependencyStream) {
var task = taskMap[taskName];
task.dep.forEach(function(depTaskName) {
var depTask = taskMap[depTaskName];
if(debugLog) {
gutil.log('dep task:', gutil.colors.cyan(depTaskName), 'started working');
}
// Dependencies can have dependencies
var recursiveStreamResult = null;
if(depTask.dep.length) {
recursiveStreamResult = runDependenciesRecursivelyForTask(depTaskName, taskMap, mergedDependencyStream);
mergedDependencyStream = mergedDependencyStream ? es.merge(mergedDependencyStream, recursiveStreamResult) : recursiveStreamResult;
}
if(depTask.fn) {
var whenStreamHandledCallback = function(/* we only use `noDeps` for logging */noDeps) {
if(debugLog) {
if(!noDeps) {
gutil.log('dep task:', gutil.colors.cyan(depTask.name), 'deps done');
}
else {
gutil.log('dep task:', gutil.colors.cyan(depTask.name), 'no deps present');
}
}
var depTaskStream = depTask.fn();
// Merge it in overall dependency progress stream
mergedDependencyStream = mergedDependencyStream ? es.merge(mergedDependencyStream, depTaskStream) : depTaskStream;
};
if(recursiveStreamResult === null) {
whenStreamHandledCallback(true);
}
else {
recursiveStreamResult.on('end', whenStreamHandledCallback);
}
}
});
return mergedDependencyStream;
}
// Return the (placeholder) streams which will get piped the real stream once the dependencies are done
return resultantTaskInfo.map(function(taskInfo) {
return taskInfo.stream;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment