Skip to content

Commit

Permalink
Add a simple POC allowing multiple configurations per "recipe" by pas…
Browse files Browse the repository at this point in the history
…sing "moduleName" in configuration.
  • Loading branch information
amobiz committed Feb 1, 2016
1 parent 15c5e41 commit 956c320
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
19 changes: 13 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,26 @@ module.exports = function (gulp, cozyPath) {

var files = fs.readdirSync(cozyPath);
files.forEach(function (file) {
var taskName = file.substr(0, file.lastIndexOf('.'));
cozy.require(taskName, options[taskName] || {});
var moduleName = file.substr(0, file.lastIndexOf('.'));
if (!options[moduleName]) {
options[moduleName] = {};
}
});
Object.keys(options).forEach(function (taskName) {
var taskOptions = options[taskName];
cozy.require(taskOptions.moduleName || taskName, taskName, taskOptions);
});
}

/**
* Require a specific task from the current cozyPath. If the task is not present in the cozyPath it will try to
* fall back to a regular module from the `node_modules` folder.
* @param {String} taskName - the name of the task to require
* @param {String} moduleName - the name of the module to require
* @param {String} taskName - the name of the task to register
* @param {Object} taskOptions - an optional configuration object to be passed to the task factory function
*/
cozy.require = function (taskName, taskOptions) {
var taskPath = path.join(cozyPath, taskName);
cozy.require = function (moduleName, taskName, taskOptions) {
var taskPath = path.join(cozyPath, moduleName);

var task;
try {
Expand All @@ -56,7 +63,7 @@ module.exports = function (gulp, cozyPath) {
throw e;
}

task = require(taskName);
task = require(moduleName);
}

if (task instanceof Function) {
Expand Down
8 changes: 8 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ test('it should include assert module as a task', function (t) {
t.ok(gulp.hasTask('assert'), 'Gulp task has been added');
t.ok(gulp.start('assert'), 'Gulp task has been executed correctly');
});

test('it should include assert module as a task indirectly', function (t) {
t.plan(3);
cozy({anotherAssert: {moduleName: 'assert'}});
t.ok(gulp.hasTask('assert'), 'Gulp task has been added');
t.ok(gulp.hasTask('anotherAssert'), 'Gulp task has been added');
t.ok(gulp.start('anotherAssert'), 'Gulp task has been executed correctly');
});

0 comments on commit 956c320

Please sign in to comment.