index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*jshint node:true */
  2. "use strict";
  3. var colors = require('chalk');
  4. var gulp = require('gulp');
  5. var DEFAULT_SUBTASK_REGEX = /[-_:]/,
  6. filter = function(inc, subtaskFilter) {
  7. return function(n) {
  8. var isSubtask = subtaskFilter(n);
  9. return (inc && isSubtask) || (!inc && !isSubtask);
  10. }
  11. },
  12. header = function(text) {
  13. console.log('');
  14. console.log(colors.gray(text));
  15. console.log('------------------------------');
  16. },
  17. regexFunc = function(rfn) {
  18. if(rfn && typeof rfn !== "function") {
  19. return function(t) {
  20. return t.search(rfn) !== -1;
  21. };
  22. }
  23. return rfn;
  24. },
  25. help = function(subtaskFilter, excludeFilter) {
  26. subtaskFilter = regexFunc(subtaskFilter || DEFAULT_SUBTASK_REGEX);
  27. excludeFilter = regexFunc(excludeFilter);
  28. return function(cb) {
  29. var tasks = gulp.tasks ? Object.keys(gulp.tasks).sort() : gulp.tree().nodes.sort();
  30. if(excludeFilter) {
  31. tasks = tasks.filter(function(task) {
  32. return !excludeFilter(task);
  33. });
  34. }
  35. header('Main Tasks');
  36. tasks.filter(filter(false, subtaskFilter)).forEach(function(name) {
  37. console.log(' ' + colors.cyan(name));
  38. });
  39. var subtasks = tasks.filter(filter(true, subtaskFilter));
  40. if(subtasks.length) {
  41. header('Sub Tasks');
  42. subtasks.forEach(function(name) {
  43. console.log(' ' + name);
  44. });
  45. }
  46. console.log('');
  47. // we're synchronous
  48. cb && cb();
  49. };
  50. };
  51. module.exports = help();
  52. module.exports.withFilters = function(subtaskFilter, excludeFilter) {
  53. return help(subtaskFilter, excludeFilter);
  54. };