Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-configuration #4

Open
wants to merge 1 commit into
base: v1.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});