-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulp-metadata.js
117 lines (94 loc) · 3.75 KB
/
gulp-metadata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var _ = require('lodash'),
chalk = require('chalk'),
changeCase = require('change-case'),
Table = require('easy-table'),
logSymbols = require('log-symbols');
module.exports = {
applyTo: function (gulp) {
var originalTaskFunc;
if (!gulp.metadata) {
gulp.metadata = {
store: {},
longestName: 0,
add: function (name, metadata) {
metadata.name = name;
if (name.length > gulp.metadata.longestName) {
gulp.metadata.longestName = name.length;
}
gulp.metadata.store[name] = metadata;
},
describeAll: function () {
var tables = {},
output = '',
groupedTasks;
function makeSection(table, task) {
var descriptionLines =
table.cell(0, task.name, function printName(name, width) {
if (width) {
return chalk.cyan(name);
} else {
// Gotta pad with a real character here to fake the width during measurement
return _.padEnd(name, gulp.metadata.longestName, '-');
}
return ;
});
table.cell(1, task.description, _.ary(chalk.grey, 1));
_.each(task.arguments, function makeArgumentRow(argDescription, argName) {
table.newRow();
table.cell(0, ' --' + argName + '=[...]', _.ary(chalk.cyan, 1));
table.cell(1, argDescription, _.ary(chalk.dim, 1));
});
_.each(task.notes, function makeNoteRow(note) {
var noteLine = ' ' + logSymbols.warning + ' ' + note;
table.newRow();
table.cell(0, '');
table.cell(1, noteLine, _.ary(chalk.yellow, 1));
});
table.newRow();
}
function makeHeader(category) {
var base = '-- ' + changeCase.title(category) + ' Tasks ',
padLength = 40;
return chalk.blue(_.padEnd(base, padLength, '-'));
}
groupedTasks = _.groupBy(gulp.metadata.store, 'category');
// In each cagtegory, sort by weight
_.each(groupedTasks, function categoryLoop(tasks, category) {
var sortedTasks,
table = new Table();
sortedTasks = _.sortBy(tasks, function sortTask(task) {
if (_.isNumber(task.weight)) {
return task.weight * -1;
} else {
return -9999;
}
});
_.each(sortedTasks, _.partial(makeSection, table));
tables[category] = table;
});
if (tables.main) {
output += '\n' + makeHeader('main') + '\n' + tables.main.print() + '\n';
}
return _.reduce(tables, function makeOutput(output, table, category) {
if (category !== 'main') {
output += makeHeader(category) + '\n' + table.print() + '\n';
}
return output;
}, output);
}
};
// Rewrite gulp.task function to accept metadata
originalTaskFunc = gulp.task;
gulp.task = function () {
var metadata = _.last(arguments);
if (_.isObject(metadata)) {
originalTaskFunc.apply(this, _.initial(arguments));
gulp.metadata.add(_.first(arguments), metadata);
} else {
originalTaskFunc.apply(this, arguments);
}
};
}
return gulp;
}
};