Skip to content

Commit

Permalink
parameterized tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
reaganthomas committed Jun 22, 2015
1 parent c086933 commit 5b86eda
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 61 deletions.
55 changes: 53 additions & 2 deletions README.md
Expand Up @@ -97,10 +97,12 @@ taskLoader({ plugins: plugins });

All tasks should be functions that receive the parameters `gulp`, `config`, and `plugins`.

There are 2 ways to structure a task -- returning a function that executes the task, or returning an object that contains dependencies and the function that executes the task.
There are 2 ways to structure a task -- returning a function that executes the task, or returning an object that contains dependencies, parameters, and the function that executes the task.

### Basic tasks

This is a typical function as you are used to with gulp.

```js
'use strict';

Expand All @@ -111,7 +113,9 @@ module.exports = function(gulp, config, plugins) {

### Tasks with dependencies

Both `deps` and `fn` are optional. This allows you to create a task that strictly calls other tasks, or a task that doesn't have dependencies. If there are no dependencies for the task you can use the above format for creating a basic task.
All 3 object keys (`deps`, `params`, and `fn`) are optional. This allows you to create a task that strictly calls other tasks, a task that is parameterized, or a task that just acts like a normal task.

If there are no dependencies or parameters for the task you can use the above "Basic task" format for creating a basic task.

The values shown below are the defaults.

Expand All @@ -121,11 +125,20 @@ The values shown below are the defaults.
module.exports = function(gulp, config, plugins) {
return {
deps: [], // an array of task names to execute before this task
params: [], // an array of parameters to send to `fn`
fn: function([callback]) {} // the task functionality -- callback optional
};
};
```

Please note that if you use the `params` key your `fn` must be of the following form:

```js
params: [ 'a', 'b' ],
fn: function(param, cb) {} // where param is an item from the params array,
// and cb is a callback to be called at the end of your function
```

## Complete examples

### Using gulp-load-plugins
Expand Down Expand Up @@ -182,3 +195,41 @@ module.exports = function(gulp, config, plugins) {
};
};
```

### Parameterize tasks

```js
(gulpfile.js)

'use strict';

var taskLoader = require('gulp-simple-task-loader');
var plugins = require('gulp-load-plugins');

taskLoader({
filenameDelimiter: '-',
tasknameDelimiter: ':',
plugins: plugins
});
```

```js
(tasks/parameterized.js)

'use strict';

