-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
160 lines (136 loc) · 4.66 KB
/
index.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var objectAssign = require('object-assign'),
chalk = require('chalk'),
attachHelp = require('./lib/attach-help.js'),
calculateMargin = require('./lib/calculate-margin.js'),
noop = require('./lib/noop'),
DEFAULT_OPTIONS = {
aliases: [],
description: 'Display this help text.',
afterPrintCallback: noop,
hideDepsMessage: false,
hideEmpty: false
};
module.exports = function (gulp, options) {
var originalTaskFn = gulp.task;
options = objectAssign({}, DEFAULT_OPTIONS, options);
/**
* gulp.task(name[, help, deps, fn, taskOptions])
*
* Adds `help` and `taskOptions` to the typical gulp task definition:
* https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulptaskname-deps-fn
* @param {string} name
* @param {string | boolean} [help]
* @param {Array} [deps]
* @param {function} [fn]
* @param {object} [taskOptions]
*/
gulp.task = function (name, help, deps, fn, taskOptions) {
var task;
/* jshint noempty: false */
if (name && (help === null || help === undefined)) {
// just a name. do nothing.
} else if (help === false) {
// .task('test', false, ...)
//ignoredTasks.push(name);
if (typeof deps === 'function') {
// .task('test', false, function(){}, {})
taskOptions = fn;
fn = deps;
deps = undefined;
} else {
// .task('test', false, ['dep'], function(){}, {})
// nothing needs to be re-assigned
}
} else if (typeof help === 'function') {
// .task('test', function(){})
taskOptions = deps;
fn = undefined;
deps = help;
help = undefined;
} else if (Array.isArray(help)) {
// .task('test', ['dep'], ...)
taskOptions = fn;
fn = deps;
deps = help;
help = undefined;
} else if (name && !deps) {
// .task('test', '...')
// help text with no func and no deps
} else if (typeof deps === 'function') {
// .task('test', '...', function, {})
taskOptions = fn;
fn = deps;
deps = undefined;
} else if (Array.isArray(deps)) {
// .task('test', '...', ['dep'], function, {})
// nothing needs to be re-assigned
} else {
throw new Error('gulp-help: Unexpected arg types. Should be in the form: `gulp.task(name[, help, deps, fn, taskOptions])`');
}
if (!deps) {
originalTaskFn.call(gulp, name, fn);
} else {
originalTaskFn.call(gulp, name, deps, fn);
}
task = gulp.tasks[name];
taskOptions = objectAssign({
aliases: []
}, taskOptions);
taskOptions.aliases.forEach(function (alias) {
gulp.task(alias, false, [name], noop);
});
attachHelp(task, help, deps, taskOptions);
return gulp;
};
gulp.task('help', options.description, function () {
var marginData = calculateMargin(gulp.tasks);
var margin = marginData.margin;
var hideDepsMessageOpt = options.hideDepsMessage;
var hideEmptyOpt = options.hideEmpty;
var showAllTasks = process.argv.indexOf('--all') !== -1;
var afterPrintCallback = options.afterPrintCallback;
// set options buffer if the tasks array has options
var optionsBuffer = marginData.hasOptions ? ' --' : '';
console.log('');
console.log(chalk.underline('Usage'));
console.log(' gulp [TASK] [OPTIONS...]');
console.log('');
console.log(chalk.underline('Available tasks'));
Object.keys(gulp.tasks).sort().forEach(function (name) {
if (gulp.tasks[name].help || showAllTasks) {
var help = gulp.tasks[name].help || {message: '', options: {}};
if (!showAllTasks && help.message === '' && hideEmptyOpt) {
return; //skip task
}
var args = [' ', chalk.cyan(name)];
args.push(new Array(margin - name.length + 1 + optionsBuffer.length).join(' '));
if (help.message) {
args.push(help.message);
}
if (help.aliases) {
args.push(help.aliases);
}
if (help.depsMessage && !hideDepsMessageOpt) {
args.push(chalk.cyan(help.depsMessage));
}
var options = Object.keys(help.options).sort();
options.forEach(function (option) {
var optText = help.options[option];
args.push('\n ' + optionsBuffer + chalk.cyan(option) + ' ');
args.push(new Array(margin - option.length + 1).join(' '));
args.push(optText);
});
console.log.apply(console, args);
}
});
console.log('');
if (afterPrintCallback) {
afterPrintCallback(gulp.tasks);
}
}, options);
// do not add default task if one already exists
if (gulp.tasks['default'] === undefined) {
gulp.task('default', false, ['help']);
}
return gulp;
};