Skip to content

Commit 4917d28

Browse files
sttkphated
authored andcommitted
New: Support --tasks-json flag for gulp 3.x
1 parent 45bb6d0 commit 4917d28

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

lib/versioned/^3.7.0/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
'use strict';
22

3+
var fs = require('fs');
4+
35
var log = require('gulplog');
46
var stdout = require('mute-stdout');
57

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

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

19-
if (opts.tasksSimple || opts.tasks) {
22+
if (opts.tasksSimple || opts.tasks || opts.tasksJson) {
2023
// Mute stdout if we are listing tasks
2124
stdout.mute();
2225
}
@@ -50,6 +53,22 @@ function execute(opts, env, config) {
5053
return gulpInst.tasks[task].fn;
5154
});
5255
}
56+
if (opts.tasksJson) {
57+
tree = taskTree(gulpInst.tasks);
58+
if (config.description && isString(config.description)) {
59+
tree.label = config.description;
60+
} else {
61+
tree.label = 'Tasks for ' + tildify(env.configPath);
62+
}
63+
64+
var output = JSON.stringify(copyTree(tree, opts));
65+
66+
if (typeof opts.tasksJson === 'boolean') {
67+
return console.log(output);
68+
}
69+
70+
return fs.writeFileSync(opts.tasksJson, output, 'utf-8');
71+
}
5372
gulpInst.start.apply(gulpInst, toRun);
5473
});
5574
}

lib/versioned/^3.7.0/task-tree.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@ module.exports = function(tasks) {
44
var map = {};
55
var arr = [];
66
Object.keys(tasks).forEach(function(taskname) {
7-
var task = { label: taskname, nodes: [], };
7+
var task = {
8+
label: taskname,
9+
type: 'task',
10+
nodes: [],
11+
};
812
map[taskname] = task;
913
arr.push(task);
1014
});
1115
Object.keys(tasks).forEach(function(taskname) {
1216
var task = map[taskname];
1317
tasks[taskname].dep.forEach(function(childname) {
14-
var child = map[childname] || { label: childname, nodes: [], };
18+
var child = map[childname] || {
19+
label: childname,
20+
type: 'task',
21+
nodes: [],
22+
};
1523
task.nodes.push(child);
1624
});
1725
});

test/lib/task-tree.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,22 @@ describe('lib: taskTree', function() {
2626
nodes: [
2727
{
2828
label: 'test',
29+
type: 'task',
2930
nodes: [
30-
{ label: 'dep1', nodes: [], },
31-
{ label: 'dep2', nodes: [], },
31+
{ label: 'dep1', type: 'task', nodes: [], },
32+
{ label: 'dep2', type: 'task', nodes: [], },
3233
],
3334
},
3435
{
3536
label: 'test2',
37+
type: 'task',
3638
nodes: [
37-
{ label: 'dep3', nodes: [], },
39+
{ label: 'dep3', type: 'task', nodes: [], },
3840
],
3941
},
4042
{
4143
label: 'test3',
44+
type: 'task',
4245
nodes: [],
4346
},
4447
],
@@ -66,33 +69,40 @@ describe('lib: taskTree', function() {
6669
nodes: [
6770
{
6871
label: 'test',
72+
type: 'task',
6973
nodes: [
7074
{
7175
label: 'test2',
76+
type: 'task',
7277
nodes: [
7378
{
7479
label: 'test3',
80+
type: 'task',
7581
nodes: [],
7682
},
7783
],
7884
},
7985
{
8086
label: 'test3',
87+
type: 'task',
8188
nodes: [],
8289
},
8390
],
8491
},
8592
{
8693
label: 'test2',
94+
type: 'task',
8795
nodes: [
8896
{
8997
label: 'test3',
98+
type: 'task',
9099
nodes: [],
91100
},
92101
],
93102
},
94103
{
95104
label: 'test3',
105+
type: 'task',
96106
nodes: [],
97107
},
98108
],

0 commit comments

Comments
 (0)