module.exports = function(gulp, config, plugins) {
return {
params: [ '1', '2' ],
fn: function(param, cb) {
console.log(param)
}
};
};
```

The task in `parameterized.js` would produce the following output:
```bash
1
2
```
65 changes: 42 additions & 23 deletions build/index.js
@@ -1,15 +1,16 @@
"use strict";
'use strict';

var fs = require("fs");
var _ = require("lodash");
var gulp = require("gulp");
var path = require("path");
var async = require('async');
var gulp = require('gulp');
var path = require('path');
var _ = require('lodash');
var fs = require('fs');

var defaultOptions = {
taskDirectory: "gulp-tasks",
taskDirectory: 'gulp-tasks',
plugins: {},
filenameDelimiter: "",
tasknameDelimiter: "",
filenameDelimiter: '',
tasknameDelimiter: '',
config: {}
};

Expand All @@ -20,48 +21,66 @@ function voidFunction() {
module.exports = function (options) {
options = _.assign(defaultOptions, options);

if (typeof options.taskDirectory !== "string") {
throw new Error("Task directory must be a string containing the relative path to a task directory");
if (typeof options.taskDirectory !== 'string') {
throw new Error('Task directory must be a string containing the relative path to a task directory');
}

if (options.taskDirectory.slice(0, 2) === "." + path.sep) {
if (options.taskDirectory.slice(0, 2) === '.' + path.sep) {
options.taskDirectory = options.taskDirectory.slice(2);
}

options.taskDirectory = path.join(process.cwd(), options.taskDirectory);

var dirStat = fs.statSync(options.taskDirectory);
if (!dirStat.isDirectory()) {
throw new Error(options.taskDirectory + " is not a directory");
throw new Error(options.taskDirectory + ' is not a directory');
}

function processDirectory(dir) {
fs.readdirSync(dir).filter(function (filename) {
function filterFilenames(filename) {
var file = path.resolve(dir, filename);
var extname = path.extname(filename);
return extname === ".js" || extname === ".coffee" || fs.statSync(file).isDirectory();
}).map(function (filename) {
return extname === '.js' || extname === '.coffee' || fs.statSync(file).isDirectory();
}

function mapFiles(filename) {
var file = path.resolve(dir, filename);

if (fs.statSync(file).isDirectory()) {
return { directory: true, filename: filename };
} else {
var taskname = path.basename(filename, path.extname(filename));
taskname = taskname.split(options.filenameDelimiter).join(options.tasknameDelimiter);

return { file: file, filename: filename, taskname: taskname };
}
}).forEach(function (obj) {
}

function createTask(obj) {
if (obj.directory) {
processDirectory(dir + "/" + obj.filename);
processDirectory(dir + '/' + obj.filename);
} else {
var taskinfo = require(obj.file)(gulp, _.defaults(options.config, _.omit(options, ["config", "plguins"])), options.plugins);
var taskdeps = taskinfo.deps || [];
var taskfn = taskinfo.deps || taskinfo.fn ? taskinfo.fn || voidFunction : taskinfo;
(function () {
var taskinfo = require(obj.file)(gulp, _.defaults(options.config, _.omit(options, ['config', 'plugins'])), options.plugins);
var taskdeps = taskinfo.deps || [];
var taskparams = taskinfo.params || [];
var taskfn = taskinfo.deps || taskinfo.fn || taskinfo.params ? taskinfo.fn || voidFunction : taskinfo;

gulp.task(obj.taskname, taskdeps, taskfn);
if (taskparams.length > 0) {
gulp.task(obj.taskname, taskdeps, function () {
async.map(taskparams, function (params, callback) {
taskfn(params, callback);
}, function (err, results) {
if (err) throw new Error(err);
});
});
} else {
gulp.task(obj.taskname, taskdeps, taskfn);
}
})();
}
});
}

fs.readdirSync(dir).filter(filterFilenames).map(mapFiles).forEach(createTask);
}

processDirectory(options.taskDirectory);
Expand Down
7 changes: 4 additions & 3 deletions gulpfile.js
Expand Up @@ -4,11 +4,12 @@ var gulp = require('gulp');
var to5 = require('gulp-6to5');
var bump = require('gulp-bump');
var mocha = require('gulp-mocha');
var babel = require('gulp-babel');
var istanbul = require('gulp-istanbul');

gulp.task('build', function() {
return gulp.src('index.js')
.pipe(to5())
.pipe(babel())
.pipe(gulp.dest('./build'));
});

Expand All @@ -23,7 +24,7 @@ gulp.task('coverage', [ 'build' ], function(cb) {
.pipe(istanbul.hookRequire())
.on('finish', function() {
gulp.src('./test/**/*.js')
.pipe(mocha({ reporter: 'dot' }))
.pipe(mocha({ reporter: 'spec' }))
.pipe(istanbul.writeReports())
.on('end', cb);
});
Expand All @@ -35,4 +36,4 @@ gulp.task('bump', [ 'build', 'test' ], function() {
.pipe(gulp.dest('./'));
});

gulp.task('default', [ 'build', 'test', 'bump' ]);
gulp.task('default', [ 'build', 'test', 'bump' ]);
71 changes: 44 additions & 27 deletions index.js
@@ -1,9 +1,10 @@
'use strict';

var fs = require('fs');
var _ = require('lodash');
var async = require('async');
var gulp = require('gulp');
var path = require('path');
var _ = require('lodash');
var fs = require('fs');

var defaultOptions = {
taskDirectory: 'gulp-tasks',
Expand Down Expand Up @@ -36,35 +37,51 @@ module.exports = function(options) {
}

function processDirectory(dir) {
fs.readdirSync(dir)
.filter(function(filename) {
let file = path.resolve(dir, filename);
var extname = path.extname(filename);
return (extname === '.js' || extname === '.coffee' || fs.statSync(file).isDirectory());
})
.map(function(filename) {
let file = path.resolve(dir, filename);

if(fs.statSync(file).isDirectory()) {
return { directory: true, filename: filename };
} else {
let taskname = path.basename(filename, path.extname(filename));
taskname = taskname.split(options.filenameDelimiter).join(options.tasknameDelimiter);
function filterFilenames(filename) {
let file = path.resolve(dir, filename);
let extname = path.extname(filename);
return (extname === '.js' || extname === '.coffee' || fs.statSync(file).isDirectory());
}

return { file: file, filename: filename, taskname: taskname };
}
})
.forEach(function(obj) {
if(obj.directory) {
processDirectory(dir + '/' + obj.filename);
} else {
let taskinfo = require(obj.file)(gulp, _.defaults(options.config, _.omit(options, [ 'config', 'plguins' ])), options.plugins);
let taskdeps = taskinfo.deps || [];
let taskfn = (taskinfo.deps || taskinfo.fn) ? (taskinfo.fn || voidFunction) : taskinfo;
function mapFiles(filename) {
let file = path.resolve(dir, filename);

if(fs.statSync(file).isDirectory()) {
return { directory: true, filename: filename };
} else {
let taskname = path.basename(filename, path.extname(filename));
taskname = taskname.split(options.filenameDelimiter).join(options.tasknameDelimiter);
return { file: file, filename: filename, taskname: taskname };
}
}

function createTask(obj) {
if(obj.directory) {
processDirectory(dir + '/' + obj.filename);
} else {
let taskinfo = require(obj.file)(gulp, _.defaults(options.config, _.omit(options, [ 'config', 'plugins' ])), options.plugins);
let taskdeps = taskinfo.deps || [];
let taskparams = taskinfo.params || [];
let taskfn = (taskinfo.deps || taskinfo.fn || taskinfo.params) ? (taskinfo.fn || voidFunction) : taskinfo;

if(taskparams.length > 0) {
gulp.task(obj.taskname, taskdeps, function() {
async.map(taskparams, function(params, callback) {
taskfn(params, callback);
}, function(err, results) {
if(err) throw new Error(err);
});
});
} else {
gulp.task(obj.taskname, taskdeps, taskfn);
}
});
}
}

fs.readdirSync(dir)
.filter(filterFilenames)
.map(mapFiles)
.forEach(createTask);
}

processDirectory(options.taskDirectory);
Expand Down
5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "gulp-simple-task-loader",
"version": "1.0.36",
"version": "1.0.37",
"description": "A simple task loader for gulp",
"keywords": [
"gulp",
Expand All @@ -22,13 +22,14 @@
"url": "http://github.com/reaganthomas/gulp-simple-task-loader"
},
"dependencies": {
"async": "^1.2.1",
"gulp": "^3.8.11",
"lodash": "^3.3.1"
},
"devDependencies": {
"coffee-script": "^1.9.1",
"coveralls": "^2.11.2",
"gulp-6to5": "^3.0.0",
"gulp-babel": "^5.1.0",
"gulp-bump": "^0.2.2",
"gulp-istanbul": "^0.6.0",
"gulp-mocha": "^2.0.0",
Expand Down

0 comments on commit 5b86eda

Please sign in to comment.