Skip to content

Commit 4c4abfd

Browse files
sttkphated
authored andcommitted
Update: Apply depth/compact flags to tasks-json/tasks w/ .gulp.* support & add sort flag (closes #57) (#75)
1 parent 090d601 commit 4c4abfd

38 files changed

+1271
-120
lines changed

README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,14 @@ Configuration from the home directory (`~`) and current working directory (`cwd`
103103

104104
Supported configurations properties:
105105

106-
| Property | Description |
107-
|----------------|-------------|
108-
| description | Top-level description of the project/gulpfile (Replaces "Tasks for ~/path/of/gulpfile.js") |
109-
| flags.gulpfile | Set a default gulpfile |
110-
| flags.silent | Silence logging by default |
106+
| Property | Description |
107+
|--------------------|-------------|
108+
| description | Top-level description of the project/gulpfile (Replaces "Tasks for ~/path/of/gulpfile.js") |
109+
| flags.continue | Continue execution of tasks upon failure by default. |
110+
| flags.compactTasks | Reduce the output of task dependency tree by default. |
111+
| flags.tasksDepth | Set default depth of task dependency tree. |
112+
| flags.gulpfile | Set a default gulpfile |
113+
| flags.silent | Silence logging by default |
111114

112115
## Flags
113116

@@ -159,11 +162,6 @@ __Some flags only work with gulp 4 and will be ignored when invoked against gulp
159162
<td>-T</td>
160163
<td>Print the task dependency tree for the loaded gulpfile.</td>
161164
</tr>
162-
<tr>
163-
<td>--depth [number]</td>
164-
<td></td>
165-
<td>Specify the depth of the task dependency tree to print.</td>
166-
</tr>
167165
<tr>
168166
<td>--tasks-simple</td>
169167
<td></td>
@@ -174,6 +172,21 @@ __Some flags only work with gulp 4 and will be ignored when invoked against gulp
174172
<td></td>
175173
<td>Print the task dependency tree, in JSON format, for the loaded gulpfile. The [path] argument is optional, and if given writes the JSON to the path.</td>
176174
</tr>
175+
<tr>
176+
<td>--tasks-depth [number]</td>
177+
<td></td>
178+
<td>Specify the depth of the task dependency tree to print. This flag can be used with --tasks or --tasks-json. (This flag was named --depth before but is deprecated.)</td>
179+
</tr>
180+
<tr>
181+
<td>--compact-tasks</td>
182+
<td></td>
183+
<td>Reduce the output of task dependency tree by printing only top tasks and their child tasks. This flag can be used with --tasks or --tasks-json.</td>
184+
</tr>
185+
<tr>
186+
<td>--sort-tasks</td>
187+
<td></td>
188+
<td>Will sort top tasks of task dependency tree. This flag can be used with --tasks.</td>
189+
</tr>
177190
<tr>
178191
<td>--color</td>
179192
<td></td>

docs/CLI.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ gulp has very few flags to know about. All other flags are for tasks to use if n
5959
**--tasks-json** [path]
6060
Print the task dependency tree, in JSON format, for the loaded gulpfile. The [path] argument is optional, and if given writes the JSON to the path.
6161

62+
**--tasks-depth** [number]
63+
Specify the depth of the task dependency tree to print. This flag can be used with --tasks or --tasks-json. (This flag was named --depth before but is deprecated.)
64+
65+
**--compact-tasks**
66+
Reduce the output of task dependency tree by printing only top tasks and their child tasks. This flag can be used with --tasks or --tasks-json.
67+
68+
**--sort-tasks**
69+
Will sort top tasks of task dependency tree. This flag can be used with --tasks.
70+
6271
**--color**
6372
Will force gulp and gulp plugins to display colors, even when no color support is detected.
6473

lib/shared/cliOptions.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ module.exports = {
4747
desc: chalk.gray(
4848
'Print the task dependency tree for the loaded gulpfile.'),
4949
},
50-
depth: {
51-
type: 'number',
52-
requiresArg: true,
53-
desc: chalk.gray(
54-
'Specify the depth of the task dependency tree.'),
55-
},
5650
'tasks-simple': {
5751
type: 'boolean',
5852
desc: chalk.gray(
@@ -63,6 +57,24 @@ module.exports = {
6357
'Print the task dependency tree, ' +
6458
'in JSON format, for the loaded gulpfile.'),
6559
},
60+
'tasks-depth': {
61+
alias: 'depth',
62+
type: 'number',
63+
requiresArg: true,
64+
desc: chalk.gray(
65+
'Specify the depth of the task dependency tree.'),
66+
},
67+
'compact-tasks': {
68+
type: 'boolean',
69+
desc: chalk.gray(
70+
'Reduce the output of task dependency tree by printing ' +
71+
'only top tasks and their child tasks.'),
72+
},
73+
'sort-tasks': {
74+
type: 'boolean',
75+
desc: chalk.gray(
76+
'Will sort top tasks of task dependency tree.'),
77+
},
6678
color: {
6779
type: 'boolean',
6880
desc: chalk.gray(

lib/shared/config/cli-flags.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ var fromTo = {
66
'flags.silent': 'silent',
77
'flags.continue': 'continue',
88
'flags.logLevel': 'logLevel',
9+
'flags.compactTasks': 'compactTasks',
10+
'flags.tasksDepth': 'tasksDepth',
11+
'flags.sortTasks': 'sortTasks',
912
};
1013

1114
function mergeConfigToCliFlags(opt, config) {

lib/shared/is-array.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
function isArray(val) {
4+
return Array.isArray(val);
5+
}
6+
7+
module.exports = isArray;

lib/shared/is-number.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
function isNumber(val) {
4+
return (typeof val === 'number');
5+
}
6+
7+
module.exports = isNumber;

lib/shared/log/copy-tree.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'use strict';
2+
3+
var isArray = require('../is-array');
4+
var isNumber = require('../is-number');
5+
6+
function copyNode(node) {
7+
var newNode = {};
8+
Object.keys(node).forEach(function(key) {
9+
newNode[key] = node[key];
10+
});
11+
return newNode;
12+
}
13+
14+
var defaultNodeFactory = {
15+
topNode: copyNode,
16+
taskNode: copyNode,
17+
childNode: copyNode,
18+
};
19+
20+
function copyTree(tree, opts, nodeFactory) {
21+
opts = opts || {};
22+
23+
var depth = opts.tasksDepth;
24+
depth = !isNumber(depth) ? null : ((depth < 1) ? 1 : depth);
25+
26+
nodeFactory = nodeFactory || defaultNodeFactory;
27+
28+
var newTree = nodeFactory.topNode(tree);
29+
newTree.nodes = [];
30+
31+
if (isArray(tree.nodes)) {
32+
tree.nodes.forEach(visit);
33+
}
34+
35+
function visit(node) {
36+
var newNode = nodeFactory.taskNode(node);
37+
newNode.nodes = [];
38+
newTree.nodes.push(newNode);
39+
40+
if (opts.compactTasks) {
41+
forEach(node.nodes, copyNotRecursively, newNode);
42+
43+
} else if (!depth || depth > 1) {
44+
forEach(node.nodes, copyRecursively, depth, 2, newNode);
45+
}
46+
}
47+
48+
function copyNotRecursively(child, newParent) {
49+
var newChild = nodeFactory.childNode(child);
50+
newChild.nodes = [];
51+
newParent.nodes.push(newChild);
52+
53+
if (child.branch) {
54+
forEach(child.nodes, copyNotRecursively, newChild);
55+
}
56+
}
57+
58+
function copyRecursively(child, maxDepth, nowDepth, newParent) {
59+
var newChild = nodeFactory.childNode(child);
60+
newChild.nodes = [];
61+
newParent.nodes.push(newChild);
62+
63+
if (!maxDepth || maxDepth > nowDepth) {
64+
forEach(child.nodes, copyRecursively, maxDepth, nowDepth + 1, newChild);
65+
}
66+
}
67+
68+
return newTree;
69+
}
70+
71+
function forEach(nodes, fn) {
72+
if (!isArray(nodes)) {
73+
return;
74+
}
75+
76+
var args = Array.prototype.slice.call(arguments, 2);
77+
78+
for (var i = 0, n = nodes.length; i < n; i++) {
79+
fn.apply(nodes[i], [nodes[i]].concat(args));
80+
}
81+
}
82+
83+
module.exports = copyTree;
84+

lib/shared/log/tasks.js

Lines changed: 52 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,24 @@ var log = require('gulplog');
66

77
var sortBy = require('lodash.sortby');
88
var isObject = require('lodash.isplainobject');
9-
109
var isString = require('../is-string');
1110

12-
function logTasks(tree, depth, getTask) {
13-
depth = (typeof depth !== 'number') ? null : ((depth < 1) ? 1 : depth);
11+
var copyTree = require('./copy-tree');
12+
13+
function sorter(node) {
14+
return node.label;
15+
}
16+
17+
function logTasks(tree, opts, getTask) {
18+
if (opts.sortTasks) {
19+
tree.nodes = sortBy(tree.nodes, sorter);
20+
}
1421

1522
var lineInfos = [];
1623
var entryObserver = getLineInfoCollector(lineInfos);
24+
var nodeFactory = getNodeFactory(getTask, entryObserver);
1725

18-
tree = copyTree(tree, depth, getTask, entryObserver);
19-
26+
tree = copyTree(tree, opts, nodeFactory);
2027
var spacer = getSpacerForLineIndents(tree, lineInfos);
2128
var lines = getLinesContainingOnlyBranches(tree);
2229

@@ -49,75 +56,52 @@ function getLineInfoCollector(lineInfos) {
4956
};
5057
}
5158

52-
function copyTree(tree, depth, getTask, entryObserver) {
53-
var newTree = {
54-
label: tree.label,
55-
nodes: [],
56-
};
57-
58-
sortBy(tree.nodes, sorter).forEach(visit);
59-
60-
function sorter(node) {
61-
return node.label;
62-
}
59+
function getNodeFactory(getTask, entryObserver) {
60+
return {
61+
topNode: function(node) {
62+
return {
63+
label: node.label,
64+
};
65+
},
6366

64-
function visit(node) {
65-
var task = getTask(node.label) || {};
66-
67-
var newNode = {
68-
label: node.label,
69-
desc: isString(task.description) ? task.description : '',
70-
opts: [],
71-
nodes: [],
72-
};
73-
entryObserver.topTask(newNode);
74-
newTree.nodes.push(newNode);
75-
76-
if (isObject(task.flags)) {
77-
Object.keys(task.flags).sort().forEach(function(flag) {
78-
if (flag.length === 0) {
79-
return;
80-
}
81-
var opt = {
82-
label: flag,
83-
desc: isString(task.flags[flag]) ? task.flags[flag] : '',
84-
};
85-
entryObserver.option(opt);
86-
newNode.opts.push(opt);
87-
newNode.label += '\n' + opt.label; // The way of archy for options.
88-
});
89-
}
67+
taskNode: function(node) {
68+
var task = getTask(node.label) || {};
9069

91-
if (!depth || depth > 1) {
92-
var fn = function(child, maxDepth, nowDepth, newParent) {
93-
var newChild = {
94-
label: child.label,
95-
nodes: [],
96-
};
97-
entryObserver.childTask(newChild);
98-
newChild.label = ''; // Because don't use child tasks to calc indents.
99-
newParent.nodes.push(newChild);
100-
if (!maxDepth || maxDepth > nowDepth) {
101-
forEachNode(child.nodes, fn, maxDepth, nowDepth + 1, newChild);
102-
}
70+
var newNode = {
71+
label: node.label,
72+
desc: isString(task.description) ? task.description : '',
73+
opts: [],
10374
};
104-
forEachNode(node.nodes, fn, depth, 2, newNode);
105-
}
106-
}
107-
108-
return newTree;
109-
}
75+
entryObserver.topTask(newNode);
76+
77+
if (isObject(task.flags)) {
78+
Object.keys(task.flags).sort().forEach(function(flag) {
79+
if (flag.length === 0) {
80+
return;
81+
}
82+
var opt = {
83+
label: flag,
84+
desc: isString(task.flags[flag]) ? task.flags[flag] : '',
85+
};
86+
entryObserver.option(opt);
87+
newNode.opts.push(opt);
88+
newNode.label += '\n' + opt.label; // The way of archy for options.
89+
});
90+
}
11091

111-
function forEachNode(nodes, fn) {
112-
if (!Array.isArray(nodes)) {
113-
return;
114-
}
92+
return newNode;
93+
},
11594

116-
var args = [].slice.call(arguments, 2);
95+
childNode: function(node) {
96+
var newChild = {
97+
label: node.label,
98+
};
99+
entryObserver.childTask(newChild);
100+
newChild.label = ''; // Because don't use child tasks to calc indents.
117101

118-
for (var i = 0, n = nodes.length; i < n; i++) {
119-
fn.apply(nodes[i], [nodes[i]].concat(args));
120-
}
102+
return newChild;
103+
},
104+
};
121105
}
122106

123107
function getSpacerForLineIndents(tree, lineInfos) {

lib/versioned/^3.7.0/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var stdout = require('mute-stdout');
66
var tildify = require('tildify');
77

88
var taskTree = require('./taskTree');
9+
910
var logTasks = require('../../shared/log/tasks');
1011
var isString = require('../../shared/is-string');
1112
var logEvents = require('./log/events');
@@ -34,17 +35,19 @@ function execute(opts, env, config) {
3435
stdout.unmute();
3536

3637
process.nextTick(function() {
38+
var tree;
39+
3740
if (opts.tasksSimple) {
3841
return logTasksSimple(env, gulpInst);
3942
}
4043
if (opts.tasks) {
41-
var tree = taskTree(gulpInst.tasks);
44+
tree = taskTree(gulpInst.tasks);
4245
if (config.description && isString(config.description)) {
4346
tree.label = config.description;
4447
} else {
4548
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
4649
}
47-
return logTasks(tree, opts.depth, function(task) {
50+
return logTasks(tree, opts, function(task) {
4851
return gulpInst.tasks[task].fn;
4952
});
5053
}

0 commit comments

Comments
 (0)