Skip to content

Instantly share code, notes, and snippets.

@mildmojo
Created May 29, 2015 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mildmojo/3710eeedf4fe15a9b3e5 to your computer and use it in GitHub Desktop.
Save mildmojo/3710eeedf4fe15a9b3e5 to your computer and use it in GitHub Desktop.
Make gulp look more like rake
/*
gulpfile.js
Gulp lacks a native way to provide task documentation or break tasks
into multiple files. This gulpfile reads './tasks/*.js' and can list
all available tasks with `gulp` or `gulp tasks`. If you set a .desc
property on your task functions, those descriptions will show up in
`gulp tasks` output. Tasks without descriptions will be omitted
from this output (but still visible with `gulp -T`).
Task files should export a function that takes the gulp object as its
sole argument. E.g. `module.exports = function(gulp) { gulp.task... };`
See the 'tasks' task in this file for an example task definition.
*/
var fs = require('fs');
var path = require('path');
var gulp = require('gulp');
var TASK_PATH = './tasks';
// Import other gulp taskfiles.
var taskFiles = fs.readdirSync(TASK_PATH);
taskFiles.forEach(function(file) {
if (!file.match(/\.js$/)) return;
require(path.resolve(TASK_PATH, file))(gulp);
});
gulp.task('default', ['tasks']);
gulp.task('tasks', tasks);
tasks.desc = 'List all gulp tasks';
function tasks() {
var allTasks = Object.keys(gulp.tasks)
.sort()
.map(function(n) { return gulp.tasks[n]; });
var taskNames = allTasks.map(function(t) { return t.name; });
var maxLen = longestString(taskNames).length;
console.log('');
console.log('Available tasks:');
console.log('================');
allTasks.forEach(function(task) {
if (task.name === 'default') return;
if (!task.fn.desc) return;
console.log('%s - %s', ljust(task.name, maxLen + 1), task.fn.desc);
});
console.log('');
}
function ljust(str, size) {
return str + Array(size - str.length).join(' ');
}
function longestString(strings) {
return strings.reduce(function(max, name) {
return name.length > max.length ? name : max;
}, '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment