Skip to content

Commit

Permalink
Support both Gulp 3.9.1 and 4.0.0
Browse files Browse the repository at this point in the history
Gulp 4.0.0 switched its task execution engine from `orchestrator` to
`undertaker`. As a result, certain methods and events from Gulp 3.9.1
upon which `slush` depended disappeared:

  gulpjs/gulp#755

Supporting Gulp 4.0.0 is important because Node 10 broke the
`graceful-fs` package upon which Gulp 3.9.1 depends. While there's a
workaround (updating the `natives` package), it places a burden on users
that still doesn't guarantee that Gulp 3.9.1 will remain future-proof:

  gulpjs/gulp#2146 (comment)
  gulpjs/gulp#2162 (comment)
  nodejs/node#19786 (comment)

As it turned out, the changes required to support both versions were
fairly straighforward, and should ensure that Slush remains future-proof
until the next major Gulp update.
  • Loading branch information
Bland, Mike committed May 26, 2018
1 parent 7f93732 commit 477f868
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions bin/slush.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,40 @@ function handleArguments(env) {
if (tasksFlag) {
return logTasks(generator.name, gulpInst);
}
gulpInst.start.apply(gulpInst, toRun);
runTask(gulpInst, toRun);
});
}

function runTask(gulpInst, toRun) {
var task,
metadata = { task: toRun },
start;

if (gulpInst.start) {
// Gulp <= 3.9.1 (unsupported, breaks under Node 10)
return gulpInst.start.apply(gulpInst, toRun);
}

// Gulp >= 4.0.0 (doesn't support events)
task = gulpInst.task(toRun);
if (task === undefined) {
return gulpInst.emit('task_not_found', metadata);
}
start = process.hrtime();
gulpInst.emit('task_start', metadata);

try {
task.apply(gulpInst);
metadata.hrDuration = process.hrtime(start);
gulpInst.emit('task_stop', metadata);
gulpInst.emit('stop');
} catch (err) {
err.hrDuration = process.hrtime(start);
err.task = metadata.task;
gulpInst.emit('task_err', err);
}
}

function logGenerators(generators) {
var tree = {
label: 'Installed generators',
Expand All @@ -121,7 +151,8 @@ function logGenerators(generators) {
}

function logTasks(name, localGulp) {
var tree = taskTree(localGulp.tasks);
// Gulp <= 3.9.1 uses gulp.tasks; Gulp >= 4.0.0 uses gulp.tree().
var tree = localGulp.tasks ? taskTree(localGulp.tasks) : localGulp.tree();
tree.label = 'Tasks for generator ' + chalk.magenta(name);
archy(tree).split('\n').forEach(function(v) {
if (v.trim().length === 0) return;
Expand Down

0 comments on commit 477f868

Please sign in to comment.