index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. var objectAssign = require('object-assign'),
  2. chalk = require('chalk'),
  3. attachHelp = require('./lib/attach-help.js'),
  4. calculateMargin = require('./lib/calculate-margin.js'),
  5. noop = require('./lib/noop'),
  6. DEFAULT_OPTIONS = {
  7. aliases: [],
  8. description: 'Display this help text.',
  9. afterPrintCallback: noop,
  10. hideDepsMessage: false,
  11. hideEmpty: false
  12. };
  13. module.exports = function (gulp, options) {
  14. var originalTaskFn = gulp.task;
  15. options = objectAssign({}, DEFAULT_OPTIONS, options);
  16. /**
  17. * gulp.task(name[, help, deps, fn, taskOptions])
  18. *
  19. * Adds `help` and `taskOptions` to the typical gulp task definition:
  20. * https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulptaskname-deps-fn
  21. * @param {string} name
  22. * @param {string | boolean} [help]
  23. * @param {Array} [deps]
  24. * @param {function} [fn]
  25. * @param {object} [taskOptions]
  26. */
  27. gulp.task = function (name, help, deps, fn, taskOptions) {
  28. var task;
  29. /* jshint noempty: false */
  30. if (name && (help === null || help === undefined)) {
  31. // just a name. do nothing.
  32. } else if (help === false) {
  33. // .task('test', false, ...)
  34. //ignoredTasks.push(name);
  35. if (typeof deps === 'function') {
  36. // .task('test', false, function(){}, {})
  37. taskOptions = fn;
  38. fn = deps;
  39. deps = undefined;
  40. } else {
  41. // .task('test', false, ['dep'], function(){}, {})
  42. // nothing needs to be re-assigned
  43. }
  44. } else if (typeof help === 'function') {
  45. // .task('test', function(){})
  46. taskOptions = deps;
  47. fn = undefined;
  48. deps = help;
  49. help = undefined;
  50. } else if (Array.isArray(help)) {
  51. // .task('test', ['dep'], ...)
  52. taskOptions = fn;
  53. fn = deps;
  54. deps = help;
  55. help = undefined;
  56. } else if (name && !deps) {
  57. // .task('test', '...')
  58. // help text with no func and no deps
  59. } else if (typeof deps === 'function') {
  60. // .task('test', '...', function, {})
  61. taskOptions = fn;
  62. fn = deps;
  63. deps = undefined;
  64. } else if (Array.isArray(deps)) {
  65. // .task('test', '...', ['dep'], function, {})
  66. // nothing needs to be re-assigned
  67. } else {
  68. throw new Error('gulp-help: Unexpected arg types. Should be in the form: `gulp.task(name[, help, deps, fn, taskOptions])`');
  69. }
  70. if (!deps) {
  71. originalTaskFn.call(gulp, name, fn);
  72. } else {
  73. originalTaskFn.call(gulp, name, deps, fn);
  74. }
  75. task = gulp.tasks[name];
  76. taskOptions = objectAssign({
  77. aliases: []
  78. }, taskOptions);
  79. taskOptions.aliases.forEach(function (alias) {
  80. gulp.task(alias, false, [name], noop);
  81. });
  82. attachHelp(task, help, deps, taskOptions);
  83. return gulp;
  84. };
  85. gulp.task('help', options.description, function () {
  86. var marginData = calculateMargin(gulp.tasks);
  87. var margin = marginData.margin;
  88. var hideDepsMessageOpt = options.hideDepsMessage;
  89. var hideEmptyOpt = options.hideEmpty;
  90. var showAllTasks = process.argv.indexOf('--all') !== -1;
  91. var afterPrintCallback = options.afterPrintCallback;
  92. // set options buffer if the tasks array has options
  93. var optionsBuffer = marginData.hasOptions ? ' --' : '';
  94. console.log('');
  95. console.log(chalk.underline('Usage'));
  96. console.log(' gulp [TASK] [OPTIONS...]');
  97. console.log('');
  98. console.log(chalk.underline('Available tasks'));
  99. Object.keys(gulp.tasks).sort().forEach(function (name) {
  100. if (gulp.tasks[name].help || showAllTasks) {
  101. var help = gulp.tasks[name].help || {message: '', options: {}};
  102. if (!showAllTasks && help.message === '' && hideEmptyOpt) {
  103. return; //skip task
  104. }
  105. var args = [' ', chalk.cyan(name)];
  106. args.push(new Array(margin - name.length + 1 + optionsBuffer.length).join(' '));
  107. if (help.message) {
  108. args.push(help.message);
  109. }
  110. if (help.aliases) {
  111. args.push(help.aliases);
  112. }
  113. if (help.depsMessage && !hideDepsMessageOpt) {
  114. args.push(chalk.cyan(help.depsMessage));
  115. }
  116. var options = Object.keys(help.options).sort();
  117. options.forEach(function (option) {
  118. var optText = help.options[option];
  119. args.push('\n ' + optionsBuffer + chalk.cyan(option) + ' ');
  120. args.push(new Array(margin - option.length + 1).join(' '));
  121. args.push(optText);
  122. });
  123. console.log.apply(console, args);
  124. }
  125. });
  126. console.log('');
  127. if (afterPrintCallback) {
  128. afterPrintCallback(gulp.tasks);
  129. }
  130. }, options);
  131. // do not add default task if one already exists
  132. if (gulp.tasks['default'] === undefined) {
  133. gulp.task('default', false, ['help']);
  134. }
  135. return gulp;
  136. };