|
3 | 3 | var archy = require('archy');
|
4 | 4 | var chalk = require('chalk');
|
5 | 5 | var log = require('gulplog');
|
| 6 | +var sortBy = require('lodash.sortby'); |
| 7 | + |
| 8 | +function isString(val) { |
| 9 | + return typeof val === 'string'; |
| 10 | +} |
| 11 | + |
| 12 | +function isObject(val) { |
| 13 | + return typeof val === 'object' && !Array.isArray(val); |
| 14 | +} |
| 15 | + |
| 16 | +function logTasks(tree, depth, getTask) { |
| 17 | + depth = (typeof depth !== 'number') ? null : ((depth < 1) ? 1 : depth); |
| 18 | + |
| 19 | + var lineInfos = []; |
| 20 | + var entryObserver = getLineInfoCollector(lineInfos); |
| 21 | + |
| 22 | + tree = copyTree(tree, depth, getTask, entryObserver); |
| 23 | + |
| 24 | + var spacer = getSpacerForLineIndents(tree, lineInfos); |
| 25 | + var lines = getLinesContainingOnlyBranches(tree); |
| 26 | + |
| 27 | + log.info(tree.label); |
| 28 | + printTreeList(lines, spacer, lineInfos); |
| 29 | +} |
| 30 | + |
| 31 | +function getLineInfoCollector(lineInfos) { |
| 32 | + return { |
| 33 | + topTask: function(node) { |
| 34 | + lineInfos.push({ |
| 35 | + name: node.label, |
| 36 | + desc: node.desc, |
| 37 | + type: 'top', |
| 38 | + }); |
| 39 | + }, |
| 40 | + option: function(opt) { |
| 41 | + lineInfos.push({ |
| 42 | + name: opt.label, |
| 43 | + desc: opt.desc, |
| 44 | + type: 'option', |
| 45 | + }); |
| 46 | + }, |
| 47 | + childTask: function(node) { |
| 48 | + lineInfos.push({ |
| 49 | + name: node.label, |
| 50 | + type: 'child', |
| 51 | + }); |
| 52 | + }, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +function copyTree(tree, depth, getTask, entryObserver) { |
| 57 | + var newTree = { |
| 58 | + label: tree.label, |
| 59 | + nodes: [], |
| 60 | + }; |
| 61 | + |
| 62 | + sortBy(tree.nodes, sorter).forEach(visit); |
| 63 | + |
| 64 | + function sorter(node) { |
| 65 | + return node.label; |
| 66 | + } |
| 67 | + |
| 68 | + function visit(node) { |
| 69 | + var task = getTask(node.label) || {}; |
| 70 | + |
| 71 | + var newNode = { |
| 72 | + label: node.label, |
| 73 | + desc: isString(task.description) ? task.description : '', |
| 74 | + opts: [], |
| 75 | + nodes: [], |
| 76 | + }; |
| 77 | + entryObserver.topTask(newNode); |
| 78 | + newTree.nodes.push(newNode); |
| 79 | + |
| 80 | + if (isObject(task.flags)) { |
| 81 | + Object.keys(task.flags).sort().forEach(function(flag) { |
| 82 | + if (flag.length === 0) { |
| 83 | + return; |
| 84 | + } |
| 85 | + var opt = { |
| 86 | + label: flag, |
| 87 | + desc: isString(task.flags[flag]) ? task.flags[flag] : '', |
| 88 | + }; |
| 89 | + entryObserver.option(opt); |
| 90 | + newNode.opts.push(opt); |
| 91 | + newNode.label += '\n' + opt.label; // The way of archy for options. |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + if (!depth || depth > 1) { |
| 96 | + var fn = function(child, maxDepth, nowDepth, newParent) { |
| 97 | + var newChild = { |
| 98 | + label: child.label, |
| 99 | + nodes: [], |
| 100 | + }; |
| 101 | + entryObserver.childTask(newChild); |
| 102 | + newChild.label = ''; // Because don't use child tasks to calc indents. |
| 103 | + newParent.nodes.push(newChild); |
| 104 | + if (!maxDepth || maxDepth > nowDepth) { |
| 105 | + forEachNode(child.nodes, fn, maxDepth, nowDepth + 1, newChild); |
| 106 | + } |
| 107 | + }; |
| 108 | + forEachNode(node.nodes, fn, depth, 2, newNode); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return newTree; |
| 113 | +} |
| 114 | + |
| 115 | +function forEachNode(nodes, fn) { |
| 116 | + if (!Array.isArray(nodes)) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + var args = [].slice.call(arguments, 2); |
| 121 | + |
| 122 | + for (var i = 0, n = nodes.length; i < n; i++) { |
| 123 | + fn.apply(nodes[i], [nodes[i]].concat(args)); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +function getSpacerForLineIndents(tree, lineInfos) { |
| 128 | + var maxSize = 0; |
| 129 | + var sizes = []; |
6 | 130 |
|
7 |
| -function logTasks(tree, getDescription) { |
8 |
| - var padding = 0; |
9 |
| - var rdependency = /[ │] [├└]/; |
10 | 131 | archy(tree)
|
11 | 132 | .split('\n')
|
12 |
| - .filter(function(v, i) { |
13 |
| - // Log first line as is |
14 |
| - if (i === 0) { |
15 |
| - log.info(v); |
16 |
| - return false; |
| 133 | + .slice(1, -1) |
| 134 | + .forEach(function(line, index) { |
| 135 | + var info = lineInfos[index]; |
| 136 | + if (info.type === 'top' || info.type === 'option') { |
| 137 | + maxSize = Math.max(maxSize, line.length); |
| 138 | + sizes.push(line.length); |
| 139 | + } else { |
| 140 | + sizes.push(0); |
17 | 141 | }
|
18 |
| - // Search for longest line |
19 |
| - if (v.length > padding) { |
20 |
| - padding = v.length; |
| 142 | + }); |
| 143 | + |
| 144 | + maxSize += 3; |
| 145 | + |
| 146 | + return function(index) { |
| 147 | + return Array(maxSize - sizes[index]).join(' '); |
| 148 | + }; |
| 149 | +} |
| 150 | + |
| 151 | +function getLinesContainingOnlyBranches(tree) { |
| 152 | + tree.nodes.forEach(function(node) { |
| 153 | + node.label = ''; |
| 154 | + node.opts.forEach(function() { |
| 155 | + node.label += '\n'; |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + return archy(tree) |
| 160 | + .split('\n') |
| 161 | + .slice(1, -1); |
| 162 | +} |
| 163 | + |
| 164 | +function printTreeList(lines, spacer, lineInfos) { |
| 165 | + lines.forEach(function(branch, index) { |
| 166 | + var info = lineInfos[index]; |
| 167 | + |
| 168 | + var line = chalk.white(branch); |
| 169 | + |
| 170 | + if (info.type === 'top') { |
| 171 | + line += chalk.cyan(info.name); |
| 172 | + if (info.desc.length > 0) { |
| 173 | + line += spacer(index) + chalk.white(info.desc); |
21 | 174 | }
|
22 |
| - return v.trim().length !== 0; |
23 |
| - }).forEach(function(v) { |
24 |
| - var line = v.split(' '); |
25 |
| - var task = line.slice(1).join(' '); |
26 |
| - |
27 |
| - // Log dependencies as is |
28 |
| - if (rdependency.test(v)) { |
29 |
| - log.info(v); |
30 |
| - return; |
| 175 | + } else if (info.type === 'option') { |
| 176 | + line += chalk.magenta(info.name); |
| 177 | + if (info.desc.length > 0) { |
| 178 | + line += spacer(index) + chalk.white('…' + info.desc); |
31 | 179 | }
|
| 180 | + } else { // If (info.type === 'child') { |
| 181 | + line += chalk.white(info.name); |
| 182 | + } |
32 | 183 |
|
33 |
| - // Pretty task with optional description |
34 |
| - log.info( |
35 |
| - line[0] + ' ' + chalk.cyan(task) + |
36 |
| - Array(padding + 3 - v.length).join(' ') + |
37 |
| - (getDescription(task) || '') |
38 |
| - ); |
39 |
| - }); |
| 184 | + log.info(line); |
| 185 | + }); |
40 | 186 | }
|
41 | 187 |
|
42 | 188 | module.exports = logTasks;
|
| 189 | + |
0 commit comments