Skip to content

Commit

Permalink
New: Support --tasks-json flag for gulp 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk authored and phated committed Dec 21, 2017
1 parent 45bb6d0 commit 4917d28
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
21 changes: 20 additions & 1 deletion lib/versioned/^3.7.0/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';

var fs = require('fs');

var log = require('gulplog');
var stdout = require('mute-stdout');

var taskTree = require('./task-tree');
var copyTree = require('../../shared/log/copy-tree');

var tildify = require('../../shared/tildify');
var logTasks = require('../../shared/log/tasks');
Expand All @@ -16,7 +19,7 @@ function execute(opts, env, config) {
var tasks = opts._;
var toRun = tasks.length ? tasks : ['default'];

if (opts.tasksSimple || opts.tasks) {
if (opts.tasksSimple || opts.tasks || opts.tasksJson) {
// Mute stdout if we are listing tasks
stdout.mute();
}
Expand Down Expand Up @@ -50,6 +53,22 @@ function execute(opts, env, config) {
return gulpInst.tasks[task].fn;
});
}
if (opts.tasksJson) {
tree = taskTree(gulpInst.tasks);
if (config.description && isString(config.description)) {
tree.label = config.description;
} else {
tree.label = 'Tasks for ' + tildify(env.configPath);
}

var output = JSON.stringify(copyTree(tree, opts));

if (typeof opts.tasksJson === 'boolean') {
return console.log(output);
}

return fs.writeFileSync(opts.tasksJson, output, 'utf-8');
}
gulpInst.start.apply(gulpInst, toRun);
});
}
Expand Down
12 changes: 10 additions & 2 deletions lib/versioned/^3.7.0/task-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ module.exports = function(tasks) {
var map = {};
var arr = [];
Object.keys(tasks).forEach(function(taskname) {
var task = { label: taskname, nodes: [], };
var task = {
label: taskname,
type: 'task',
nodes: [],
};
map[taskname] = task;
arr.push(task);
});
Object.keys(tasks).forEach(function(taskname) {
var task = map[taskname];
tasks[taskname].dep.forEach(function(childname) {
var child = map[childname] || { label: childname, nodes: [], };
var child = map[childname] || {
label: childname,
type: 'task',
nodes: [],
};
task.nodes.push(child);
});
});
Expand Down
16 changes: 13 additions & 3 deletions test/lib/task-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ describe('lib: taskTree', function() {
nodes: [
{
label: 'test',
type: 'task',
nodes: [
{ label: 'dep1', nodes: [], },
{ label: 'dep2', nodes: [], },
{ label: 'dep1', type: 'task', nodes: [], },
{ label: 'dep2', type: 'task', nodes: [], },
],
},
{
label: 'test2',
type: 'task',
nodes: [
{ label: 'dep3', nodes: [], },
{ label: 'dep3', type: 'task', nodes: [], },
],
},
{
label: 'test3',
type: 'task',
nodes: [],
},
],
Expand Down Expand Up @@ -66,33 +69,40 @@ describe('lib: taskTree', function() {
nodes: [
{
label: 'test',
type: 'task',
nodes: [
{
label: 'test2',
type: 'task',
nodes: [
{
label: 'test3',
type: 'task',
nodes: [],
},
],
},
{
label: 'test3',
type: 'task',
nodes: [],
},
],
},
{
label: 'test2',
type: 'task',
nodes: [
{
label: 'test3',
type: 'task',
nodes: [],
},
],
},
{
label: 'test3',
type: 'task',
nodes: [],
},
],
Expand Down

0 comments on commit 4917d28

Please sign in to comment.